工具设计目的
NGUI中图集默认使用”Unlit/Transparent Colored” Shader来创建材质。这样的话需要一张RGBA32的带透明通道的贴图,一张515x512的图占用空间1M,加载到内存后变成2M。在手机内存还是比较宝贵的时代这个是不大能接受的。一般项目的做法是分隔成两张图一张color图包含RGB通道,一张alpha图包含A通道。通过shader来组合长RGBA的图,来实现接近于RGBA的效果。通常Android使用ETC格式,IOS使用PVRTC格式。一张ETC格式的512x512的图占用128kb,两张一起256kb。这样相对于RGBA32格式的只占用其四分之一的空间。
NGUI自带的图集工具并不支持打ETC通道分离。通常一般做法采用第三方工具TexturePacker来做这个,但是使用第三方工具来做这个也会比较麻烦。所以通过修改NGUI打图集工具来实现这个通道分离。
图集增加通道分离
通道分离的做法是生成一张完整RGBA32的图片,然后读取RGBA分别生成两张和RGBA32一样大小的RGB图片和Alpha图片。最后替换shader,生成两张图片合成的材质 。代码如下: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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
using com.geargames.common.utils;
public class UIAtlasChangeShaderTool : EditorWindow
{
private bool _mSearched;
private Object[] mObjects;
private Vector2 mScroll = Vector2.zero;
public static bool useAlpha = true;
const string defShader = "Unlit/Transparent Colored";
const string newShader = "这里填合成两张图的shader名字";
private readonly string[] atlasPaths = { "这里填图集所在路径"};
[MenuItem("Tool/Open UIAtlas Shader Change Window")]
public static void OpenWindow()
{
UIAtlasChangeShaderTool window = (UIAtlasChangeShaderTool)EditorWindow.GetWindow(typeof(UIAtlasChangeShaderTool));
window.Show();
}
void OnGUI()
{
if (mObjects != null && mObjects.Length != 0)
{
mScroll = GUILayout.BeginScrollView(mScroll);
foreach (Object o in mObjects)
{
DrawUIAtalsObject(o);
}
GUILayout.EndScrollView();
}
GUILayout.Space(6f);
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
bool search = GUILayout.Button("Show All", "LargeButton", GUILayout.Width(120f));
bool Normal = GUILayout.Button("Normal All", "LargeButton", GUILayout.Width(120f));
bool Etc = GUILayout.Button("Etc All", "LargeButton", GUILayout.Width(120f));
bool CreateTexture = GUILayout.Button("CreateTexture All", "LargeButton", GUILayout.Width(200f));
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
if (search) Search(typeof(UIAtlas));
if (mObjects == null || mObjects.Length == 0) return;
if (Normal) foreach (Object o in mObjects) _ChangeShaderToNormal(o as UIAtlas);
if (Etc) foreach (Object o in mObjects) _ChangeShaderToEtc(o as UIAtlas);
if (CreateTexture)foreach (Object o in mObjects) SeperateRGBAandlphaChannel((o as UIAtlas).spriteMaterial.mainTexture as Texture2D);
}
private void DrawUIAtalsObject(Object obj)
{
if (obj == null) return;
Component comp = obj as Component;
UIAtlas at = obj as UIAtlas;
GUILayout.BeginHorizontal();
{
string path = AssetDatabase.GetAssetPath(obj);
if (string.IsNullOrEmpty(path))
{
path = "[Embedded]";
GUI.contentColor = new Color(0.7f, 0.7f, 0.7f);
}
else if (comp != null && EditorUtility.IsPersistent(comp.gameObject))
GUI.contentColor = new Color(0.6f, 0.8f, 1f);
GUILayout.Label(obj.name, "TextArea", GUILayout.Width(160f), GUILayout.Height(20f));
GUILayout.Label(path.Replace("Assets/", ""), "TextArea", GUILayout.Width(300f), GUILayout.Height(20f));
GUILayout.Label(at.spriteMaterial.shader.name, "TextArea", GUILayout.Width(200f), GUILayout.Height(20f));
GUI.contentColor = Color.white;
if (GUILayout.Button("Normal", "ButtonLeft", GUILayout.Width(60f), GUILayout.Height(16f)))
{
_ChangeShaderToNormal(at);
AssetDatabase.Refresh();
}
if (GUILayout.Button("Etc", "ButtonLeft", GUILayout.Width(60f), GUILayout.Height(16f)))
{
_ChangeShaderToEtc(at);
AssetDatabase.Refresh();
}
if (GUILayout.Button("CreateTexture", "ButtonLeft", GUILayout.Width(100f), GUILayout.Height(16f)))
{
string assetPath = AssetDatabase.GetAssetPath(at).Replace(".prefab",".png") ;
Texture2D texture = AssetDatabase.LoadAssetAtPath(assetPath,typeof(Texture2D)) as Texture2D;
SeperateRGBAandlphaChannel(at.texture as Texture2D);
AssetDatabase.Refresh();
}
}
GUILayout.EndHorizontal();
}
protected void Search(System.Type mType)
{
_mSearched = true;
List<string> pathList = new List<string>();
_GetAllAtlasPath(pathList);
bool isComponent = mType.IsSubclassOf(typeof(Component));
List<Object> list = new List<Object>();
if (mObjects != null)
{
for (int i = 0; i < mObjects.Length; ++i)
if (mObjects[i] != null)
list.Add(mObjects[i]);
}
string path = "";
string[] paths = pathList.ToArray();
for (int i = 0; i < paths.Length; ++i)
{
path = paths[i];
EditorUtility.DisplayProgressBar("Loading", "Searching assets, please wait...", (float)i / paths.Length);
Object obj = AssetDatabase.LoadMainAssetAtPath(path);
if (obj == null || list.Contains(obj)) continue;
if (!isComponent)
{
System.Type t = obj.GetType();
if (t == mType || t.IsSubclassOf(mType) && !list.Contains(obj))
list.Add(obj);
}
else if (PrefabUtility.GetPrefabType(obj) == PrefabType.Prefab)
{
Object t = (obj as GameObject).GetComponent(mType);
if (t != null && !list.Contains(t)) list.Add(t);
}
}
list.Sort(delegate (Object a, Object b) { return a.name.CompareTo(b.name); });
mObjects = list.ToArray();
EditorUtility.ClearProgressBar();
}
private void _GetAllAtlasPath(List<string> pathList)
{
for (int i = 0; i < atlasPaths.Length; i++)
{
FileUtils.GetAllFiles(Application.dataPath + atlasPaths[i], pathList);
}
for (int i = 0; i < pathList.Count; i++)
{
pathList[i] = pathList[i].Replace("\\", "/").Replace(Application.dataPath, "Assets");
}
}
static string c_flg = "_C";
static string a_flg = "_A";
static string exp_flg = ".png";
static private void _ChangeShaderToNormal(UIAtlas obj)
{
if (obj.spriteMaterial.shader.name != defShader)
{
obj.spriteMaterial.shader = Shader.Find(defShader);
string path = AssetDatabase.GetAssetPath(obj.spriteMaterial.mainTexture);
string p = path.Substring(0, path.IndexOf(c_flg + "."))+ ".png";
obj.spriteMaterial.mainTexture = AssetDatabase.LoadAssetAtPath(p , typeof(Texture2D)) as Texture2D;
AssetDatabase.SaveAssets();
}
}
static private void _ChangeShaderToEtc(UIAtlas obj)
{
if (obj.spriteMaterial.shader.name != newShader)
{
obj.spriteMaterial.shader = Shader.Find(newShader);
string path = AssetDatabase.GetAssetPath(obj.spriteMaterial.mainTexture);
string p = path.Substring(0, path.IndexOf("."));
string cpath = p + c_flg + exp_flg;
string apath = p + a_flg + exp_flg;
obj.spriteMaterial.mainTexture = AssetDatabase.LoadAssetAtPath(cpath, typeof(Texture2D)) as Texture2D;
obj.spriteMaterial.SetTexture("_MaskTex", AssetDatabase.LoadAssetAtPath(apath, typeof(Texture2D)) as Texture2D);
Debug.Log("path = " + cpath + " " + apath);
AssetDatabase.SaveAssets();
}
}
static void SeperateRGBAandlphaChannel(Texture2D sourcetex)
{
string path = AssetDatabase.GetAssetPath(sourcetex);
AssetDatabase.ImportAsset(path);
string p = path.Substring(0, path.IndexOf("."));
string cpath = p + c_flg + exp_flg;
string apath = p + a_flg + exp_flg;
AssetDatabase.DeleteAsset(cpath);
AssetDatabase.DeleteAsset(apath);
TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter;
MakeTextureReadable(ti, path, false);
Color[] sc = sourcetex.GetPixels();
int sw = sourcetex.width;
int sh = sourcetex.height;
Texture2D rgbTex = new Texture2D(sw, sh, TextureFormat.RGB24, false);
Texture2D alphaTex = new Texture2D(sw, sh, TextureFormat.RGB24, false);
Color[] ac = new Color[sw * sh];
for (int i = 0; i <sw; i++)
{
for (int j = 0; j < sh; j++)
{
int ind = j * sw + i;
Color color = sc[ind];
color.r = color.a;
color.g = color.a;
color.b = color.a;
ac[ind] = color;
}
}
rgbTex.SetPixels(sc);
rgbTex.Apply();
alphaTex.SetPixels(ac);
alphaTex.Apply();
byte[] bytes = rgbTex.EncodeToPNG();
File.WriteAllBytes(cpath, bytes);
bytes = alphaTex.EncodeToPNG();
File.WriteAllBytes(apath, bytes);
AssetDatabase.ImportAsset(cpath);
AssetDatabase.ImportAsset(apath);
TextureImporter tic = AssetImporter.GetAtPath(cpath) as TextureImporter;
TextureImporter tia = AssetImporter.GetAtPath(apath) as TextureImporter;
SetTextureFormat(tic, tia);
MakeTextureAnAtlas(ti, path, false, true);
MakeTextureAnAtlas(tic, cpath, false, false);
MakeTextureAnAtlas(tia, apath, false, true);
}
static public bool MakeTextureReadable(TextureImporter ti, string path, bool force)
{
TextureImporterSettings settings = new TextureImporterSettings();
ti.ReadTextureSettings(settings);
if (force || !settings.readable || settings.npotScale != TextureImporterNPOTScale.None || settings.alphaIsTransparency)
{
settings.readable = true;
if (NGUISettings.trueColorAtlas) settings.textureFormat = TextureImporterFormat.AutomaticTruecolor;
settings.npotScale = TextureImporterNPOTScale.None;
settings.alphaIsTransparency = false;
ti.SetTextureSettings(settings);
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
}
return true;
}
static bool MakeTextureAnAtlas(TextureImporter ti, string path, bool force, bool alphaTransparency)
{
TextureImporterSettings settings = new TextureImporterSettings();
ti.ReadTextureSettings(settings);
if (force ||
settings.readable ||
settings.maxTextureSize < 4096 ||
settings.wrapMode != TextureWrapMode.Clamp ||
settings.npotScale != TextureImporterNPOTScale.ToNearest)
{
settings.readable = false;
settings.maxTextureSize = 4096;
settings.wrapMode = TextureWrapMode.Clamp;
settings.npotScale = TextureImporterNPOTScale.ToNearest;
if (NGUISettings.trueColorAtlas)
{
settings.textureFormat = TextureImporterFormat.ARGB32;
settings.filterMode = FilterMode.Trilinear;
}
settings.aniso = 4;
settings.alphaIsTransparency = alphaTransparency;
ti.SetTextureSettings(settings);
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
}
return true;
}
static public void SetTextureFormat(TextureImporter tic, TextureImporter tia)
{
tic.alphaIsTransparency = false;
tic.isReadable = tia.isReadable = false;
tic.mipmapEnabled = tia.mipmapEnabled = false;
// TextureImporterPlatformSettings textureImporterPlatformSettings = tic.
// UnityEditor.TextureImporter.SetPlatformTextureSettings();
TextureImporterPlatformSettings tips = new TextureImporterPlatformSettings();
tips.name = "Android";
tips.overridden = true;
tips.maxTextureSize = 2048;
tips.format = TextureImporterFormat.ETC_RGB4;
tips.textureCompression = (int)UnityEditor.TextureCompressionQuality.Fast;
tic.SetPlatformTextureSettings(tips);
tia.SetPlatformTextureSettings(tips);
tips.overridden = true;
tips.name = "iPhone";
tips.format = TextureImporterFormat.PVRTC_RGB4;
tic.SetPlatformTextureSettings(tips);
tia.SetPlatformTextureSettings(tips);
#if UNITY_ANDROID || UNITY_STANDALONE
tips.name = "Standalone";
tips.format = TextureImporterFormat.ETC_RGB4;
tips.overridden = true;
tic.SetPlatformTextureSettings(tips);
tia.SetPlatformTextureSettings(tips);
// tic.SetPlatformTextureSettings("Standalone", 1024, TextureImporterFormat.ETC_RGB4, (int)TextureCompressionQuality.Fast);
// tia.SetPlatformTextureSettings("Standalone", 1024, TextureImporterFormat.ETC_RGB4, (int)TextureCompressionQuality.Fast);
// tic.textureFormat = TextureImporterFormat.ETC_RGB4;
// tia.textureFormat = TextureImporterFormat.ETC_RGB4;
#else
tips.name = "Standalone";
tips.format = TextureImporterFormat.PVRTC_RGB4;
tips.overridden = true;
tic.SetPlatformTextureSettings(tips);
tia.SetPlatformTextureSettings(tips);
// tic.SetPlatformTextureSettings("Standalone", 1024, TextureImporterFormat.PVRTC_RGB4, (int)TextureCompressionQuality.Fast);
// tia.SetPlatformTextureSettings("Standalone", 1024, TextureImporterFormat.PVRTC_RGB4, (int)TextureCompressionQuality.Fast);
// tic.textureFormat = TextureImporterFormat.PVRTC_RGB4;
// tia.textureFormat = TextureImporterFormat.PVRTC_RGB4;
#endif
}
public static bool flg = false;
static public void OnInspectorGUI(UIAtlas mLastAtlas)
{
GUILayout.BeginHorizontal();
UIAtlasChangeShaderTool.flg = EditorGUILayout.Toggle("spaceAlpha", UIAtlasChangeShaderTool.flg, GUILayout.Width(100f));
GUILayout.Label("分隔color,Alpha两通道");
GUILayout.EndHorizontal();
}
static public void updateAtlas(UIAtlas mLastAtlas)
{
updateAtlas(mLastAtlas, UIAtlasChangeShaderTool.flg);
}
static public void updateAtlas(UIAtlas mLastAtlas, bool flg)
{
if (flg)
{
SeperateRGBAandlphaChannel(mLastAtlas.texture as Texture2D);
_ChangeShaderToEtc(mLastAtlas);
}
else
{
_ChangeShaderToNormal(mLastAtlas);
}
AssetDatabase.Refresh();
}
}
注意,我这用的是Unity2018.3,比较早期的版本在图集格式设置那块需要修改 。就是SetPlatformTextureSettings这个方法。
NGUI中打图集的工具写在UIAtlasMaker.cs里面。我们需要在系统打图集的选项里面增加一个选择打分离通道的。如下:
UIAtlasMaker.cs需要增加代码位置为:1
2
3
4void OnEnable () { instance = this;
//默认勾选上
UIAtlasChangeShaderTool.flg = true;
}
1 | void OnGUI () |