当前位置:   article > 正文

UNITY__EDITORWINDOW 编辑器初探_unity editorwindow

unity editorwindow

前言:

在开发游戏的过程中我们一般都会开发一些便利的开发工具,大部分时候我们都是借助unity自带编辑器去写一些常用工具。然后 有什么错误的理解或者使用的,也欢迎各位指正哈!就当是相互学习了。
EditorWindow
窗口基类,基本上我们需要开发的窗口工具都是继承自EditorWindow。包括unity自身的一些窗口(Hierarchy,Inspector,Scene,Game,Project,Console)等等!

/// <summary>
/// 编辑器基类,方便后期管理拓展
/// </summary>
public class EditorWindowBase<T> : EditorWindow where T : EditorWindow
{
    private static T _instance;
    public static T Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = GetWindow<T>();
            }
            return _instance;
        }
    }
    /// <summary>
    /// 窗口优先级
    /// </summary>
    public int Priority { get; set; }
    
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

EditorWindowGetWindow()方法可以返回一个模板窗口,然后我们就基于此窗口,进行拓展。
在这里插入图片描述

绘制窗口控件

绘制窗口控件有 几个常用的类: EditorGUI,EditorGUILayout,GUILayout,GUI。这几个类都可以创建常用控件,不同的也是互补的,基本能满足我们的需要!
EditorGUI 和 GUI 是基于我们设置的基准点生成的,无法自动排列!
EditorGUILayout 和 GUILayout 是自动排列的!

GUI 我一般很少用来绘制控件,用的比较多的是修改一些基本属性,比如控件颜色,文本颜色,控件层级什么的!

ToolBar 工具栏
常用的工具栏,就是下拉菜单,按钮,搜索框什么的!废话咱就不多说了,直接看效果!
在这里插入图片描述

private void TopToolBar()
    {
        GUI.color = Color.cyan;

        GUILayout.BeginHorizontal(EditorStyles.toolbar);
        if (GUILayout.Button("Clear All",EditorStyles.toolbarButton,GUILayout.MaxWidth(100f)))
        {
            
        }
        if (GUILayout.Button("下拉列表",EditorStyles.toolbarPopup,GUILayout.MaxWidth(200f)))
        {
            
        }
        CreateDropdown();
        SearchField();
        GUILayout.EndHorizontal();
        GUI.color = Color.white;
    }
    private void CreateDropdown()
    {
        Rect rect = GUILayoutUtility.GetRect(m_Styles.m_CreateDropdownContent, EditorStyles.toolbarDropDown,GUILayout.MaxWidth(240f),GUILayout.MinWidth(120f));
        if (EditorGUI.DropdownButton(rect,m_Styles.m_CreateDropdownContent,FocusType.Passive,EditorStyles.toolbarDropDown))
        {
            GUIUtility.hotControl = 0;
            EditorUtility.DisplayPopupMenu(rect,"Assets/Create",null);
        }
    }

    private void SearchField()
    {
        Rect rect = GUILayoutUtility.GetRect(120f, 300f, 18f, 20f, EditorStyles.toolbarSearchField,GUILayout.MinWidth(120f),GUILayout.MaxWidth(300f));
        m_lastSearchFilter = EditorGUI.TextField(rect, m_SearchFieldText, EditorStyles.toolbarSearchField);
        if (m_lastSearchFilter != m_SearchFieldText)
        {
            m_SearchFieldText = m_lastSearchFilter;
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

在这里插入图片描述

	private void LoadFiles()
    {
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.TextField("文件路径:",m_FilePath );
        if (GUILayout.Button("打开文件夹",GUILayout.MaxWidth(200f)))
        {
            m_FilePath = EditorUtility.OpenFilePanel("选择文件", m_FilePath, "");
        }
        EditorGUILayout.EndHorizontal();
    }

    private void FiledControls()
    {
        m_IntField = EditorGUILayout.IntField("InitField", m_IntField);
        m_BoundsField = EditorGUILayout.BoundsField("BoundsField", m_BoundsField);
        m_GameObjects = (GameObject)EditorGUILayout.ObjectField("ObjectField", m_GameObjects, typeof(GameObject));
        m_Colors = EditorGUILayout.ColorField("颜色框", m_Colors);
        m_Cuvrs = EditorGUILayout.CurveField("曲线框", m_Cuvrs);
    }
    private void EditorGuiLayOut()
    {
        EditorGUILayout.MinMaxSlider("MinMaxSlider",ref m_MinNum,ref m_MaxNum,0,1f);
        m_EnumFlagsFields = (EnumFlagsFields)EditorGUILayout.EnumFlagsField("多选:",m_EnumFlagsFields);
        m_EnumPopUps = (EnumFlagsFields) EditorGUILayout.EnumPopup("单选:",m_EnumPopUps);
    }

    private void SliderBar()
    {
        var num = EditorGUILayout.Slider(m_Styles.m_Slider, m_SliderNum, 0, 1f);
        if (num != m_SliderNum)
        {
            m_SliderNum = num;
        }
        m_ScrollBarNum = GUILayout.HorizontalScrollbar(m_ScrollBarNum, 1f, 0, 1f);
        //GUILayout.VerticalScrollbar(m_ScrollBarNum, 1f, 0, 1f,GUILayout.Height(600f));
        m_SelectionNum = GUILayout.SelectionGrid(m_SelectionNum, new[] {"1", "2", "3", "4", "5", "6"}, 6);
        GUILayout.Toolbar(0, new[] {"1", "2", "3"});
        
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

以下是全部代码:

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

public class GorgeousWindow : EditorWindowBase<GorgeousWindow>
{

    [MenuItem("窗口管理/奥利给",priority = 0)]
    public static void StartUp()
    {
        Instance.Priority = 1;
        Instance.Show();
        Instance.minSize = new Vector2(400,400);
    }

    internal void OnEnable()
    {
        Init();

    }
    //---------------------------------以下为窗口属性字段----------------------------------//
    private static Styles m_Styles;
    [NonSerialized]
    private string m_SearchFieldText = "";
    [NonSerialized]
    private string m_lastSearchFilter;
    [SerializeField] 
    private float m_SliderNum;
    [SerializeField] 
    private float m_ScrollBarNum;
    [SerializeField] 
    private int m_SelectionNum;
    [SerializeField] 
    private float m_MinNum;
    [SerializeField] 
    private float m_MaxNum;
    [SerializeField]
    private EnumFlagsFields m_EnumFlagsFields;
    [SerializeField]
    private EnumFlagsFields m_EnumPopUps;
    [SerializeField]
    private int m_IntField;
    [SerializeField]
    private Bounds m_BoundsField;
    [SerializeField] 
    private GameObject m_GameObjects = null;
    [SerializeField] 
    private Color m_Colors;
    [SerializeField] 
    private AnimationCurve m_Cuvrs = AnimationCurve.Linear(Single.Epsilon, 0f,2f,1f);
    [SerializeField]
    private string m_FilePath = String.Empty;

    private string m_NormalPath = "F:/UnityDemos/XluaGF_Pro";
    
    //----------------------------------以下为窗口所需数据类型------------------------------//
    private enum EnumFlagsFields
    {
        FIRST,
        SECOND,
        THIRD,
        FOURTH,
        FIFTH,
        SIXTH,
        MORE
    }
    
    void Init()
    {
        m_Styles = new Styles();
        m_FilePath = m_NormalPath;
    }
    internal void OnGUI()
    {
        TopToolBar();
        
        GUILayout.BeginVertical();
        GUILayout.BeginScrollView(new Vector2(0, 0));
        SliderBar();
        EditorGuiLayOut();
        FiledControls();
        LoadFiles();
        GUILayout.EndScrollView();
        GUILayout.EndVertical();
    }

    private void LoadFiles()
    {
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.TextField("文件路径:",m_FilePath );
        if (GUILayout.Button("打开文件夹",GUILayout.MaxWidth(200f)))
        {
            m_FilePath = EditorUtility.OpenFilePanel("选择文件", m_FilePath, "");
        }
        EditorGUILayout.EndHorizontal();
    }

    private void FiledControls()
    {
        m_IntField = EditorGUILayout.IntField("InitField", m_IntField);
        m_BoundsField = EditorGUILayout.BoundsField("BoundsField", m_BoundsField);
        m_GameObjects = (GameObject)EditorGUILayout.ObjectField("ObjectField", m_GameObjects, typeof(GameObject));
        m_Colors = EditorGUILayout.ColorField("颜色框", m_Colors);
        m_Cuvrs = EditorGUILayout.CurveField("曲线框", m_Cuvrs);
    }
    private void EditorGuiLayOut()
    {
        EditorGUILayout.MinMaxSlider("MinMaxSlider",ref m_MinNum,ref m_MaxNum,0,1f);
        m_EnumFlagsFields = (EnumFlagsFields)EditorGUILayout.EnumFlagsField("多选:",m_EnumFlagsFields);
        m_EnumPopUps = (EnumFlagsFields) EditorGUILayout.EnumPopup("单选:",m_EnumPopUps);
    }

    private void SliderBar()
    {
        var num = EditorGUILayout.Slider(m_Styles.m_Slider, m_SliderNum, 0, 1f);
        if (num != m_SliderNum)
        {
            m_SliderNum = num;
        }
        m_ScrollBarNum = GUILayout.HorizontalScrollbar(m_ScrollBarNum, 1f, 0, 1f);
        //GUILayout.VerticalScrollbar(m_ScrollBarNum, 1f, 0, 1f,GUILayout.Height(600f));
        m_SelectionNum = GUILayout.SelectionGrid(m_SelectionNum, new[] {"1", "2", "3", "4", "5", "6"}, 6);
        GUILayout.Toolbar(0, new[] {"1", "2", "3"});
        
    }
    private void TopToolBar()
    {
        GUI.color = Color.cyan;

        GUILayout.BeginHorizontal(EditorStyles.toolbar);
        if (GUILayout.Button("Clear All",EditorStyles.toolbarButton,GUILayout.MaxWidth(100f)))
        {
            
        }
        if (GUILayout.Button("下拉列表",EditorStyles.toolbarPopup,GUILayout.MaxWidth(200f)))
        {
            
        }
        CreateDropdown();
        SearchField();
        GUILayout.EndHorizontal();
        GUI.color = Color.white;
    }
    private void CreateDropdown()
    {
        Rect rect = GUILayoutUtility.GetRect(m_Styles.m_CreateDropdownContent, EditorStyles.toolbarDropDown,GUILayout.MaxWidth(240f),GUILayout.MinWidth(120f));
        if (EditorGUI.DropdownButton(rect,m_Styles.m_CreateDropdownContent,FocusType.Passive,EditorStyles.toolbarDropDown))
        {
            GUIUtility.hotControl = 0;
            EditorUtility.DisplayPopupMenu(rect,"Assets/Create",null);
        }
    }

    private void SearchField()
    {
        Rect rect = GUILayoutUtility.GetRect(120f, 300f, 18f, 20f, EditorStyles.toolbarSearchField,GUILayout.MinWidth(120f),GUILayout.MaxWidth(300f));
        m_lastSearchFilter = EditorGUI.TextField(rect, m_SearchFieldText, EditorStyles.toolbarSearchField);
        if (m_lastSearchFilter != m_SearchFieldText)
        {
            m_SearchFieldText = m_lastSearchFilter;
        }
    }
    
    private class Styles
    {
        public GUIContent m_CreateDropdownContent = EditorGUIUtility.IconContent("CreateAddNew");
        public GUIContent m_Slider = new GUIContent("Slider");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/100049
推荐阅读
相关标签
  

闽ICP备14008679号