Subversion Repository Public Repository

Nextrek

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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//----------------------------------------------
//            NGUI: Next-Gen UI kit
// Copyright © 2011-2013 Tasharen Entertainment
//----------------------------------------------

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

/// <summary>
/// Unity doesn't keep the values of static variables after scripts change get recompiled. One way around this
/// is to store the references in EditorPrefs -- retrieve them at start, and save them whenever something changes.
/// </summary>

public class NGUISettings
{
	public enum ColorMode
	{
		Orange,
		Green,
		Blue,
	}

#region Generic Get and Set methods
	/// <summary>
	/// Save the specified boolean value in settings.
	/// </summary>

	static public void SetBool (string name, bool val) { EditorPrefs.SetBool(name, val); }

	/// <summary>
	/// Save the specified integer value in settings.
	/// </summary>

	static public void SetInt (string name, int val) { EditorPrefs.SetInt(name, val); }

	/// <summary>
	/// Save the specified float value in settings.
	/// </summary>

	static public void SetFloat (string name, float val) { EditorPrefs.SetFloat(name, val); }

	/// <summary>
	/// Save the specified string value in settings.
	/// </summary>

	static public void SetString (string name, string val) { EditorPrefs.SetString(name, val); }

	/// <summary>
	/// Save the specified color value in settings.
	/// </summary>

	static public void SetColor (string name, Color c) { SetString(name, c.r + " " + c.g + " " + c.b + " " + c.a); }

	/// <summary>
	/// Save the specified enum value to settings.
	/// </summary>

	static public void SetEnum (string name, System.Enum val) { SetString(name, val.ToString()); }

	/// <summary>
	/// Save the specified object in settings.
	/// </summary>

	static public void Set (string name, Object obj)
	{
		if (obj == null)
		{
			EditorPrefs.DeleteKey(name);
		}
		else
		{
			if (obj != null)
			{
				string path = AssetDatabase.GetAssetPath(obj);

				if (!string.IsNullOrEmpty(path))
				{
					EditorPrefs.SetString(name, path);
				}
				else
				{
					EditorPrefs.SetString(name, obj.GetInstanceID().ToString());
				}
			}
			else EditorPrefs.DeleteKey(name);
		}
	}

	/// <summary>
	/// Get the previously saved boolean value.
	/// </summary>

	static public bool GetBool (string name, bool defaultValue) { return EditorPrefs.GetBool(name, defaultValue); }

	/// <summary>
	/// Get the previously saved integer value.
	/// </summary>

	static public int GetInt (string name, int defaultValue) { return EditorPrefs.GetInt(name, defaultValue); }

	/// <summary>
	/// Get the previously saved float value.
	/// </summary>

	static public float GetFloat (string name, float defaultValue) { return EditorPrefs.GetFloat(name, defaultValue); }

	/// <summary>
	/// Get the previously saved string value.
	/// </summary>

	static public string GetString (string name, string defaultValue) { return EditorPrefs.GetString(name, defaultValue); }
	
	/// <summary>
	/// Get a previously saved color value.
	/// </summary>

	static public Color GetColor (string name, Color c)
	{
		string strVal = GetString(name, c.r + " " + c.g + " " + c.b + " " + c.a);
		string[] parts = strVal.Split(' ');

		if (parts.Length == 4)
		{
			float.TryParse(parts[0], out c.r);
			float.TryParse(parts[1], out c.g);
			float.TryParse(parts[2], out c.b);
			float.TryParse(parts[3], out c.a);
		}
		return c;
	}

	/// <summary>
	/// Get a previously saved enum from settings.
	/// </summary>

	static public T GetEnum<T> (string name, T defaultValue)
	{
		string val = GetString(name, defaultValue.ToString());
		string[] names = System.Enum.GetNames(typeof(T));
		System.Array values = System.Enum.GetValues(typeof(T));
		
		for (int i = 0; i < names.Length; ++i)
		{
			if (names[i] == val)
				return (T)values.GetValue(i);
		}
		return defaultValue;
	}

	/// <summary>
	/// Get a previously saved object from settings.
	/// </summary>

	static public T Get<T> (string name, T defaultValue) where T : Object
	{
		string path = EditorPrefs.GetString(name);
		if (string.IsNullOrEmpty(path)) return null;
		
		T retVal = NGUIEditorTools.LoadAsset<T>(path);
		
		if (retVal == null)
		{
			int id;
			if (int.TryParse(path, out id))
				return EditorUtility.InstanceIDToObject(id) as T;
		}
		return retVal;
	}
#endregion

#region Convenience accessor properties

	static public Color color
	{
		get { return GetColor("NGUI Color", Color.white); }
		set { SetColor("NGUI Color", value); }
	}

	static public ColorMode colorMode
	{
		get { return GetEnum("NGUI Color Mode", ColorMode.Blue); }
		set { SetEnum("NGUI Color Mode", value); }
	}

