MenuItem 常用方法

  1. 将场景中GameObject路径添加到剪切板
[MenuItem("GameObject/Copy Control Path", false, 0)]
    private static void CopyControlPath(MenuCommand menuCommand)
    {
        var target = Selection.activeGameObject;
        var transform = target.transform;
        Stack s = new Stack();
        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 常用方法)