====== 121 GameObjectを検索する拡張機能(5.5.0f3) ====== Unityの標準のGameObject.Find を使いやすい形にラップしたFindDeepクラスを提供します。 using System.Collections.Generic; using UnityEngine; using System.Linq; public static class GameObjectExtensions { /// /// 指定した名前のGameObjectが見つかるまで探します。 /// public static GameObject FindDeep( this GameObject self, string name, bool includeInactive = true, bool parent = false) { Transform[] children = null; if (parent) { children = self.GetComponentsInParent(includeInactive); } else { children = self.GetComponentsInChildren(includeInactive); } foreach (var transform in children) { if (transform.name == name) { return transform.gameObject; } } return null; } /// /// 指定した名前のGameObjectが見つかるまでPath指定で探します。 /// public static GameObject FindDeepPath( this GameObject self, string name, bool includeInactive = true, bool parent = false) { GameObject target = self; string[] nameList = name.Split('/'); foreach (var findName in nameList) { GameObject result = target.FindDeep(findName, includeInactive, parent); if(result != null) { target = result; } else { return null; } } return target; } } GameObject result; void Start() { // 検索。 result = this.gameObject.FindDeep("HogeHoge"); // スラッシュ区切りで、検索。 result = this.gameObject.FindDeepPath("Header/Button_01"); }