UNITY__EDITORWINDOW 编辑器初探

本文探讨了在Unity开发中如何使用EditorWindow创建自定义编辑器工具,介绍了GetWindow方法及其基本使用。文章详细讲解了EditorGUI、EditorGUILayout、GUILayout和GUI类在绘制窗口控件时的作用和区别,并指出GUI常用于调整控件属性而非绘制。最后,提到了ToolBar工具栏的实现,并提供了部分代码示例。
摘要由CSDN通过智能技术生成

前言:

在开发游戏的过程中我们一般都会开发一些便利的开发工具,大部分时候我们都是借助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; }
    
}

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(
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值