121 GameObjectを検索する拡張機能(5.5.0f3)

Unityの標準のGameObject.Find を使いやすい形にラップしたFindDeepクラスを提供します。

GameObjectExtensions.cs
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
 
public static class GameObjectExtensions
{
    /// <summary>
    /// 指定した名前のGameObjectが見つかるまで探します。
    /// </summary>
    public static GameObject FindDeep(
        this GameObject self,
        string name,
        bool includeInactive = true,
        bool parent = false)
    {
        Transform[] children = null;
        if (parent)
        {
            children = self.GetComponentsInParent<Transform>(includeInactive);
        }
        else
        {
            children = self.GetComponentsInChildren<Transform>(includeInactive);
        }
 
        foreach (var transform in children)
        {
            if (transform.name == name)
            {
                return transform.gameObject;
            }
        }
        return null;
    }
 
    /// <summary>
    /// 指定した名前のGameObjectが見つかるまでPath指定で探します。
    /// </summary>
    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");
}