利用反射打开PreferencesWindow

作者:追风剑情 发布于:2018-4-11 15:02 分类:Unity3d

示例


[MenuItem ("Tool/Open Unity Preferences")]
    static void OpenUnityPreferences()
    {
        //GetType("NameSpace.ClassName+嵌套类,程序集")
        System.Type T = System.Type.GetType("UnityEditor.PreferencesWindow,UnityEditor");
        Debug.Log(T);
        if (T == null)
            return;
        var focusedWindow = EditorWindow.focusedWindow;
        //已经打开
        if (focusedWindow != null && focusedWindow.GetType() == T)
            return;

        //GetWindow(Type, true(不可嵌入其他窗口内), 标题)
        var window = EditorWindow.GetWindow(T, true, "Unity Preferences");
        if (window == null) {
            Debug.Log("No Preferences Window Found");
            return;
        }

        var invokerType = window.GetType();
        var invokerMethod = invokerType.GetMethod("ReadPreferences",
            System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

        if (invokerMethod != null) {
            invokerMethod.Invoke(window, null);
        } else {
            Debug.Log("No Reflection Method Found For Preferences");
        }
    }


111111.jpg

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号