图集SpriteMetaData数据拷贝

关于SpriteMetaData类:

用于生成精灵的编辑器数据。
官方文档介绍:https://docs.unity3d.com/2017.2/Documentation/ScriptReference/SpriteMetaData.html

在这里插入图片描述

工具界面

在这里插入图片描述

工具使用场景

适用于游戏中存在多套皮肤图集

使用方法

拖入数据源图集的texture、克隆图集的texture,然后点击Copy,就会将clone texture 的Sprite data 拷贝到clone texture 中,对应关系是按照sprite name来对比的。

具体代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Editor.CopyAtlasDataTool
{
public class CopyAtlasDataTool : EditorWindow
{
private static CopyAtlasDataTool window = null;

[MenuItem("Tools/CopyAtlasDataTool")]
public static CopyAtlasDataTool ShowGameDataEditorWindow()
{
if (window == null)
window = EditorWindow.GetWindow(typeof(CopyAtlasDataTool)) as CopyAtlasDataTool;
window.titleContent = new GUIContent("CopyAtlasDataTool");
window.Show();
return window;
}

private UnityEngine.Object _originObject;
private UnityEngine.Object _cloneObject;
private void OnGUI()
{
_originObject = EditorGUILayout.ObjectField("数据源图集", _originObject,typeof(Texture));
_cloneObject = EditorGUILayout.ObjectField("克隆图集", _cloneObject, typeof(Texture));
if (GUILayout.Button("Copy"))
{
if (_originObject != null && _cloneObject != null)
{
CopyData(_originObject, _cloneObject);
}
}
}

public void CopyData(Object origin, Object clone)
{
string originAssetPath = AssetDatabase.GetAssetPath(origin);
string cloneAssetPath = AssetDatabase.GetAssetPath(clone);

TextureImporter originTi = AssetImporter.GetAtPath(originAssetPath) as TextureImporter;
TextureImporter cloneTi = AssetImporter.GetAtPath(cloneAssetPath) as TextureImporter;


if (originTi != null && cloneTi != null)
{
//打开读写,才能写入数据
cloneTi.isReadable = true;

List < SpriteMetaData > newData = new List < SpriteMetaData > ();
List<string> hasAddData = new List<string>();
for (int i = 0; i < originTi.spritesheet.Length; i++)
{
for (int j = 0; j < cloneTi.spritesheet.Length; j++)
{
SpriteMetaData oSm = originTi.spritesheet[i];
SpriteMetaData cSm = cloneTi.spritesheet[j];
//根绝sprite name进行匹配的
if (oSm.name == cSm.name)
{
cSm.border = oSm.border;
cSm.pivot = oSm.pivot;
cSm.alignment = oSm.alignment;

newData.Add(cSm);
hasAddData.Add(cSm.name);
}
}
}

for (int j = 0; j < cloneTi.spritesheet.Length; j++)
{
SpriteMetaData cSm = cloneTi.spritesheet[j];
if (!hasAddData.Contains(cSm.name))
{
newData.Add(cSm);
}
}

cloneTi.spritesheet = newData.ToArray();

AssetDatabase.ImportAsset(cloneAssetPath, ImportAssetOptions.ForceUpdate);

cloneTi = AssetImporter.GetAtPath(cloneAssetPath) as TextureImporter;
if(cloneTi != null)
cloneTi.isReadable = false;

AssetDatabase.ImportAsset(cloneAssetPath, ImportAssetOptions.ForceUpdate);
}
}
}
}

注意点

(1)克隆图集的TextureImporter的isReadable要在复制前打开,不然不能写入数据
(2)SpriteMetaData是结构体,要注意结构体和类的区别
(3)copy完后,注意检查两个图集的读写是否正确关闭了

以上知识分享,如有错误,欢迎指出,共同学习,共同进步。

最近在用hexo 和 github page搭 个人博客,地址如下:
http://www.jingfengji.tech/
欢迎大家关注。

最近的一些博客 还是会更新在 CSDN这边,后续以自己个人的博客站点会主。