(快捷键)一键CopyUI路径(全路径/相对于预制体路径)

(快捷键)一键CopyUI路径(全路径/相对于预制体路径)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace MissYi
{
    public enum FindType 
    {
        /// <summary>
        /// 上层全路径
        /// </summary>
        FullPath = 0,

        /// <summary>
        /// 相对于上层预制体的路径
        /// </summary>
        PrefabPath,
    }

    public class E_ManualCopyPath : Editor
    {
        //粘贴板内容
        public static string m_Path = "";

        #region CopyFullPath 复制全路径
        [MenuItem("GameObject/Copy/复制全路径(可使用快捷键A一键复制) _a", priority = 0)]
        public static void CopyPathByFull()
        {
            EditorGUIUtility.systemCopyBuffer = GetFullPath(FindType.FullPath);
            Debug.Log($"已复制到粘贴板(上层全路径):  {m_Path}");
        }

        [MenuItem("GameObject/Copy/复制全路径(相对于预制体)(可使用快捷键S一键复制) _s", priority = 10)]
        //查找上层路径,以Prefab为结束点
        public static void CopyPathByPrefab()
        {
            EditorGUIUtility.systemCopyBuffer = GetFullPath(FindType.PrefabPath);
            Debug.Log($"已复制到粘贴板(相对于预制体路径):  {m_Path}");
        }
        #endregion


        /// <summary>
        /// 根据类型进行查找路径
        /// </summary>
        /// <param name="theFindType"></param>
        /// <returns></returns>
        public static string GetFullPath(FindType theFindType)
        {
            m_Path = "";
            GameObject target = Selection.gameObjects[0];
            if (target == null)
            {
                return m_Path;
            }
            m_Path = target.name;
            switch (theFindType)
            {
                case FindType.FullPath:
                    m_Path = GetParentPath(target.transform);
                    break;
                case FindType.PrefabPath:
                    m_Path = GetParentPathByPrefab(target.transform);
                    break;
                default:
                    break;
            }
            return m_Path;
        }

        //递归查找全路径(相对于顶层)
        public static string GetParentPath(Transform theTrans)
        {
            Transform parent = theTrans.parent;
            if (parent == null)
            {
                return m_Path;
            }
            m_Path = string.Format($"{parent.name}/{m_Path}");
            return GetParentPath(parent);
        }

        //递归查找全路径(相对于预制体)
        public static string GetParentPathByPrefab(Transform theTrans)
        {
            Transform parent = theTrans.parent;
            if (parent == null)
            {
                return m_Path;
            }
            m_Path = string.Format($"{parent.name}/{m_Path}");
            bool isPrefab = UnityEditor.PrefabUtility.IsAnyPrefabInstanceRoot(parent.gameObject);
            if (isPrefab)
            {
                return m_Path;
            }
            else
            {
                return GetParentPathByPrefab(parent);
            }
        }
    }
}

操作步骤:
1.Hierarchy面板中选中单个对象Obj
2.按下快捷键A或S即可一键复制路径,也可右键召唤菜单,使用Copy入口进行复制。
---
若有不足,欢迎指正...
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值