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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
|
//----------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2013 Tasharen Entertainment
//----------------------------------------------
using UnityEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
/// <summary>
/// Helper class containing generic functions used throughout the UI library.
/// </summary>
static public class NGUITools
{
static AudioListener mListener;
static bool mLoaded = false;
static float mGlobalVolume = 1f;
/// <summary>
/// Globally accessible volume affecting all sounds played via NGUITools.PlaySound().
/// </summary>
static public float soundVolume
{
get
{
if (!mLoaded)
{
mLoaded = true;
mGlobalVolume = PlayerPrefs.GetFloat("Sound", 1f);
}
return mGlobalVolume;
}
set
{
if (mGlobalVolume != value)
{
mLoaded = true;
mGlobalVolume = value;
PlayerPrefs.SetFloat("Sound", value);
}
}
}
/// <summary>
/// Helper function -- whether the disk access is allowed.
/// </summary>
static public bool fileAccess
{
get
{
return Application.platform != RuntimePlatform.WindowsWebPlayer &&
Application.platform != RuntimePlatform.OSXWebPlayer;
}
}
/// <summary>
/// Play the specified audio clip.
/// </summary>
static public AudioSource PlaySound (AudioClip clip) { return PlaySound(clip, 1f, 1f); }
/// <summary>
/// Play the specified audio clip with the specified volume.
/// </summary>
static public AudioSource PlaySound (AudioClip clip, float volume) { return PlaySound(clip, volume, 1f); }
/// <summary>
/// Play the specified audio clip with the specified volume and pitch.
/// </summary>
static public AudioSource PlaySound (AudioClip clip, float volume, float pitch)
{
volume *= soundVolume;
if (clip != null && volume > 0.01f)
{
if (mListener == null || !NGUITools.IsActive(mListener))
{
mListener = GameObject.FindObjectOfType(typeof(AudioListener)) as AudioListener;
if (mListener == null)
{
Camera cam = Camera.main;
if (cam == null) cam = GameObject.FindObjectOfType(typeof(Camera)) as Camera;
if (cam != null) mListener = cam.gameObject.AddComponent<AudioListener>();
}
}
if (mListener != null && mListener.enabled && NGUITools.GetActive(mListener.gameObject))
{
AudioSource source = mListener.audio;
if (source == null) source = mListener.gameObject.AddComponent<AudioSource>();
source.pitch = pitch;
source.PlayOneShot(clip, volume);
return source;
}
}
return null;
}
/// <summary>
/// New WWW call can fail if the crossdomain policy doesn't check out. Exceptions suck. It's much more elegant to check for null instead.
/// </summary>
static public WWW OpenURL (string url)
{
#if UNITY_FLASH
Debug.LogError("WWW is not yet implemented in Flash");
return null;
#else
WWW www = null;
try { www = new WWW(url); }
catch (System.Exception ex) { Debug.LogError(ex.Message); }
return www;
#endif
}
/// <summary>
/// New WWW call can fail if the crossdomain policy doesn't check out. Exceptions suck. It's much more elegant to check for null instead.
/// </summary>
static public WWW OpenURL (string url, WWWForm form)
{
if (form == null) return OpenURL(url);
#if UNITY_FLASH
Debug.LogError("WWW is not yet implemented in Flash");
return null;
#else
WWW www = null;
try { www = new WWW(url, form); }
catch (System.Exception ex) { Debug.LogError(ex != null ? ex.Message : "<null>"); }
return www;
#endif
}
/// <summary>
/// Same as Random.Range, but the returned value is between min and max, inclusive.
/// Unity's Random.Range is less than max instead, unless min == max.
/// This means Range(0,1) produces 0 instead of 0 or 1. That's unacceptable.
/// </summary>
static public int RandomRange (int min, int max)
{
if (min == max) return min;
return UnityEngine.Random.Range(min, max + 1);
}
/// <summary>
/// Returns the hierarchy of the object in a human-readable format.
/// </summary>
static public string GetHierarchy (GameObject obj)
{
string path = obj.name;
while (obj.transform.parent != null)
{
obj = obj.transform.parent.gameObject;
path = obj.name + "\\" + path;
}
return path;
}
/// <summary>
/// Find all scene components, active or inactive.
/// </summary>
static public List<T> FindAll<T> () where T : Component
{
T[] comps = Resources.FindObjectsOfTypeAll(typeof(T)) as T[];
List<T> list = new List<T>();
foreach (T comp in comps)
{
if (comp.gameObject.hideFlags == 0)
list.Add(comp);
}
return list;
}
/// <summary>
/// Find all active objects of specified type.
/// </summary>
static public T[] FindActive<T> () where T : Component
{
#if UNITY_3_5 || UNITY_4_0
return GameObject.FindSceneObjectsOfType(typeof(T)) as T[];
#else
return GameObject.FindObjectsOfType(typeof(T)) as T[];
#endif
}
/// <summary>
/// Find the camera responsible for drawing the objects on the specified layer.
/// </summary>
static public Camera FindCameraForLayer (int layer)
{
int layerMask = 1 << layer;
Camera[] cameras = NGUITools.FindActive<Camera>();
for (int i = 0, imax = cameras.Length; i < imax; ++i)
{
Camera cam = cameras[i];
if ((cam.cullingMask & layerMask) != 0)
{
return cam;
}
}
return null;
}
/// <summary>
/// Add a collider to the game object containing one or more widgets.
/// </summary>
static public BoxCollider AddWidgetCollider (GameObject go) { return AddWidgetCollider(go, false); }
/// <summary>
/// Add a collider to the game object containing one or more widgets.
/// </summary>
static public BoxCollider AddWidgetCollider (GameObject go, bool considerInactive)
{
if (go != null)
{
Collider col = go.GetComponent<Collider>();
BoxCollider box = col as BoxCollider;
if (box == null)
{
if (col != null)
{
if (Application.isPlaying) GameObject.Destroy(col);
else GameObject.DestroyImmediate(col);
}
box = go.AddComponent<BoxCollider>();
box.isTrigger = true;
UIWidget widget = go.GetComponent<UIWidget>();
if (widget != null) widget.autoResizeBoxCollider = true;
}
UpdateWidgetCollider(box, considerInactive);
return box;
}
return null;
}
/// <summary>
/// Adjust the widget's collider based on the depth of the widgets, as well as the widget's dimensions.
/// </summary>
static public void UpdateWidgetCollider (GameObject go)
{
UpdateWidgetCollider(go, false);
}
/// <summary>
/// Adjust the widget's collider based on the depth of the widgets, as well as the widget's dimensions.
/// </summary>
static public void UpdateWidgetCollider (GameObject go, bool considerInactive)
{
if (go != null)
{
UpdateWidgetCollider(go.GetComponent<BoxCollider>(), considerInactive);
}
}
/// <summary>
/// Adjust the widget's collider based on the depth of the widgets, as well as the widget's dimensions.
/// </summary>
static public void UpdateWidgetCollider (BoxCollider bc)
{
UpdateWidgetCollider(bc, false);
}
/// <summary>
/// Adjust the widget's collider based on the depth of the widgets, as well as the widget's dimensions.
/// </summary>
static public void UpdateWidgetCollider (BoxCollider box, bool considerInactive)
{
if (box != null)
{
GameObject go = box.gameObject;
UIWidget w = go.GetComponent<UIWidget>();
if (w != null)
{
Vector4 region = w.drawingDimensions;
box.center = new Vector3((region.x + region.z) * 0.5f, (region.y + region.w) * 0.5f);
box.size = new Vector3(region.z - region.x, region.w - region.y);
}
else
{
Bounds b = NGUIMath.CalculateRelativeWidgetBounds(go.transform, considerInactive);
box.center = b.center;
box.size = new Vector3(b.size.x, b.size.y, 0f);
}
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(box);
#endif
}
}
/// <summary>
/// Helper function that returns the string name of the type.
/// </summary>
static public string GetTypeName<T> ()
{
string s = typeof(T).ToString();
if (s.StartsWith("UI")) s = s.Substring(2);
else if (s.StartsWith("UnityEngine.")) s = s.Substring(12);
return s;
}
/// <summary>
/// Helper function that returns the string name of the type.
/// </summary>
static public string GetTypeName (UnityEngine.Object obj)
{
if (obj == null) return "Null";
string s = obj.GetType().ToString();
if (s.StartsWith("UI")) s = s.Substring(2);
else if (s.StartsWith("UnityEngine.")) s = s.Substring(12);
return s;
}
/// <summary>
/// Convenience method that works without warnings in both Unity 3 and 4.
/// </summary>
static public void RegisterUndo (UnityEngine.Object obj, string name)
{
#if UNITY_EDITOR
#if UNITY_3_5 || UNITY_4_0 || UNITY_4_1 || UNITY_4_2
UnityEditor.Undo.RegisterUndo(obj, name);
#else
UnityEditor.Undo.RecordObject(obj, name);
#endif
UnityEditor.EditorUtility.SetDirty(obj);
#endif
}
/// <summary>
/// Add a new child game object.
/// </summary>
static public GameObject AddChild (GameObject parent) { return AddChild(parent, true); }
/// <summary>
/// Add a new child game object.
/// </summary>
static public GameObject AddChild (GameObject parent, bool undo)
{
GameObject go = new GameObject();
#if UNITY_EDITOR
if (undo) UnityEditor.Undo.RegisterCreatedObjectUndo(go, "Create Object");
#endif
if (parent != null)
{
Transform t = go.transform;
t.parent = parent.transform;
t.localPosition = Vector3.zero;
t.localRotation = Quaternion.identity;
t.localScale = Vector3.one;
go.layer = parent.layer;
}
return go;
}
/// <summary>
/// Instantiate an object and add it to the specified parent.
/// </summary>
static public GameObject AddChild (GameObject parent, GameObject prefab)
{
GameObject go = GameObject.Instantiate(prefab) as GameObject;
#if UNITY_EDITOR && !UNITY_3_5 && !UNITY_4_0 && !UNITY_4_1 && !UNITY_4_2
UnityEditor.Undo.RegisterCreatedObjectUndo(go, "Create Object");
#endif
if (go != null && parent != null)
{
Transform t = go.transform;
t.parent = parent.transform;
t.localPosition = Vector3.zero;
t.localRotation = Quaternion.identity;
t.localScale = Vector3.one;
go.layer = parent.layer;
}
return go;
}
/// <summary>
/// Calculate the game object's depth based on the widgets within, and also taking panel depth into consideration.
/// </summary>
static public int CalculateRaycastDepth (GameObject go)
{
UIWidget w = go.GetComponent<UIWidget>();
if (w != null) return w.raycastDepth;
UIWidget[] widgets = go.GetComponentsInChildren<UIWidget>();
if (widgets.Length == 0) return 0;
int depth = int.MaxValue;
for (int i = 0, imax = widgets.Length; i < imax; ++i)
{
if (widgets[i].enabled)
depth = Mathf.Min(depth, widgets[i].raycastDepth);
}
return depth;
}
/// <summary>
/// Gathers all widgets and calculates the depth for the next widget.
/// </summary>
static public int CalculateNextDepth (GameObject go)
{
int depth = -1;
UIWidget[] widgets = go.GetComponentsInChildren<UIWidget>();
for (int i = 0, imax = widgets.Length; i < imax; ++i)
depth = Mathf.Max(depth, widgets[i].depth);
return depth + 1;
}
/// <summary>
/// Gathers all widgets and calculates the depth for the next widget.
/// </summary>
static public int CalculateNextDepth (GameObject go, bool ignoreChildrenWithColliders)
{
if (ignoreChildrenWithColliders)
{
int depth = -1;
UIWidget[] widgets = go.GetComponentsInChildren<UIWidget>();
for (int i = 0, imax = widgets.Length; i < imax; ++i)
{
UIWidget w = widgets[i];
if (w.cachedGameObject != go && w.collider != null) continue;
depth = Mathf.Max(depth, w.depth);
}
return depth + 1;
}
return CalculateNextDepth(go);
}
/// <summary>
/// Adjust the widgets' depth by the specified value.
/// </summary>
static public int AdjustDepth (GameObject go, int adjustment)
{
if (go != null)
{
UIPanel panel = go.GetComponent<UIPanel>();
if (panel != null)
{
UIPanel[] panels = go.GetComponentsInChildren<UIPanel>(true);
for (int i = 0; i < panels.Length; ++i)
{
UIPanel p = panels[i];
#if UNITY_EDITOR
RegisterUndo(p, "Depth Change");
#endif
p.depth = p.depth + adjustment;
}
return 1;
}
else
{
UIWidget[] widgets = go.GetComponentsInChildren<UIWidget>(true);
for (int i = 0, imax = widgets.Length; i < imax; ++i)
{
UIWidget w = widgets[i];
#if UNITY_EDITOR
RegisterUndo(w, "Depth Change");
#endif
w.depth = w.depth + adjustment;
}
return 2;
}
}
return 0;
}
/// <summary>
/// Bring all of the widgets on the specified object forward.
/// </summary>
static public void BringForward (GameObject go)
{
int val = AdjustDepth(go, 1000);
if (val == 1) NormalizePanelDepths();
else if (val == 2) NormalizeWidgetDepths();
}
/// <summary>
/// Push all of the widgets on the specified object back, making them appear behind everything else.
/// </summary>
static public void PushBack (GameObject go)
{
int val = AdjustDepth(go, -1000);
if (val == 1) NormalizePanelDepths();
else if (val == 2) NormalizeWidgetDepths();
}
/// <summary>
/// Normalize the depths of all the widgets and panels in the scene, making them start from 0 and remain in order.
/// </summary>
static public void NormalizeDepths ()
{
NormalizeWidgetDepths();
NormalizePanelDepths();
}
/// <summary>
/// Normalize the depths of all the widgets in the scene, making them start from 0 and remain in order.
/// </summary>
static public void NormalizeWidgetDepths ()
{
List<UIWidget> widgets = FindAll<UIWidget>();
if (widgets.Count > 0)
{
widgets.Sort(UIWidget.CompareFunc);
int start = 0;
int current = widgets[0].depth;
for (int i = 0; i < widgets.Count; ++i)
{
UIWidget w = widgets[i];
if (w.depth == current)
{
w.depth = start;
}
else
{
current = w.depth;
w.depth = ++start;
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(w);
#endif
}
}
}
}
/// <summary>
/// Normalize the depths of all the panels in the scene, making them start from 0 and remain in order.
/// </summary>
static public void NormalizePanelDepths ()
{
List<UIPanel> panels = FindAll<UIPanel>();
if (panels.Count > 0)
{
panels.Sort(UIPanel.CompareFunc);
int start = 0;
int current = panels[0].depth;
for (int i = 0; i < panels.Count; ++i)
{
UIPanel p = panels[i];
if (p.depth == current)
{
p.depth = start;
}
else
{
current = p.depth;
p.depth = ++start;
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(p);
#endif
}
}
}
}
/// <summary>
/// Add a child object to the specified parent and attaches the specified script to it.
/// </summary>
static public T AddChild<T> (GameObject parent) where T : Component
{
GameObject go = AddChild(parent);
go.name = GetTypeName<T>();
return go.AddComponent<T>();
}
/// <summary>
/// Add a child object to the specified parent and attaches the specified script to it.
/// </summary>
static public T AddChild<T> (GameObject parent, bool undo) where T : Component
{
GameObject go = AddChild(parent, undo);
go.name = GetTypeName<T>();
return go.AddComponent<T>();
}
/// <summary>
/// Add a new widget of specified type.
/// </summary>
static public T AddWidget<T> (GameObject go) where T : UIWidget
{
int depth = CalculateNextDepth(go);
// Create the widget and place it above other widgets
T widget = AddChild<T>(go);
widget.width = 100;
widget.height = 100;
widget.depth = depth;
widget.gameObject.layer = go.layer;
return widget;
}
/// <summary>
/// Add a sprite appropriate for the specified atlas sprite.
/// It will be sliced if the sprite has an inner rect, and a regular sprite otherwise.
/// </summary>
static public UISprite AddSprite (GameObject go, UIAtlas atlas, string spriteName)
{
UISpriteData sp = (atlas != null) ? atlas.GetSprite(spriteName) : null;
UISprite sprite = AddWidget<UISprite>(go);
sprite.type = (sp == null || !sp.hasBorder) ? UISprite.Type.Simple : UISprite.Type.Sliced;
sprite.atlas = atlas;
sprite.spriteName = spriteName;
return sprite;
}
/// <summary>
/// Get the rootmost object of the specified game object.
/// </summary>
static public GameObject GetRoot (GameObject go)
{
Transform t = go.transform;
for (; ; )
{
Transform parent = t.parent;
if (parent == null) break;
t = parent;
}
return t.gameObject;
}
/// <summary>
/// Finds the specified component on the game object or one of its parents.
/// </summary>
static public T FindInParents<T> (GameObject go) where T : Component
{
if (go == null) return null;
object comp = go.GetComponent<T>();
if (comp == null)
{
Transform t = go.transform.parent;
while (t != null && comp == null)
{
comp = t.gameObject.GetComponent<T>();
t = t.parent;
}
}
return (T)comp;
}
/// <summary>
/// Finds the specified component on the game object or one of its parents.
/// </summary>
static public T FindInParents<T> (Transform trans) where T : Component
{
if (trans == null) return null;
object comp = trans.GetComponent<T>();
if (comp == null)
{
Transform t = trans.transform.parent;
while (t != null && comp == null)
{
comp = t.gameObject.GetComponent<T>();
t = t.parent;
}
}
return (T)comp;
}
/// <summary>
/// Destroy the specified object, immediately if in edit mode.
/// </summary>
static public void Destroy (UnityEngine.Object obj)
{
if (obj != null)
{
if (Application.isPlaying)
{
if (obj is GameObject)
{
GameObject go = obj as GameObject;
go.transform.parent = null;
}
UnityEngine.Object.Destroy(obj);
}
else UnityEngine.Object.DestroyImmediate(obj);
}
}
/// <summary>
/// Destroy the specified object immediately, unless not in the editor, in which case the regular Destroy is used instead.
/// </summary>
static public void DestroyImmediate (UnityEngine.Object obj)
{
if (obj != null)
{
if (Application.isEditor) UnityEngine.Object.DestroyImmediate(obj);
else UnityEngine.Object.Destroy(obj);
}
}
/// <summary>
/// Call the specified function on all objects in the scene.
/// </summary>
static public void Broadcast (string funcName)
{
GameObject[] gos = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[];
for (int i = 0, imax = gos.Length; i < imax; ++i) gos[i].SendMessage(funcName, SendMessageOptions.DontRequireReceiver);
}
/// <summary>
/// Call the specified function on all objects in the scene.
/// </summary>
static public void Broadcast (string funcName, object param)
{
GameObject[] gos = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[];
for (int i = 0, imax = gos.Length; i < imax; ++i) gos[i].SendMessage(funcName, param, SendMessageOptions.DontRequireReceiver);
}
/// <summary>
/// Determines whether the 'parent' contains a 'child' in its hierarchy.
/// </summary>
static public bool IsChild (Transform parent, Transform child)
{
if (parent == null || child == null) return false;
while (child != null)
{
if (child == parent) return true;
child = child.parent;
}
return false;
}
/// <summary>
/// Activate the specified object and all of its children.
/// </summary>
static void Activate (Transform t)
{
SetActiveSelf(t.gameObject, true);
// Prior to Unity 4, active state was not nested. It was possible to have an enabled child of a disabled object.
// Unity 4 onwards made it so that the state is nested, and a disabled parent results in a disabled child.
#if UNITY_3_5
for (int i = 0, imax = t.GetChildCount(); i < imax; ++i)
{
Transform child = t.GetChild(i);
Activate(child);
}
#else
// If there is even a single enabled child, then we're using a Unity 4.0-based nested active state scheme.
for (int i = 0, imax = t.childCount; i < imax; ++i)
{
Transform child = t.GetChild(i);
if (child.gameObject.activeSelf) return;
}
// If this point is reached, then all the children are disabled, so we must be using a Unity 3.5-based active state scheme.
for (int i = 0, imax = t.childCount; i < imax; ++i)
{
Transform child = t.GetChild(i);
Activate(child);
}
#endif
}
/// <summary>
/// Deactivate the specified object and all of its children.
/// </summary>
static void Deactivate (Transform t)
{
#if UNITY_3_5
for (int i = 0, imax = t.GetChildCount(); i < imax; ++i)
{
Transform child = t.GetChild(i);
Deactivate(child);
}
#endif
SetActiveSelf(t.gameObject, false);
}
/// <summary>
/// SetActiveRecursively enables children before parents. This is a problem when a widget gets re-enabled
/// and it tries to find a panel on its parent.
/// </summary>
static public void SetActive (GameObject go, bool state)
{
if (state)
{
Activate(go.transform);
}
else
{
Deactivate(go.transform);
}
}
/// <summary>
/// Activate or deactivate children of the specified game object without changing the active state of the object itself.
/// </summary>
static public void SetActiveChildren (GameObject go, bool state)
{
Transform t = go.transform;
if (state)
{
for (int i = 0, imax = t.childCount; i < imax; ++i)
{
Transform child = t.GetChild(i);
Activate(child);
}
}
else
{
for (int i = 0, imax = t.childCount; i < imax; ++i)
{
Transform child = t.GetChild(i);
Deactivate(child);
}
}
}
/// <summary>
/// Helper function that returns whether the specified MonoBehaviour is active.
/// </summary>
static public bool IsActive (Behaviour mb)
{
#if UNITY_3_5
return mb != null && mb.enabled && mb.gameObject.active;
#else
return mb != null && mb.enabled && mb.gameObject.activeInHierarchy;
#endif
}
/// <summary>
/// Unity4 has changed GameObject.active to GameObject.activeself.
/// </summary>
static public bool GetActive(GameObject go)
{
#if UNITY_3_5
return go && go.active;
#else
return go && go.activeInHierarchy;
#endif
}
/// <summary>
/// Unity4 has changed GameObject.active to GameObject.SetActive.
/// </summary>
static public void SetActiveSelf(GameObject go, bool state)
{
#if UNITY_3_5
go.active = state;
#else
go.SetActive(state);
#endif
}
/// <summary>
/// Recursively set the game object's layer.
/// </summary>
static public void SetLayer (GameObject go, int layer)
{
go.layer = layer;
Transform t = go.transform;
for (int i = 0, imax = t.childCount; i < imax; ++i)
{
Transform child = t.GetChild(i);
SetLayer(child.gameObject, layer);
}
}
/// <summary>
/// Helper function used to make the vector use integer numbers.
/// </summary>
static public Vector3 Round (Vector3 v)
{
v.x = Mathf.Round(v.x);
v.y = Mathf.Round(v.y);
v.z = Mathf.Round(v.z);
return v;
}
/// <summary>
/// Make the specified selection pixel-perfect.
/// </summary>
static public void MakePixelPerfect (Transform t)
{
UIWidget w = t.GetComponent<UIWidget>();
if (w != null) w.MakePixelPerfect();
if (t.GetComponent<UIAnchor>() == null && t.GetComponent<UIRoot>() == null)
{
#if UNITY_EDITOR
RegisterUndo(t, "Make Pixel-Perfect");
#endif
t.localPosition = Round(t.localPosition);
t.localScale = Round(t.localScale);
}
// Recurse into children
for (int i = 0, imax = t.childCount; i < imax; ++i)
MakePixelPerfect(t.GetChild(i));
}
/// <summary>
/// Save the specified binary data into the specified file.
/// </summary>
static public bool Save (string fileName, byte[] bytes)
{
#if UNITY_WEBPLAYER || UNITY_FLASH || UNITY_METRO || UNITY_WP8
return false;
#else
if (!NGUITools.fileAccess) return false;
string path = Application.persistentDataPath + "/" + fileName;
if (bytes == null)
{
if (File.Exists(path)) File.Delete(path);
return true;
}
FileStream file = null;
try
{
file = File.Create(path);
}
catch (System.Exception ex)
{
NGUIDebug.Log(ex.Message);
return false;
}
file.Write(bytes, 0, bytes.Length);
file.Close();
return true;
#endif
}
/// <summary>
/// Load all binary data from the specified file.
/// </summary>
static public byte[] Load (string fileName)
{
#if UNITY_WEBPLAYER || UNITY_FLASH || UNITY_METRO || UNITY_WP8
return null;
#else
if (!NGUITools.fileAccess) return null;
string path = Application.persistentDataPath + "/" + fileName;
if (File.Exists(path))
{
return File.ReadAllBytes(path);
}
return null;
#endif
}
/// <summary>
/// Pre-multiply shaders result in a black outline if this operation is done in the shader. It's better to do it outside.
/// </summary>
static public Color ApplyPMA (Color c)
{
if (c.a != 1f)
{
c.r *= c.a;
c.g *= c.a;
c.b *= c.a;
}
return c;
}
/// <summary>
/// Inform all widgets underneath the specified object that the parent has changed.
/// </summary>
static public void MarkParentAsChanged (GameObject go)
{
UIWidget[] widgets = go.GetComponentsInChildren<UIWidget>();
for (int i = 0, imax = widgets.Length; i < imax; ++i)
widgets[i].ParentHasChanged();
}
/// <summary>
/// Access to the clipboard via undocumented APIs.
/// </summary>
static public string clipboard
{
get
{
TextEditor te = new TextEditor();
te.Paste();
return te.content.text;
}
set
{
TextEditor te = new TextEditor();
te.content = new GUIContent(value);
te.OnFocus();
te.Copy();
}
}
[System.Obsolete("Use NGUIText.EncodeColor instead")]
static public string EncodeColor (Color c) { return NGUIText.EncodeColor(c); }
[System.Obsolete("Use NGUIText.ParseColor instead")]
static public Color ParseColor (string text, int offset) { return NGUIText.ParseColor(text, offset); }
[System.Obsolete("Use NGUIText.StripSymbols instead")]
static public string StripSymbols (string text) { return NGUIText.StripSymbols(text); }
/// <summary>
/// Extension for the game object that checks to see if the component already exists before adding a new one.
/// If the component is already present it will be returned instead.
/// </summary>
static public T AddMissingComponent<T> (this GameObject go) where T : Component
{
T comp = go.GetComponent<T>();
if (comp == null)
{
#if UNITY_EDITOR
if (!Application.isPlaying)
RegisterUndo(go, "Add " + typeof(T));
#endif
comp = go.AddComponent<T>();
}
return comp;
}
}
|