Created
August 24, 2015 08:18
-
-
Save grimmdev/3c4789dec2c8489fe4b0 to your computer and use it in GitHub Desktop.
Some useful functions that extends Unity's GameObject
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public static class GameObjectExtension | |
{ | |
public static T GetComponentInParents<T>(this Component self) where T : Component | |
{ | |
return self.transform.GetComponentInParents<T>(); | |
} | |
public static T GetComponentInParents<T>(this GameObject self) where T : Component | |
{ | |
return self.transform.GetComponentInParents<T>(); | |
} | |
public static T GetComponentInParents<T>(this Transform self) where T : Component | |
{ | |
T component = self.GetComponent<T>(); | |
if (component != null) | |
{ | |
return component; | |
} | |
if (self.parent == null) | |
{ | |
return (T)((object)null); | |
} | |
return self.parent.GetComponentInParents<T>(); | |
} | |
public static GameObject GetRootParentWithLayer(this GameObject self, int layer) | |
{ | |
Transform parent = self.transform.parent; | |
return (!(parent == null) && parent.gameObject.layer == layer) ? parent.gameObject.GetRootParentWithLayer(layer) : self; | |
} | |
public static GameObject GetRootParent(this GameObject self) | |
{ | |
return self.transform.GetRootParent(); | |
} | |
public static GameObject GetRootParent(this Transform self) | |
{ | |
return (!(self.parent == null)) ? self.parent.GetRootParent() : self.gameObject; | |
} | |
public static List<GameObject> FindGameObjectsWithLayer(int layer) | |
{ | |
GameObject[] array = UnityEngine.Object.FindObjectsOfType(typeof(GameObject)) as GameObject[]; | |
List<GameObject> list = new List<GameObject>(); | |
for (int i = 0; i < array.Length; i++) | |
{ | |
if (array[i].layer == layer) | |
{ | |
list.Add(array[i]); | |
} | |
} | |
if (list.Count != 0) | |
{ | |
return list; | |
} | |
return null; | |
} | |
public static GameObject FindGameObjectWithLayer(int layer) | |
{ | |
GameObject[] array = UnityEngine.Object.FindObjectsOfType(typeof(GameObject)) as GameObject[]; | |
for (int i = 0; i < array.Length; i++) | |
{ | |
if (array[i].layer == layer) | |
{ | |
return array[i]; | |
} | |
} | |
return null; | |
} | |
public static List<Collider> FindCollidersWithLayer(int layer) | |
{ | |
List<Collider> list = new List<Collider>(); | |
List<GameObject> list2 = GameObjectExtension.FindGameObjectsWithLayer(layer); | |
if (list2 != null) | |
{ | |
foreach (GameObject current in list2) | |
{ | |
if (current.collider != null) | |
{ | |
list.Add(current.collider); | |
} | |
} | |
} | |
if (list.Count != 0) | |
{ | |
return list; | |
} | |
return null; | |
} | |
public static Collider FindColliderWithLayer(int layer) | |
{ | |
GameObject gameObject = GameObjectExtension.FindGameObjectWithLayer(layer); | |
return gameObject.collider; | |
} | |
public static void SetParent(this Transform self, Transform parent, bool resetScale = true) | |
{ | |
self.parent = parent; | |
self.localPosition = Vector3.zero; | |
self.localRotation = Quaternion.identity; | |
if (resetScale) | |
{ | |
self.localScale = Vector3.one; | |
} | |
} | |
public static Transform FindChild(this GameObject self, string name) | |
{ | |
Transform[] componentsInChildren = self.transform.GetComponentsInChildren<Transform>(); | |
Transform[] array = componentsInChildren; | |
for (int i = 0; i < array.Length; i++) | |
{ | |
Transform transform = array[i]; | |
if (transform.gameObject.name == name) | |
{ | |
return transform; | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment