利用反射查探UnityEditor

作者: karma085 | 来源:发表于2016-07-01 11:21 被阅读0次

    最近看雨松mono的教程的教程,知道了c#的反射用法,就试着查探一下UnityEditor.dll内部的方法和各种属性,大多数都没有实际用途.

    下面是雨松写的,加了个按钮,等同点击InspectorWindow右上角的锁按钮

    ```

    [MenuItem("打包AssetBundle/Lock&unlock")]

    static void OnLock()

    {

    var type = typeof(EditorWindow).Assembly.GetType("UnityEditor.InspectorWindow");

    var window = EditorWindow.GetWindow(type);

    MethodInfo info = type.GetMethod("FlipLocked", BindingFlags.NonPublic | BindingFlags.Instance);

    info.Invoke(window, null);

    }

    ```

    雨松是反编译查看了代码,知道了InspectorWindow里面的属性和方法等等.个人不会反编译,就利用了反射来遍历查看UnityEditor.

    ```

    static void test()

    {

    string name = "C:/work/Rabish/Library/UnityAssemblies/UnityEditor.dll";//项目工程下面的Library

    var type = Assembly.LoadFile(name).GetTypes();

    Type inspetorWindow = type[0];

    foreach (var t in type)//得到.dll里面的内容

    {

    if (t.Name.Contains("InspectorWindow"))

    {

    inspetorWindow = t;

    Debug.Log(t.Name + "...." + t.BaseType);//得出editorWindow类型

    }

    }

    MethodInfo methodinfo = inspetorWindow.GetMethods()[0];

    foreach (var method in inspetorWindow.GetMethods( BindingFlags.NonPublic | BindingFlags.Instance))//查探方法

    {

    Debug.Log(method.Name);

    if (method.Name == "FlipLocked")

    methodinfo = method;

    }

    foreach (var par in methodinfo.GetParameters())//查探方法的参数

    {

    Debug.Log(par.ParameterType + "" + methodinfo.GetParameters().Length);

    }

    var window = EditorWindow.GetWindow(inspetorWindow);

    methodinfo.Invoke(window, null);

    //设置属性

    //foreach (var method in inspetorWindow.GetProperties())

    //{

    //    Debug.Log(method.Name+".....type:"+method.PropertyType);

    //    //if (method.Name == "OnInspectorUpdate")

    //    //    methodinfo = method;

    //}

    //PropertyInfo pi1 = inspetorWindow.GetProperty("title");

    //pi1.SetValue(window, "宝宝巴士", null);

    PropertyInfo pi2 = inspetorWindow.GetProperty("titleContent");

    pi2.SetValue(window, new GUIContent("宝宝巴士", "This is the tooltip"), null);

    }

    ```

    汉化unity,不过没啥用

    相关文章

      网友评论

        本文标题:利用反射查探UnityEditor

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