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
|
//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2013 Tasharen Entertainment
//----------------------------------------------
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
/// <summary>
/// Inspector class used to edit the UIAtlas.
/// </summary>
[CustomEditor(typeof(UIAtlas))]
public class UIAtlasInspector : Editor
{
static public UIAtlasInspector instance;
enum AtlasType
{
Normal,
Reference,
}
UIAtlas mAtlas;
AtlasType mType = AtlasType.Normal;
UIAtlas mReplacement = null;
void OnEnable () { instance = this; }
void OnDisable () { instance = null; }
/// <summary>
/// Convenience function -- mark all widgets using the sprite as changed.
/// </summary>
void MarkSpriteAsDirty ()
{
UISpriteData sprite = (mAtlas != null) ? mAtlas.GetSprite(NGUISettings.selectedSprite) : null;
if (sprite == null) return;
UISprite[] sprites = NGUITools.FindActive<UISprite>();
foreach (UISprite sp in sprites)
{
if (UIAtlas.CheckIfRelated(sp.atlas, mAtlas) && sp.spriteName == sprite.name)
{
UIAtlas atl = sp.atlas;
sp.atlas = null;
sp.atlas = atl;
EditorUtility.SetDirty(sp);
}
}
UILabel[] labels = NGUITools.FindActive<UILabel>();
foreach (UILabel lbl in labels)
{
if (lbl.bitmapFont != null && UIAtlas.CheckIfRelated(lbl.bitmapFont.atlas, mAtlas) && lbl.bitmapFont.UsesSprite(sprite.name))
{
UIFont font = lbl.bitmapFont;
lbl.bitmapFont = null;
lbl.bitmapFont = font;
EditorUtility.SetDirty(lbl);
}
}
}
/// <summary>
/// Replacement atlas selection callback.
/// </summary>
void OnSelectAtlas (Object obj)
{
if (mReplacement != obj)
{
// Undo doesn't work correctly in this case... so I won't bother.
//NGUIEditorTools.RegisterUndo("Atlas Change");
//NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
mAtlas.replacement = obj as UIAtlas;
mReplacement = mAtlas.replacement;
UnityEditor.EditorUtility.SetDirty(mAtlas);
if (mReplacement == null) mType = AtlasType.Normal;
}
}
/// <summary>
/// Draw the inspector widget.
/// </summary>
public override void OnInspectorGUI ()
{
NGUIEditorTools.SetLabelWidth(80f);
mAtlas = target as UIAtlas;
UISpriteData sprite = (mAtlas != null) ? mAtlas.GetSprite(NGUISettings.selectedSprite) : null;
GUILayout.Space(6f);
if (mAtlas.replacement != null)
{
mType = AtlasType.Reference;
mReplacement = mAtlas.replacement;
}
GUILayout.BeginHorizontal();
AtlasType after = (AtlasType)EditorGUILayout.EnumPopup("Atlas Type", mType);
GUILayout.Space(18f);
GUILayout.EndHorizontal();
if (mType != after)
{
if (after == AtlasType.Normal)
{
mType = AtlasType.Normal;
OnSelectAtlas(null);
}
else
{
mType = AtlasType.Reference;
}
}
if (mType == AtlasType.Reference)
{
ComponentSelector.Draw<UIAtlas>(mAtlas.replacement, OnSelectAtlas, true);
GUILayout.Space(6f);
EditorGUILayout.HelpBox("You can have one atlas simply point to " +
"another one. This is useful if you want to be " +
"able to quickly replace the contents of one " +
"atlas with another one, for example for " +
"swapping an SD atlas with an HD one, or " +
"replacing an English atlas with a Chinese " +
"one. All the sprites referencing this atlas " +
"will update their references to the new one.", MessageType.Info);
if (mReplacement != mAtlas && mAtlas.replacement != mReplacement)
{
NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
mAtlas.replacement = mReplacement;
UnityEditor.EditorUtility.SetDirty(mAtlas);
}
return;
}
//GUILayout.Space(6f);
Material mat = EditorGUILayout.ObjectField("Material", mAtlas.spriteMaterial, typeof(Material), false) as Material;
if (mAtlas.spriteMaterial != mat)
{
NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
mAtlas.spriteMaterial = mat;
// Ensure that this atlas has valid import settings
if (mAtlas.texture != null) NGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha);
mAtlas.MarkAsChanged();
}
if (mat != null)
{
TextAsset ta = EditorGUILayout.ObjectField("TP Import", null, typeof(TextAsset), false) as TextAsset;
if (ta != null)
{
// Ensure that this atlas has valid import settings
if (mAtlas.texture != null) NGUIEditorTools.ImportTexture(mAtlas.texture, false, false, !mAtlas.premultipliedAlpha);
NGUIEditorTools.RegisterUndo("Import Sprites", mAtlas);
NGUIJson.LoadSpriteData(mAtlas, ta);
if (sprite != null) sprite = mAtlas.GetSprite(sprite.name);
mAtlas.MarkAsChanged();
}
float pixelSize = EditorGUILayout.FloatField("Pixel Size", mAtlas.pixelSize, GUILayout.Width(120f));
if (pixelSize != mAtlas.pixelSize)
{
NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
mAtlas.pixelSize = pixelSize;
}
}
if (mAtlas.spriteMaterial != null)
{
Color blue = new Color(0f, 0.7f, 1f, 1f);
Color green = new Color(0.4f, 1f, 0f, 1f);
if (sprite == null && mAtlas.spriteList.Count > 0)
{
string spriteName = NGUISettings.selectedSprite;
if (!string.IsNullOrEmpty(spriteName)) sprite = mAtlas.GetSprite(spriteName);
if (sprite == null) sprite = mAtlas.spriteList[0];
}
if (sprite != null)
{
if (sprite == null) return;
Texture2D tex = mAtlas.spriteMaterial.mainTexture as Texture2D;
if (tex != null)
{
if (!NGUIEditorTools.DrawHeader("Sprite Details")) return;
NGUIEditorTools.BeginContents();
GUILayout.Space(3f);
NGUIEditorTools.DrawAdvancedSpriteField(mAtlas, sprite.name, SelectSprite, true);
GUILayout.Space(6f);
GUI.changed = false;
GUI.backgroundColor = green;
NGUIEditorTools.IntVector sizeA = NGUIEditorTools.IntPair("Dimensions", "X", "Y", sprite.x, sprite.y);
NGUIEditorTools.IntVector sizeB = NGUIEditorTools.IntPair(null, "Width", "Height", sprite.width, sprite.height);
EditorGUILayout.Separator();
GUI.backgroundColor = blue;
NGUIEditorTools.IntVector borderA = NGUIEditorTools.IntPair("Border", "Left", "Right", sprite.borderLeft, sprite.borderRight);
NGUIEditorTools.IntVector borderB = NGUIEditorTools.IntPair(null, "Bottom", "Top", sprite.borderBottom, sprite.borderTop);
EditorGUILayout.Separator();
GUI.backgroundColor = Color.white;
NGUIEditorTools.IntVector padA = NGUIEditorTools.IntPair("Padding", "Left", "Right", sprite.paddingLeft, sprite.paddingRight);
NGUIEditorTools.IntVector padB = NGUIEditorTools.IntPair(null, "Bottom", "Top", sprite.paddingBottom, sprite.paddingTop);
if (GUI.changed)
{
NGUIEditorTools.RegisterUndo("Atlas Change", mAtlas);
sprite.x = sizeA.x;
sprite.y = sizeA.y;
sprite.width = sizeB.x;
sprite.height = sizeB.y;
sprite.paddingLeft = padA.x;
sprite.paddingRight = padA.y;
sprite.paddingBottom = padB.x;
sprite.paddingTop = padB.y;
sprite.borderLeft = borderA.x;
sprite.borderRight = borderA.y;
sprite.borderBottom = borderB.x;
sprite.borderTop = borderB.y;
MarkSpriteAsDirty();
}
if (sprite != null && GUILayout.Button("Extract Sprite"))
{
string path = EditorUtility.SaveFilePanelInProject("Save As", sprite.name + ".png", "png", "Extract sprite into which file?");
if (!string.IsNullOrEmpty(path))
{
UIAtlasMaker.SpriteEntry se = UIAtlasMaker.ExtractSprite(mAtlas, sprite.name);
if (se != null)
{
byte[] bytes = se.tex.EncodeToPNG();
File.WriteAllBytes(path, bytes);
AssetDatabase.ImportAsset(path);
}
if (se.temporaryTexture) DestroyImmediate(se.tex);
}
}
NGUIEditorTools.EndContents();
}
if (NGUIEditorTools.previousSelection != null)
{
GUI.backgroundColor = Color.green;
if (GUILayout.Button("<< Return to " + NGUIEditorTools.previousSelection.name))
{
NGUIEditorTools.SelectPrevious();
}
GUI.backgroundColor = Color.white;
}
}
}
}
/// <summary>
/// Sprite selection callback.
/// </summary>
void SelectSprite (string spriteName)
{
NGUISettings.selectedSprite = spriteName;
Repaint();
}
/// <summary>
/// All widgets have a preview.
/// </summary>
public override bool HasPreviewGUI () { return true; }
/// <summary>
/// Draw the sprite preview.
/// </summary>
public override void OnPreviewGUI (Rect rect, GUIStyle background)
{
UISpriteData sprite = (mAtlas != null) ? mAtlas.GetSprite(NGUISettings.selectedSprite) : null;
if (sprite == null) return;
Texture2D tex = mAtlas.texture as Texture2D;
if (tex != null) NGUIEditorTools.DrawSprite(tex, rect, sprite, Color.white);
}
}
|