コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】ゲームオブジェクトの Hierarchy におけるパスを取得できる拡張メソッド

ソースコード

using UnityEngine;

public static class GameObjectExt
{
    public static string GetHierarchyPath( this GameObject gameObject )
    {
        var path = gameObject.name;
        var parent = gameObject.transform.parent;

        while ( parent != null )
        {
            path = parent.name + "/" + path;
            parent = parent.parent;
        }

        return path;
    }
}

使用例

using UnityEngine;

public class Test : MonoBehaviour
{
    private void Awake()
    {
        Debug.Log( gameObject.GetHierarchyPath() );
    }
}

関連記事