Unity相关征服Unity3dUnity编辑器开发分享

Unity编辑器开关控制显示隐藏

2016-12-22  本文已影响146人  白九a

一直想通过切换按钮实现编辑器上某些部分显示或者隐藏的功能。今天无意中实现了这个功能。原理很简单,编辑器面板上看到的都是绘制上去的,那么只要在特定时候不绘制就实现了这功能。

using UnityEditor;
using UnityEngine;

public class TestEditorWindow : EditorWindow
{
    [MenuItem ("EditorWindow/TestEditorWindow")]
    static void Init ()
    {
        EditorWindow.GetWindow<TestEditorWindow> ();
    }

    string mNotification = "通知消息";
    bool mNotificationShow = false;

    void OnGUI ()
    {
        #region 检测鼠标事件
//      wantsMouseMove = EditorGUILayout.Toggle ("Recive Movment : ", wantsMouseMove);
        wantsMouseMove = EditorGUILayout.BeginToggleGroup ("鼠标移动", wantsMouseMove);
        if (wantsMouseMove) {
            EditorGUILayout.Vector2Field ("    鼠标位置", Event.current.mousePosition);
            if (Event.current.type == EventType.MouseMove && wantsMouseMove) {
                Repaint ();
            }
        }
        EditorGUILayout.EndToggleGroup ();
        #endregion

        #region 显示通知消息
        mNotificationShow = EditorGUILayout.BeginToggleGroup ("通知消息", mNotificationShow);
        if (mNotificationShow) {
            mNotification = EditorGUILayout.TextField (mNotification);
            if (GUILayout.Button ("显示消息")) {
                this.ShowNotification (new GUIContent (mNotification));
            }
            if (GUILayout.Button ("隐藏消息")) {
                RemoveNotification ();
            }
        }
        EditorGUILayout.EndToggleGroup ();
        #endregion
    }
}
效果如下:


上一篇 下一篇

猜你喜欢

热点阅读