将此脚本保存为.cs文件并将其放置在项目的Editor文件夹中。之后,在Unity中选择Hierarchy视图中的任何GameObject,右键单击该对象,你将会看到一个"Copy Path"的选项。点击它将会将该GameObject的完整路径复制到剪贴板中。
测试UnityEditor为:2022.3.12f1
如图:
using UnityEditor;
using UnityEngine;
public class CopyPath : EditorWindow
{
[MenuItem("GameObject/Copy Path", priority = 0)]
static void CopyGameObjectPathCommand(MenuCommand menuCommand)
{
GameObject selectedObject = Selection.activeGameObject;
if (selectedObject != null)
{
string gameObjectPath = GetGameObjectPath(selectedObject);
EditorGUIUtility.systemCopyBuffer = gameObjectPath;
Debug.Log("选择的节点路径为: " + gameObjectPath);
}
else
{
Debug.LogWarning("No GameObject selected.");
}
}
private static string GetGameObjectPath(GameObject obj)
{
string path = obj.name;
Transform parent = obj.transform.parent;
while (parent != null)
{
if (parent.name != "Main Camera")
{
path = parent.name + "/" + path;
}
parent = parent.parent;
}
return path;
}
}