-
Notifications
You must be signed in to change notification settings - Fork 5
/
PopGameObject.cs
156 lines (125 loc) · 3.67 KB
/
PopGameObject.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class PopGameObject
{
public static T[] FindObjectsOfTypeIncludingDisabled<T>(this GameObject go)
{
return go.GetComponentsInChildren<T> (true);
}
public static T[] FindObjectsOfTypeIncludingDisabled<T>()
{
var ActiveScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene ();
var RootObjects = ActiveScene.GetRootGameObjects ();
var MatchObjects = new List<T> ();
foreach (var ro in RootObjects) {
var Matches = ro.FindObjectsOfTypeIncludingDisabled<T> ();
MatchObjects.AddRange (Matches);
}
return MatchObjects.ToArray ();
}
public static T FindObjectOfTypeIncludingDisabled<T>(this GameObject go)
{
return go.GetComponentInChildren<T> (true);
}
public static T FindObjectOfTypeIncludingDisabled<T>()
{
var ActiveScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene ();
var RootObjects = ActiveScene.GetRootGameObjects ();
foreach (var ro in RootObjects) {
var Match = ro.FindObjectOfTypeIncludingDisabled<T> ();
if (Match != null)
return Match;
}
return default(T);
}
public static T FindObjectOfTypeInParents<T>(this GameObject go)
{
var parent = go.transform.parent;
if (parent == null)
return default (T);
var Match = parent.GetComponent<T> ();
if (Match != null)
return Match;
return parent.gameObject.FindObjectOfTypeInParents<T> ();
}
public static T[] FindObjectsOfTypeWithName<T>(string MatchName) where T : Component
{
System.Func<T,bool> Match = (x) =>
{
return x.name == MatchName;
};
return FindObjectsOfTypeMatching<T> (Match);
}
public static T[] FindObjectsOfTypeMatching<T>(System.Func<T,bool> Match) where T : Object
{
var MatchObjects = new List<T> ();
MatchObjects.AddRange (GameObject.FindObjectsOfType<T> ());
for (int i = MatchObjects.Count - 1; i >= 0; i--) {
var mo = MatchObjects [i];
if (Match (mo))
continue;
MatchObjects.RemoveAt (i);
}
return MatchObjects.ToArray ();
}
public static void ForEachChild(this GameObject go,System.Action<GameObject> Lambda)
{
var t = go.transform;
for (var c = 0; c < t.childCount; c++ )
{
var Child = t.GetChild(c);
Lambda.Invoke(Child.gameObject);
}
}
// if you return false from this lambda, the search will abort early
public static void ForEachChild(this GameObject go, System.Func<GameObject,bool> Lambda)
{
var t = go.transform;
for (var c = 0; c < t.childCount; c++)
{
var Child = t.GetChild(c);
if (!Lambda.Invoke(Child.gameObject))
break;
}
}
public static T GetChildMatching<T>(this GameObject go,System.Func<T,bool> Match) where T : Object
{
var t = go.transform;
for (var c = 0; c < t.childCount; c++)
{
var Child = t.GetChild(c);
var ChildComp = (typeof(T) == typeof(GameObject)) ? (Child.gameObject as T) : Child.GetComponent<T>();
if ( !ChildComp )
continue;
if ( Match( ChildComp) )
return ChildComp;
}
return null;
}
public static GameObject GetChildWithName(this GameObject go,string Name)
{
System.Func<GameObject,bool> Match = (Child) =>
{
if (Child.name != Name)
return false;
return true;
};
return go.GetChildMatching<GameObject>( Match );
}
public static int GetChildIndex(this Transform Parent, Transform ChildMatch)
{
var Count = Parent.childCount;
for (var c = 0; c < Count; c++)
{
var Child = Parent.GetChild(c);
if (Child == ChildMatch)
return c;
}
throw new System.Exception("Child " + ChildMatch.name + " not a child of " + Parent.name);
}
public static int GetChildIndex(this GameObject Parent, GameObject ChildMatch)
{
return Parent.transform.GetChildIndex(ChildMatch.transform);
}
}