UnityEditorWindow学习笔记

了解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()
{
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
GUILayout.FlexibelSpace
GUILayout.Label("label");

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

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

scrollBarValue1 = GUILayout.HorizontalScrollbar(scrollBarValue1,0,0,500,new[]{GUILayout.Width(500)});
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)
{
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");
EditorGUILayout.Space();
GUILayout.TextArea("TextArea can multiline \n second line \n third line");

密码形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)]](https://img-blog.csdnimg.cn/cc3a41f96f2f456e8d832fa7955238c5.png)
互斥弹框枚举
public enum employee
{
doctor,
nurse,
teacher,
police
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rzIVosm6-1634635775630)(C:\Users\gy\AppData\Roaming\Typora\typora-user-images\image-20211019152205534.png)]](https://img-blog.csdnimg.cn/0abdb11ff4334ee3b66a7726a5469f2b.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://img-blog.csdnimg.cn/9527006f2540456188533e6893837f62.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBASmFzb27vvJ8xMw==,size_20,color_FFFFFF,t_70,g_se,x_16)
参考大佬:https://blog.csdn.net/qq_38275140/article/details/84778344
|