	static public Object ambigiousFont
	{
		get
		{
			Font fnt = Get<Font>("NGUI Dynamic Font", null);
			if (fnt != null) return fnt;
			return Get<UIFont>("NGUI Bitmap Font", null);
		}
		set
		{
			if (value == null)
			{
				Set("NGUI Bitmap Font", null);
				Set("NGUI Dynamic Font", null);
			}
			else if (value is Font)
			{
				Set("NGUI Bitmap Font", null);
				Set("NGUI Dynamic Font", value as Font);
			}
			else if (value is UIFont)
			{
				Set("NGUI Bitmap Font", value as UIFont);
				Set("NGUI Dynamic Font", null);
			}
		}
	}

	static public UIAtlas atlas
	{
		get { return Get<UIAtlas>("NGUI Atlas", null); }
		set { Set("NGUI Atlas", value); }
	}

	static public Texture texture
	{
		get { return Get<Texture>("NGUI Texture", null); }
		set { Set("NGUI Texture", value); }
	}

#if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1 && !UNITY_4_2
	static public Sprite sprite2D
	{
		get { return Get<Sprite>("NGUI Sprite2D", null); }
		set { Set("NGUI Sprite2D", value); }
	}
#endif

	static public string selectedSprite
	{
		get { return GetString("NGUI Sprite", null); }
		set { SetString("NGUI Sprite", value); }
	}

	static public UIWidget.Pivot pivot
	{
		get { return GetEnum("NGUI Pivot", UIWidget.Pivot.Center); }
		set { SetEnum("NGUI Pivot", value); }
	}

	static public int layer
	{
		get
		{
			int layer = GetInt("NGUI Layer", -1);
			if (layer == -1) layer = LayerMask.NameToLayer("UI");
			if (layer == -1) layer = LayerMask.NameToLayer("2D UI");
			return (layer == -1) ? 9 : layer;
		}
		set
		{
			SetInt("NGUI Layer", value);
		}
	}

	static public TextAsset fontData
	{
		get { return Get<TextAsset>("NGUI Font Data", null); }
		set { Set("NGUI Font Data", value); }
	}

	static public Texture2D fontTexture
	{
		get { return Get<Texture2D>("NGUI Font Texture", null); }
		set { Set("NGUI Font Texture", value); }
	}

	static public int fontSize
	{
		get { return GetInt("NGUI Font Size", 16); }
		set { SetInt("NGUI Font Size", value); }
	}

	static public FontStyle fontStyle
	{
		get { return GetEnum("NGUI Font Style", FontStyle.Normal); }
		set { SetEnum("NGUI Font Style", value); }
	}

	static public UILabel.Overflow overflowStyle
	{
		get { return GetEnum("NGUI Overflow", UILabel.Overflow.ShrinkContent); }
		set { SetEnum("NGUI Overflow", value); }
	}

	static public string partialSprite
	{
		get { return GetString("NGUI Partial", null); }
		set { SetString("NGUI Partial", value); }
	}

	static public int atlasPadding
	{
		get { return GetInt("NGUI Padding", 1); }
		set { SetInt("NGUI Padding", value); }
	}

	static public bool atlasTrimming
	{
		get { return GetBool("NGUI Trim", true); }
		set { SetBool("NGUI Trim", value); }
	}

	static public bool atlasPMA
	{
		get { return GetBool("NGUI PMA", false); }
		set { SetBool("NGUI PMA", value); }
	}

	static public bool unityPacking
	{
		get { return GetBool("NGUI Packing", true); }
		set { SetBool("NGUI Packing", value); }
	}

	static public bool forceSquareAtlas
	{
		get { return GetBool("NGUI Square", false); }
		set { SetBool("NGUI Square", value); }
	}

	static public bool allow4096
	{
		get { return GetBool("NGUI 4096", true); }
		set { SetBool("NGUI 4096", value); }
	}

	static public bool showAllDCs
	{
		get { return GetBool("NGUI DCs", true); }
		set { SetBool("NGUI DCs", value); }
	}
#endregion

	/// <summary>
	/// Convenience method -- add a widget.
	/// </summary>

	static public UIWidget AddWidget (GameObject go)
	{
		UIWidget w = NGUITools.AddWidget<UIWidget>(go);
		w.name = "Container";
		w.pivot = pivot;
		w.width = 100;
		w.height = 100;
		return w;
	}

	/// <summary>
	/// Convenience method -- add a texture.
	/// </summary>

	static public UITexture AddTexture (GameObject go)
	{
		UITexture w = NGUITools.AddWidget<UITexture>(go);
		w.name = "Texture";
		w.pivot = pivot;
		w.mainTexture = texture;
		w.width = 100;
		w.height = 100;
		return w;
	}

#if !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1 && !UNITY_4_2
	/// <summary>
	/// Convenience method -- add a UnityEngine.Sprite.
	/// </summary>

