动态图集的意义

比如说MOBA类游戏,在进入对战场景时,我们需要选择英雄,配置技能,选择天赋和符文等,这些图标如果都事先打好了图集,在对战场景就会加载很多没有用的图标,而如果不打图集,就会增加很多drawcall,所以我们需要动态生成图集。

动态图集算法

GitHub - DaVikingCode/UnityRuntimeSpriteSheetsGenerator: Unity – generate SpriteSheets at runtime!

Unity也提供了算法Unity - Scripting API: Texture2D.PackTextures (unity3d.com)

我们使用GitHub的项目。

核心代码

AssetPacker

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
//传入单张图片的路径,带拓展名,以及单张图片的自定义ID		
public void AddTextureToPack(string file, string customID = null) {

itemsToRaster.Add(new TextureToPack(file, customID != null ? customID : Path.GetFileNameWithoutExtension(file)));
}

public void AddTexturesToPack(string[] files) {

foreach (string file in files)
AddTextureToPack(file);
}

public void Process(bool allow4096Textures = false) {

this.allow4096Textures = allow4096Textures;//默认创建的是2048,是否使用4096

if (useCache) {//是否要缓存到本地

if (cacheName == "")
throw new Exception("No cache name specified");

string path = Application.persistentDataPath + "/AssetPacker/" + cacheName + "/" + cacheVersion + "/";

bool cacheExist = Directory.Exists(path);

if (!cacheExist)
StartCoroutine(createPack(path));
else
StartCoroutine(loadPack(path));

} else
StartCoroutine(createPack());

}
protected IEnumerator createPack(string savePath = "") {

if (savePath != "") {
//创建保存路径,或者将之前的路径清理掉
}
List<Texture2D> textures = new List<Texture2D>();//缓存所有指定路径下需要被打入合集的图片
List<string> images = new List<string>();//缓存所有指定图片的ID
foreach (TextureToPack itemToRaster in itemsToRaster) {

WWW loader = new WWW("file:///" + itemToRaster.file);

yield return loader;

textures.Add(loader.texture);
images.Add(itemToRaster.id);
}
int textureSize = allow4096Textures ? 4096 : 2048;//确定图集大小
//创建每个被打入合集的图片的Rect信息...
List<Rect> rectangles = new List<Rect>();
//...
while (rectangles.Count > 0) {

Texture2D texture = new Texture2D(textureSize, textureSize, TextureFormat.ARGB32, false);//整个图集
//清除图集的颜色...
RectanglePacker packer = new RectanglePacker(texture.width, texture.height, padding);//RectanglePacker对象,合并图集核心算法对象
//上面的rectangles的Rect信息添加进packer内并pack
if (packer.rectangleCount > 0) {//如果还有需要pack的rect
//准备好生成sprite的信息、以及最后要移除的对象的缓存列表
for (int j = 0; j < packer.rectangleCount; j++) {
//...
//使用下面的方法添加像素信息
texture.SetPixels32(rect.x, rect.y, rect.width, rect.height, textures[index].GetPixels32());
//生成TextureAsset信息,用于生成sprite
//生成garbage信息,用于清除无用的缓存
}
}
}
}

Texture2D.SetPixels32方法需要Texture2D是可读可写的。

如果最后打出的文件是红色的,说明传入需要打图集的单个图片时没有加入后缀。