unity中获取从根节点到本transfrom节点的路径的公用方法

using UnityEngine;
using System.Text;
    // *** 静态类
    public static class Extensions
    {
        ///


        /// // *** 获取某对象从根节点到自身的路径
        ///

        public static string GetPath(this Transform _tra)
        {
            if (_tra == null)
            {
                UnityEngine.Debug.Log("对象为空"+_tra.name);
            }
            StringBuilder tempPath = new StringBuilder(_tra.name);
            Transform tempTra = _tra;
            string g = "/";
            while (tempTra.parent != null)
            {
                tempTra = tempTra.parent;
                tempPath.Insert(0, tempTra.name + g);
            }
            //Debug.Log("路径: " + tempPath);
            return tempPath.ToString();
        }
    }

    public class GameMAnagera :MonoBehaviour
    {
        private void Awake()
        {
            // *** 输出本脚本挂载的节点的路径
            Debug.Log(transform.GetPath());
        }
    }

你可能感兴趣的:(本人原创,U3D好好学习)