本专栏基础资源来自唐老狮和siki学院,仅作学习交流使用,不作任何商业用途,吃水不忘打井人,谨遵教诲
本文只需要知道三个重点即可
EditorWindow类负责操作窗口
OnGUI方法是进入GUI窗口绘制的关键函数
GUILayout类负责绘制具体内容
EditorWindow
官方文档:EditorWindow - Unity 脚本 API
继承了 EditorWindow类的编辑器脚本, 你可以在这个窗口中添加自定义方法,如下图演示将创建一个名为Name,有一个任意内容label的窗口
public class Window : EditorWindow
{
[MenuItem("MyWindow/window1")]
private static void MyWindow()
{
Window w =EditorWindow.GetWindow<Window>("Name");
w.Show();
}
private void OnGUI() {
GUILayout.Label("任意内容");
}
}
EditorGUILayout
但是在具体绘制这一点,unity提供了一个新的类叫做EditorGUILayout,功能与GUILayout相似加入了自动布局功能
EditorGUILayout - Unity 脚本 API
举例:每种元素都用常用api
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
public class Window : EditorWindow
{
[MenuItem("MyWindow/window1")]
private static void MyWindow()
{
Window w =EditorWindow.GetWindow<Window>("Name");
w.Show();
}
//层级和标签
int layer;
string tag;
//选择枚举
enum E_AnyType{
a=1,
b=2,
c=4,
d=8,
//要将枚举中的变量定义为2的幂次方,原因是多选枚举时,是按照 或运算 将得出的结果记录下来
//比如a=1,二进制为0001,b=2,二进制为0010,或运算后结果为0011 = 3
//如果不按此规则,a=1,b=2,c=3,当a|b之后,得到的结果就变成了c,而不是多选a和b
}
E_AnyType single;
E_AnyType multiple;
//字符串选择对应数组值
int returnNum;
string[] options = { "op1", "op2", "op3", "op4" };
int[] result = { 1,2,3,4};
//关联资源
GameObject obj;
int value;//整形输入框
//折叠控件
bool isHide;
bool isGroupHide;
//滑动条
float slider;
int intSlider;
float min;
float max;
private void OnGUI() {
EditorGUILayout.LabelField("页签");
layer =EditorGUILayout.LayerField(layer);//return int
tag =EditorGUILayout.TagField("标签");//return sting
single = (E_AnyType)EditorGUILayout.EnumPopup("单选枚举",single);//return typeof(E_AnyType)
multiple = (E_AnyType)EditorGUILayout.EnumFlagsField("多选枚举",multiple);
returnNum = EditorGUILayout.IntPopup("字符串对应数组", returnNum, options, result);
EditorGUILayout.LabelField(returnNum.ToString());
EditorGUILayout.DropdownButton(new GUIContent("按钮"), FocusType.Passive);//return bool
isHide = EditorGUILayout.Foldout(isHide,"折叠控件 bool包裹内容");
if(isHide){
obj = EditorGUILayout.ObjectField("关联资源对象框", obj, typeof(GameObject), true) as GameObject;
value = EditorGUILayout.IntField("输入框", value);
}
isGroupHide = EditorGUILayout.BeginFoldoutHeaderGroup(isGroupHide,"折叠组控件 必须包括开始和结束");
EditorGUILayout.EndFoldoutHeaderGroup();
//开关和开关组类似于折叠控件 关键词:Toggle
slider =EditorGUILayout.Slider("滑动条",slider,0,1);
intSlider = EditorGUILayout.IntSlider("整型滑动条",intSlider,0,1);
//双块滑动条
EditorGUILayout.MinMaxSlider("双块",ref min,ref max,0,1);
EditorGUILayout.Space(10);//间隔框
//提示框
EditorGUILayout.HelpBox("提示框",MessageType.Warning);//MessageType选择类型
}
}