	static public UI2DSprite Add2DSprite (GameObject go)
	{
		UI2DSprite w = NGUITools.AddWidget<UI2DSprite>(go);
		w.name = "2D Sprite";
		w.pivot = pivot;
		w.sprite2D = sprite2D;
		w.width = 100;
		w.height = 100;
		return w;
	}
#endif
	/// <summary>
	/// Convenience method -- add a sprite.
	/// </summary>

	static public UISprite AddSprite (GameObject go)
	{
		UISprite w = NGUITools.AddWidget<UISprite>(go);
		w.name = "Sprite";
		w.atlas = atlas;
		w.spriteName = selectedSprite;

		if (w.atlas != null && !string.IsNullOrEmpty(w.spriteName))
		{
			UISpriteData sp = w.atlas.GetSprite(w.spriteName);
			if (sp != null && sp.hasBorder)
				w.type = UISprite.Type.Sliced;
		}

		w.pivot = pivot;
		w.width = 100;
		w.height = 100;
		w.MakePixelPerfect();
		return w;
	}

	/// <summary>
	/// Convenience method -- add a label with default parameters.
	/// </summary>

	static public UILabel AddLabel (GameObject go)
	{
		UILabel w = NGUITools.AddWidget<UILabel>(go);
		w.name = "Label";
		w.ambigiousFont = ambigiousFont;
		w.text = "New Label";
		w.pivot = pivot;
		w.width = 120;
		w.height = Mathf.Max(20, GetInt("NGUI Font Height", 16));
		w.fontStyle = fontStyle;
		w.fontSize = fontSize;
		w.applyGradient = true;
		w.gradientBottom = new Color(0.7f, 0.7f, 0.7f);
		w.AssumeNaturalSize();
		return w;
	}

	/// <summary>
	/// Convenience method -- add a new panel.
	/// </summary>

	static public UIPanel AddPanel (GameObject go)
	{
		if (go == null) return null;
		int depth = UIPanel.nextUnusedDepth;
		UIPanel panel = NGUITools.AddChild<UIPanel>(go);
		panel.depth = depth;
		return panel;
	}

	/// <summary>
	/// Copy the specified widget's style.
	/// </summary>

	static public void CopyStyle (UIWidget widget)
	{
		SetColor("Widget Color", widget.color);
		SetEnum("Widget Pivot", widget.pivot);
		if (widget is UILabel) CopyLabelStyle(widget as UILabel);
	}

	/// <summary>
	/// Paste the specified widget's style.
	/// </summary>

	static public void PasteStyle (UIWidget widget)
	{
		widget.color = GetColor("Widget Color", widget.color);
		widget.pivot = GetEnum<UIWidget.Pivot>("Widget Pivot", widget.pivot);
		if (widget is UILabel) PasteLabelStyle(widget as UILabel);
	}

	/// <summary>
	/// Copy the specified label's style.
	/// </summary>

	static void CopyLabelStyle (UILabel lbl)
	{
		SetEnum("Overflow", lbl.overflowMethod);
		SetInt("SpacingX", lbl.spacingX);
		SetInt("SpacingY", lbl.spacingY);
		SetInt("MaxLines", lbl.maxLineCount);
		SetBool("Encoding", lbl.supportEncoding);
		SetBool("Gradient", lbl.applyGradient);
		SetColor("Gradient B", lbl.gradientBottom);
		SetColor("Gradient T", lbl.gradientTop);
		SetEnum("Effect", lbl.effectStyle);
		SetColor("Effect C", lbl.effectColor);
		SetFloat("Effect X", lbl.effectDistance.x);
		SetFloat("Effect Y", lbl.effectDistance.y);
	}

	/// <summary>
	/// Paste the specified label's style.
	/// </summary>

	static void PasteLabelStyle (UILabel lbl)
	{
		lbl.overflowMethod = GetEnum<UILabel.Overflow>("Overflow", lbl.overflowMethod);
		lbl.spacingX = GetInt("SpacingX", lbl.spacingX);
		lbl.spacingY = GetInt("SpacingY", lbl.spacingY);
		lbl.maxLineCount = GetInt("MaxLines", lbl.maxLineCount);
		lbl.supportEncoding = GetBool("Encoding", lbl.supportEncoding);
		lbl.applyGradient = GetBool("Gradient", lbl.applyGradient);
		lbl.gradientBottom = GetColor("Gradient B", lbl.gradientBottom);
		lbl.gradientTop = GetColor("Gradient T", lbl.gradientTop);
		lbl.effectStyle = GetEnum<UILabel.Effect>("Effect", lbl.effectStyle);
		lbl.effectColor = GetColor("Effect C", lbl.effectColor);

		float x = GetFloat("Effect X", lbl.effectDistance.x);
		float y = GetFloat("Effect Y", lbl.effectDistance.y);
		lbl.effectDistance = new Vector2(x, y);
	}
}

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/NGUI/Scripts/Editor/NGUISettings.cs

Diff revisions: vs.
Revision Author Commited Message
83 FMMortaroli picture FMMortaroli Tue 13 May, 2014 11:32:51 +0000