赞
踩
在开发游戏的过程中我们一般都会开发一些便利的开发工具,大部分时候我们都是借助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; } }
EditorWindow的 GetWindow()方法可以返回一个模板窗口,然后我们就基于此窗口,进行拓展。
绘制窗口控件有 几个常用的类: 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; } }
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"}); }
以下是全部代码:
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"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。