Unity编辑器——Inspector 面板扩展

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34536551/article/details/84258948

目录

将EditorGUI 扩展在 Inspector 面板上

EditorWindows 窗口

EditorWindows 下拉菜单

预览窗口

获取预览信息



将EditorGUI 扩展在 Inspector 面板上


●  EditorGUI 和 GUI 的用法几乎完全一致,目前来说前者多用于编辑器开发,后者多用于发布后调试编辑器。总之,它们都是起辅助作用的。 EditorGUI 提供的组件非常丰富,常用的绘制元素包括文本、按钮、图片和滚动框等。做一个好的编辑器,是离不开 EditorGUI 的。如图:我们将 EditorGUI 拓展在 Inspector 面板上了,相关代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class Script_03_23 : MonoBehaviour
{
    public Vector3 scrollPos;
    public int myId;
    public string myName;
    public GameObject prefab;
    public MyEnum myEnum = MyEnum.One;
    public bool toogle1;
    public bool toogle2;
    public enum MyEnum
    {
        One=1,
        Two,
    }

#if UNITY_EDITOR
    [CustomEditor(typeof(Script_03_23))]
    public class ScriptEditor_03_23 : Editor
    {
        private bool m_EnableToogle;
        public override void OnInspectorGUI()
        {
            //获取脚本对象
            Script_03_23 script = target as Script_03_23;

            //绘制滚动条
            script.scrollPos =
                EditorGUILayout.BeginScrollView(script.scrollPos, false, true);
            script.myName = EditorGUILayout.TextField("text", script.myName);
            script.myId = EditorGUILayout.IntField("int", script.myId);

            script.prefab = EditorGUILayout.ObjectField("GameObject", script.prefab,
                typeof(GameObject), true) as GameObject;

            //绘制按钮
            EditorGUILayout.BeginHorizontal();
            GUILayout.Button("1");
            GUILayout.Button("2");
            script.myEnum = (Script_03_23.MyEnum)EditorGUILayout.EnumPopup("MyEnum:",
                script.myEnum);

            EditorGUILayout.EndHorizontal();

            //Toogle 组件
            m_EnableToogle = EditorGUILayout.BeginToggleGroup("EnableToogle",
                m_EnableToogle);
            script.toogle1 = EditorGUILayout.Toggle("toogle1", script.toogle1);
            script.toogle2 = EditorGUILayout.Toggle("toogle2", script.toogle2);
            EditorGUILayout.EndToggleGroup();

            EditorGUILayout.EndScrollView();
        }
    }
}
#endif

EditorGUILayout ——  是EditorGUI 自动 布局版本。
 EditorGUILayout.BeginScrollView ——  函数原型: public static Vector2 BeginScrollView( Vector2 scrollPosition, bool alwaysShowHorizontal,  bool alwaysShowVertical,  params GUILayoutOption[] options);

scrollPosition参数: 显式使用的位置

alwaysShowHorizontal —— 表示可选的参数,始终显示水平滚动条。如果为false或省略,那么则仅当ScrollView中的内容比ScrollView本身更宽时才会显示。

alwayShowVertical ——  表示可选的参数,始终显示垂直滚动条。如果为false或省略,那么只有当ScrollView中的内容比ScrollView本身高时才会显示。


EditorWindows 窗口
 


Unity 提供编辑器窗口,开发者可以自由拓展自己的窗口。 Unity 编辑器系统自带的视图窗口其实也是用 EditorWindows 实现的。如图所示,我们来制作一个简单的编辑窗口,它绘制元素时同样使用 EditorGUI 代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Script_03_24Window : EditorWindow
{

    [MenuItem("Window/Open My Window")]
    static void Init()
    {
        Script_03_24Window window = (Script_03_24Window)EditorWindow.GetWindow(typeof(Script_03_24Window));
        window.Show();
    }

    private Texture m_MyTexture = null;
    private float m_MyFloat = 0.5f;
    void Awake()
    {
        Debug.LogFormat("窗口初始化时调用");
        m_MyTexture = AssetDatabase.LoadAssetAtPath<Texture>("Assets/unity1.png");
    }
    void OnGUI()
    {
        GUILayout.Label("Hello World!!", EditorStyles.boldLabel);
        m_MyFloat = EditorGUILayout.Slider("Slider", m_MyFloat, -5, 5);
        GUI.DrawTexture(new Rect(0, 30, 100, 100), m_MyTexture);
    }
    void OnDestroy()
    {
        Debug.LogFormat("窗口销毁时调用");
    }
    void OnFocus()
    {
        Debug.LogFormat("窗口拥有焦点时调用");
    }
    void OnHierarchyChange()
    {
        Debug.LogFormat("Hierarchy视图发生改变时调用");
    }
    void OnInspectorUpdate()
    {
        //Debug.LogFormat ("Inspector每帧更新");
    }
    void OnLostFocus()
    {
        Debug.LogFormat("失去焦点");
    }
    void OnProjectChange()
    {
        Debug.LogFormat("Project视图发生改变时调用");
    }
    void OnSelectionChange()
    {
        Debug.LogFormat("Hierarchy或者Project视图中选择一个对象时调用");
    }
    void Update()
    {
        //Debug.LogFormat ("每帧更新");
    }
}

