Created
May 17, 2016 04:26
-
-
Save cubed2d/61e28c6008fb0e0e27d14eeb15ec36d3 to your computer and use it in GitHub Desktop.
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 UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
public static class TransformExtentions | |
{ | |
public static Transform FindInChildren(this Transform t, string name) | |
{ | |
// Heres a smart linq version. i dont like it. | |
//return (from x in t.gameObject.GetComponentsInChildren<Transform>() | |
// where x.gameObject.name == name | |
// select x.gameObject).First().transform; | |
Transform[] children; | |
if (!t.gameObject.activeSelf) | |
{ | |
// Corner case! GetComponents in children only works if object is active. >< | |
children = t.GetArrayOfAllChildrenWhilstInactive(); | |
} | |
else | |
{ | |
children = t.GetComponentsInChildren<Transform>(); | |
} | |
foreach (Transform child in children) | |
{ | |
if (child.name == name) | |
return child; | |
} | |
return null; | |
} | |
public static Transform[] GetArrayOfAllChildrenWhilstInactive(this Transform t) | |
{ | |
var list = GetListOfAllChildrenWhilstInactive(t); | |
return list.ToArray<Transform>(); | |
} | |
public static List<Transform> GetListOfAllChildrenWhilstInactive(this Transform t, List<Transform> list = null) | |
{ | |
if (list == null) | |
{ | |
list = new List<Transform>(); | |
} | |
foreach (Transform child in t) | |
{ | |
list.Add(child); | |
GetListOfAllChildrenWhilstInactive(child, list); | |
} | |
return list; | |
} | |
public static T FindComponentInParents<T>(this Transform transform) where T : Component | |
{ | |
var t = transform; | |
while (t != null) | |
{ | |
var ret = t.GetComponent<T>(); | |
if (ret != null) | |
return ret; | |
t = t.parent; | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment