Unity EditorWindow 使用案例

UnityEditorWindow学习笔记

img

了解EditorWindow的生命周期:

OnEnable():当打开界面的时候调用
OnFocus():当该窗口被聚焦(点击该窗口)
OnGUI():当渲染UI的时候调用
OnSelectionChange():当选择发生更改时调用,选中的可选项(在Project和Hierarchy视图中)
OnLostFocus():从该窗口离开时调用(点击非窗口外其他地方)
OnInspectorUpdate():当属性界面更新时,几乎一直在更
OnHierarchyChange():当场景层次界面发生改变时调用");//在Hierarchy界面改变(增加、减少物体)
OnProjectChange():当项目发生更改时调用");//在Project视图删除、增加文件
OnDisable():当隐藏的时候调用
OnDestroy():当销毁的时候调用
OnValidate():当拖拽式赋值时调用

GUIStyle 用法示例https://www.jianshu.com/p/44078f1f07ef https://blog.csdn.net/RICKShaozhiheng/article/details/52304627

(该Style可以完成一些自定风格的EditorWindow)

GUILayout常用布局接口

GUILayoutOption:基本每个控件方法都有一个可选参数是GUILayoutOption[] Options 这是一个可以控制组件大小之类的选项,在GUILayout类中共有8个。 看名字应该就知道是设置什么的了。

GUILayout.Height() GUILayout.Width()

GUILayout.MaxHeight() GUILayout.MaxWidth()

GUILayout.MinHeight() GUILayout.MinWidth()

GUILayout.ExpandHeight() GUILayout.ExpandWidth()

GetWindow(一定要show,EditorWindow可以调窗口的最大最小等等)

[MenuItem("Extend Windows/MyWindow2")]
public static void ShowWindow()
{
    // 显示某个编辑器窗口。传参即是要显示的窗口类型(类名) 即调用谁的OnGUI 这里调用的时TestEditorWindow
    // 是否为独立窗口 窗口名字 是否聚焦(这个不是很确定)
    EditorWindow rThisWindow = EditorWindow.GetWindow(typeof(TestEditorWindow), false, "Test", false);
    rThisWindow.minSize = new Vector2(500,300);
    rThisWindow.maxSize = new Vector2(800,500);
    rThisWindow.Show();
}
GUILayout.FlexibelSpace //用法示例https://blog.csdn.net/u010331385/article/details/43560235  类似于组件GroupLayout
GUILayout.FlexibelSpace //空格
GUILayout.Label("label");//文本

请添加图片描述

GUILayout.Box(new GUIContent("testBox",texture),new []{GUILayout.Height(200),GUILayout.Width(200)});//Box

在这里插入图片描述

sliderVal = EditorGUILayout.Slider ("Slider", sliderVal, -1, 1);//Slider

在这里插入图片描述

scrollBarValue1 = GUILayout.HorizontalScrollbar(scrollBarValue1,0,0,500,new[]{GUILayout.Width(500)});//ScollBar
scrollBarValue2 = GUILayout.VerticalScrollbar(scrollBarValue2,0,0,200,new[]{GUILayout.Height(200)});

在这里插入图片描述

按钮类控件

普通Button

if (GUILayout.Button("changeColor",GUILayout.MaxWidth(100)))
{
    st.normal.textColor = new Color(Random.Range(0f,1f),Random.Range(0f,1f),Random.Range(0f,1f));
}

在这里插入图片描述
RepeatButton按下时才会为true

if (showBox) //按钮按下的时候这个Box才会显示出来
{
    GUILayout.Box(new GUIContent("testBox",img),new []{GUILayout.Height(200),GUILayout.Width(200)});
    EditorGUILayout.Space();
}

在这里插入图片描述

互斥按钮群体:

gridId = GUILayout.SelectionGrid(gridId, new[] {"1", "2", "3","4","5","6"}, 5);//第二个参数可以控制一行出现多少个按钮

在这里插入图片描述
ToolBar

toolbarid = GUILayout.Toolbar(toolbarid, new[] {"1", "2", "3"});

在这里插入图片描述

Fields

普通fields

GUILayout.TextField("TextField only one line");//单行Fields
EditorGUILayout.Space();
GUILayout.TextArea("TextArea can multiline \n second line \n third line");//多行Fields

在这里插入图片描述

密码形fields

password = GUILayout.PasswordField(password, '?');

在这里插入图片描述
颜色fields

curColor = EditorGUILayout.ColorField(curColor);

在这里插入图片描述

Toggle

isToggle = GUILayout.Toggle (isToggle,"Toggle");
isToggle2 = EditorGUILayout.Toggle("Toggle2", this.isToggle2);

在这里插入图片描述

弹选框类控件

多选弹框

curEnum=(test)EditorGUILayout.EnumFlagsField(curEnum);//test 枚举要符合2进制每一位对应的值 符合按位与的枚举
public enum test
{
    t1 = 1,
    t2 = 2,
    t3 = 4,
    t4 = 8
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VJYPEUoH-1634635775629)(C:\Users\gy\AppData\Roaming\Typora\typora-user-images\image-20211019152142969.png)]

互斥弹框枚举

public enum employee
{
    doctor,
    nurse,
    teacher,
    police
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rzIVosm6-1634635775630)(C:\Users\gy\AppData\Roaming\Typora\typora-user-images\image-20211019152205534.png)]

互斥弹框 int

popindex=EditorGUILayout.IntPopup("IntPopup", popindex, new[] {"1", "2","3","5"}, new[] {1, 2,3});//多余的5是选择不上的 初始会为5 若对其则为0

在这里插入图片描述

多选弹框 int

maskindex=EditorGUILayout.MaskField("Mask",maskindex,new []{"a","b","c","d"});//此时的maskIndex的规律我还没摸清楚

在这里插入图片描述

折叠功能:

foldout = EditorGUILayout.Foldout(foldout, "折叠Label");
if (foldout)
{
    val1=EditorGUILayout.IntField("Attribute1", val1);
    val2=EditorGUILayout.IntField("Attribute2", val2);
}

foldout2=EditorGUILayout.InspectorTitlebar(foldout2,GameObject.Find("Cube"));
if (foldout2)
{
    val3=EditorGUILayout.IntField("Attribute1", val3);
    val4=EditorGUILayout.IntField("Attribute2", val4);
}

在这里插入图片描述
示例图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xHExgdjN-1634635775632)(C:\Users\gy\AppData\Roaming\Typora\typora-user-images\image-20211019172830041.png)]

参考大佬:https://blog.csdn.net/qq_38275140/article/details/84778344

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值