Unity 快键复制输出Prefab节点路径

使用transform.Find需要传入相应节点的路径。之前懵懂无知,傻乎乎地挨个节点复制粘贴,遇到节点层级多的,还担心看岔看漏,得反复检查。 有一回复制得恶心难受,迫出灵光一闪。想到开发快捷键Console面板输出节点路径。解放生产力!后来进一步偷懒,将路径复制到剪贴板,直接粘贴,一步到位!
Unity 快键复制输出Prefab节点路径_第1张图片
自定义功能菜单,支持快捷键

public class Tools
{
    // 根节点->当前节点的完整路径
    private static string m_FullPath = string.Empty;
    // 不含根节点的路径
    private static string m_PathNotIncludeRoot = string.Empty;

    // %-Ctrl; #-Shift; &-Alt
    [MenuItem("Tool/输出Prefab节点路径 %#&c", false, 1)]
    public static void OutputNodePath()
    {
        Transform selectedTransform = Selection.activeTransform;
        PrefabType type = PrefabUtility.GetPrefabType(selectedTransform);
        if (type != PrefabType.PrefabInstance)
        {
            Debug.LogError("当前选中的不是prefab实例");
            return;
        }

        GameObject prefabRoot = PrefabUtility.FindPrefabRoot(selectedTransform.gameObject);
        GameObject nodeGo = selectedTransform.gameObject;
        if (GameObject.Equals(nodeGo, prefabRoot))
        {
            Debug.LogError("当前选中的是Prefab实例的根节点,无需输出节点路径");
            return;
        }

        Deal(prefabRoot, nodeGo);
    }

    private static void Deal(GameObject prefabRoot, GameObject nodeGo)
    {
        Stack<string> nodeNameStack = new Stack<string>();
        nodeNameStack.Push(nodeGo.name);
        do
        {
            nodeGo = nodeGo.transform.parent.gameObject;
            nodeNameStack.Push(nodeGo.name);
        } while (!GameObject.Equals(nodeGo, prefabRoot));

        int i = 0;
        foreach (var nodeName in nodeNameStack)
        {
            if (i > 0)
            {
                if (i > 1)
                {
                    m_PathNotIncludeRoot += "/";
                }
                m_FullPath += "/";
                m_PathNotIncludeRoot += nodeName;
            }
            m_FullPath += nodeName;
            i++;
        }
        // 复制粘贴到剪贴板
        GUIUtility.systemCopyBuffer = m_PathNotIncludeRoot;
        Debug.Log("呀哈哈 节点路径:\n" + m_FullPath);
    }
}

呀哈哈 将偷懒贯彻到底 第一篇技术博客;)

你可能感兴趣的:(Unity)