Unity插件-快速复制所选择节点的路径

将此脚本保存为.cs文件并将其放置在项目的Editor文件夹中。之后,在Unity中选择Hierarchy视图中的任何GameObject,右键单击该对象,你将会看到一个"Copy Path"的选项。点击它将会将该GameObject的完整路径复制到剪贴板中。
测试UnityEditor为:2022.3.12f1
如图:


屏幕截图 2023-11-23 173209.png
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;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容