(快捷键)一键CopyUI路径(全路径/相对于预制体路径)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace MissYi
{
public enum FindType
{
FullPath = 0,
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)]
public static void CopyPathByPrefab()
{
EditorGUIUtility.systemCopyBuffer = GetFullPath(FindType.PrefabPath);
Debug.Log($"已复制到粘贴板(相对于预制体路径): {m_Path}");
}
#endregion
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入口进行复制。
---
若有不足,欢迎指正...