-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameObjectExtensions.cs
executable file
·177 lines (158 loc) · 6.01 KB
/
GameObjectExtensions.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
// using System.Reflection;
using System.Linq;
using System;
namespace UberTools
{
public static class GameObjectExtensions
{
public static T FindComponentInChildren<T>(this GameObject go, Predicate<T> pred) where T : Component
{
foreach(var t in go.GetComponentsInChildren<T>())
{
if(pred(t))
{
return t;
}
}
return null;
}
/// Delegate for operating on a series of game objects
public delegate void GameObjectDelegate(GameObject go);
/// Operate on all child game objects
public static void ForEachChild(this GameObject go, Action<GameObject> action, bool includeSelf=true)
{
foreach(GameObject subGo in GetChildren(go, includeSelf))
{
action(subGo);
}
}
/// Destroy all children of a game object
public static void DestroyChildren(this GameObject go)
{
var children = new List<GameObject>();
foreach (Transform child in go.transform)
children.Add(child.gameObject);
children.ForEach(child => GameObject.Destroy(child));
}
/// Gets all the game objects below 'go'
public static IEnumerable<GameObject> GetChildren(this GameObject go, bool includeSelf=true)
{
if(includeSelf)
{
yield return go;
}
foreach (Transform child in go.transform)
{
var childGo = child.gameObject;
yield return childGo;
foreach (var subGo in GetChildren(childGo))
{
yield return subGo;
}
}
}
/// Gets all the game objects above 'go'
public static IEnumerable<GameObject> GetParents(this GameObject go, bool includeSelf=true)
{
if(includeSelf)
{
yield return go;
}
Transform loopTransform = go.transform.parent;
while (loopTransform != null)
{
yield return loopTransform.gameObject;
loopTransform = loopTransform.parent;
}
}
/// Gets all the components of type T in GameObjects above and including 'go'
public static IEnumerable<T> GetComponentsInParents<T>(this GameObject go, bool includeSelf = true) where T : Component
{
foreach (var parentGo in GetParents(go, includeSelf))
{
T component = parentGo.GetComponent<T>();
if (component != null)
{
yield return component;
}
}
}
/// Gets all the components of type T in GameObjects above and including 'go'
public static T GetComponentInParents<T>(this GameObject go, bool includeSelf = true) where T : Component
{
foreach (var parentGo in GetParents(go, includeSelf))
{
T component = parentGo.GetComponent<T>();
if (component != null)
{
return component;
}
}
return null;
}
/// A helper function for cloning GameObjects;
/// "go" - The GameObject to clone
/// "parent" - The parent for the new object, which may be null
/// "pos" - The position for the new object
/// "rotation" - The quat rotation for the new object
/// "scale" - The vector scale of the new object
/// "space" - The 'space' in which to apply pos/scale/rotation. Defaults to 'World', but if you're parenting to another object you may want 'Local'
static public GameObject Clone(this GameObject go,
GameObject parent=null,
Vector3? pos=null,
Quaternion? rotation=null,
Vector3? scale=null,
TransformSpace space=TransformSpace.World)
{
var newGO = UnityEngine.Object.Instantiate(go) as GameObject;
if (newGO != null)
{
newGO.name = go.name;
}
if(pos!=null)
{
newGO.transform.position = pos ?? Vector3.zero;
}
if (rotation != null)
{
newGO.transform.rotation = rotation ?? Quaternion.identity;
}
if (scale != null)
{
newGO.transform.localScale = scale ?? Vector3.one;
}
if (parent != null)
{
newGO.transform.SetParent(parent.transform, space);
}
return newGO;
}
/// Return the bounds of the renderers of the gameObject and its children
/// Set includeParticles to false if you want to exclude particles
static public Bounds GetBoundsInclusive(this GameObject go, bool includeParticles = true)
{
Bounds bounds = new Bounds();
bool boundsSet = false;
Renderer[] renderers = go.GetComponentsInChildren<Renderer>();
foreach (var r in renderers)
{
if(r.GetType()!=typeof(ParticleSystemRenderer) || includeParticles)
{
if (boundsSet)
{
bounds.Encapsulate(r.bounds);
}
else
{
bounds = r.bounds;
boundsSet = true;
}
}
}
return bounds;
}
}
}