EditorWindows 下拉菜单
 


如图 所示,在 EditorWindows 编辑窗口的右上角,有个下拉菜单,我们也可以对该菜单中的选项进行拓展,不过这里需要实现 IHasCustomMenu 接口。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Script_03_25Window : EditorWindow,IHasCustomMenu
{
    void IHasCustomMenu.AddItemsToMenu(GenericMenu menu)
    {
        menu.AddDisabledItem(new GUIContent("Disable"));
        menu.AddItem(new GUIContent("Test1"), true, () =>
        {
            Debug.Log("Test1");
        });

        menu.AddItem(new GUIContent("Test2"), true, () =>
        {
            Debug.Log("Test2");
        });

        menu.AddSeparator("Test/");
        menu.AddItem(new GUIContent("Test/Tes3"), true, () =>
        {
            Debug.Log("Tes3");
        });
    }
        [MenuItem("Window/TAOTAO My Window")]
    static void Init()
    {
        Script_03_25Window window = (Script_03_25Window)EditorWindow.GetWindow(typeof(Script_03_25Window));
        window.Show();
    }
}

上述代码中,我们通过 AddItem()方法来添加列表元素,并且监听选择后的事件。


预览窗口


选择游戏对象或者游戏资源后, Inspector 面板下方将会出现它的预览窗口,但是有些资源是没有预览信息的,不过我们可以监听它的窗口方法来重新绘制它,如图 所示,相关代码如下。


 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomPreview(typeof(GameObject))]
public class Script_03_26 : ObjectPreview
{

    public override bool HasPreviewGUI()
    {
        return true;
    }
    public override void OnPreviewGUI(Rect r, GUIStyle background)
    {
        GUI.DrawTexture(r, AssetDatabase.LoadAssetAtPath<Texture>("Assets/Unity.png"));
        GUILayout.Label("Hello World!");
    }
}

获取预览信息
 


有些资源是有预览信息的,比如模型资源。在预览窗口中,我们可以看到它的样式。如果需要在自定义窗口中显示它,就需要获取它的预览信息。如图所示,选择一个游戏对象后,会在自定义窗口中显示它,相关代码如下:
 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Script_03_27window : EditorWindow
{
    private GameObject m_MyGo;
    private Editor m_MyEditor;

    [MenuItem("Window/TAOSHUI Open My Window")]
    static void Init()
    {
        Script_03_27window window = (Script_03_27window)EditorWindow.GetWindow(typeof(Script_03_27window));
        window.Show();
    }
     void OnGUI()
    {
        //设置一个游戏对象
        m_MyGo = (GameObject)EditorGUILayout.ObjectField(m_MyGo, 
            typeof(GameObject), true);
        if(m_MyGo !=null)
        {
            if(m_MyEditor==null)
            {
                //创建Editor 实例
                m_MyEditor = Editor.CreateEditor(m_MyGo);
            }
            //预览它
            m_MyEditor.OnPreviewGUI(GUILayoutUtility.GetRect(500, 500),
               EditorStyles.whiteLabel );
        }
    }
}

在上述代码中,预览对象首先需要通过 Editor.CreateEditor()拿到它的 Editor 实例对象,接着调用 OnPreviewGUI()方法传入窗口的显示区域。

猜你喜欢

转载自blog.csdn.net/qq_34536551/article/details/84258948