Last active
August 29, 2015 14:21
-
-
Save enpel/b83b2da041c7def548c7 to your computer and use it in GitHub Desktop.
親の親の親の...親からGetComponentしたい!!そんな拡張 ref: http://qiita.com/enpel/items/7d9eb414a92f15ea694a
This file contains 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; | |
public static class GameObjectExtensions | |
{ | |
public static T GetComponentInParents<T>(this Transform obj) where T : MonoBehaviour, new() | |
{ | |
Transform parent = obj.transform.parent; | |
while(parent != null) | |
{ | |
T res = parent.GetComponent<T> (); | |
if (res != null) | |
return res; | |
parent = parent.parent; | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment