美文网首页
Unity插件-快速复制所选择节点的路径

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

作者: 程洛_1114 | 来源:发表于2023-11-22 17:29 被阅读0次

将此脚本保存为.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;
    }
}

相关文章

网友评论

      本文标题:Unity插件-快速复制所选择节点的路径

      本文链接:https://www.haomeiwen.com/subject/tqhwwdtx.html