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()
{
// 显示某个编辑器窗口。传参即是要显示的窗口类型(类名) 即调用谁的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
}
互斥弹框枚举
public enum employee
{
doctor,
nurse,
teacher,
police
}
互斥弹框 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);
}
示例图:
参考大佬:https://blog.csdn.net/qq_38275140/article/details/84778344