2014年4月15日 星期二

[Unity3D] 找尋gameobject的child gameobject

[實作]

ObjectUltility.FindChildTransform

public static Transform FindChildTransform(Transform root ,string name ){
      if (root == null) {
        return null;
      } else if (root.name == name) {
        return root;
      }

      foreach(Transform child in root) {
        Transform childResult  = FindChildTransform(child, name);
        if (childResult != null) {

            return childResult;
          }
       }
    return null;
}

[用法]

GameObject flashLight;

flashLight = ObjectUltility.FindChildTransform( transform , "flashLight").gameObject;



沒有留言:

張貼留言