美文网首页
MenuItem 常用方法

MenuItem 常用方法

作者: 知交 | 来源:发表于2020-11-12 17:18 被阅读0次
    1. 将场景中GameObject路径添加到剪切板
    [MenuItem("GameObject/Copy Control Path", false, 0)]
        private static void CopyControlPath(MenuCommand menuCommand)
        {
            var target = Selection.activeGameObject;
            var transform = target.transform;
            Stack<string> s = new Stack<string>();
            while (transform) {
                s.Push(transform.name);
                transform = transform.parent;
            }
    
            //remove canvas and root;
            for (int i = 0; i < 2; ++i) {
                s.Pop();
            }
            var arr = s.ToArray();
            var result = string.Join("/", arr);
            GUIUtility.systemCopyBuffer = "\"" + result + "\"".Trim('\n');
            Debug.Log($"{result} has copied to clipboard");
        }
    
    1. 将prefab路径添加到剪切板
    [MenuItem("Assets/Copy Asset Path",false,10000)]
        private static void CopyAssetPath()
        {
            var target = Selection.activeObject;
            if (target != null) {
                var filename = AssetDatabase.GetAssetPath(target);
                GUIUtility.systemCopyBuffer = filename;
                Debug.Log($"{filename} has copied to clipboard");
            }
        }
    

    相关文章

      网友评论

          本文标题:MenuItem 常用方法

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