前言
文章主要记录下常用的用过的组件,记录内容为函数名以及较为简略的注释,以便在需要时可以快速查阅到,但是具体如何使用需要根据经验或者查阅文档,所以文章不详细解释各个参数意义,不介绍其他重写的函数,使用时可以查看定义,然后根据实际情况选择需要的函数使用,参数具体可以参考Unity文档中心,不过大多可以根据命名推出代表的意义。官方文档链接:Unity User Manual 2021.3 (LTS) - Unity 手册
1.基础组件
a.不可输入的文本框
GUILayout.Label(string text, GUIStyle style, params GUILayoutOption[] options)
参数介绍: text-显示的内容 ;style-文本显示的风格,这里不做介绍,需要了解可直接浏览“其他相关”标签;options-组件的尺寸等信息,同样,需要了解可直接浏览“其他相关”标签
b.可输入的文本框
GUILayout.TextField(string text, params GUILayoutOption[] options)
参数介绍:text-输入框已经输入的内容,options-组件的尺寸等信息(需要了解可直接浏览“其他相关”标签),返回值是输入框已经输入的内容。需要注意的是需要将输入内容赋值后再显示在框内,不然会造成修改无效情况,用法
string textInput;
public void DrawContents()
{
textInput = GUILayout.TextField(textInput);
}
c.空行/列
GUILayout.Space(float pixels)
参数介绍:pixels-要空余的间距,会根据当前布局 水平/垂直 自动匹配方向空行
d.按钮
GUILayout.Button(string text, params GUILayoutOption[] options)
参数介绍:text-按钮上显示的内容,options-组件的尺寸等信息(需要了解可直接浏览“其他相关”标签),返回为按钮是否被点击,用法
public void DrawContents()
{
if (GUILayout.Button("ButtonText"))
{
//按钮被点击
}
}
e.复选框
GUILayout.Toggle(bool value, string text, params GUILayoutOption[] options)
参数介绍:value-复选框当前选择结果;text-复选框显示的文本内容;options-组件的尺寸等信息(需要了解可直接浏览“其他相关”标签),,用法
bool isSelect;
public void DrawContents()
{
isSelect = GUILayout.Toggle(isSelect,"ToggleText");
}
持续更新中...
2.进阶组件
a.窗体
EditorWindow.GetWindow<T>(string title) where T : EditorWindow
参数介绍:title-窗口标题,T-自定义的窗口类,需要继承EditorWindow,界面显示在生命周期函数OnGUI()中编写。返回值为获取到的窗口实例,如为创建会自动创建,显示窗口调用成员函数show()即可。该函数重写函数较多,可参考文档根据需要使用。用法
static void ShowPngWindow()
{
CompressWindowBase window = EditorWindow.GetWindow<CompressWindowBase>(title: "标题");
window.Show();
}
public class CompressWindowBase : EditorWindow
{
}
b.水平布局
GUILayout.BeginHorizontal();
//布局中的内容
GUILayout.EndHorizontal();
主要用于布局对齐时使用,两个函数之间的组件会以水平对齐布局
c.垂直布局
GUILayout.BeginVertical();
//*****
GUILayout.EndVertical();
同水平布局
d.滑动区域
GUILayout.BeginScrollView(Vector2 scrollPosition, bool alwaysShowHorizontal, bool alwaysShowVertical, params GUILayoutOption[] options)
参数介绍:scrollPosition-滑动区域当前滑到的位置;alwaysShowHorizontal-水平方向滑动条是否常显;alwaysShowVertical-垂直方向滑动条是否常显;options-组件的尺寸等信息(需要了解可直接浏览“其他相关”标签),返回值为当前滑动区域所在位置。用法
Vector2 scrollViewPos;
public void DrawContents()
{
scrollViewPos = GUILayout.BeginScrollView(scrollViewPos, alwaysShowHorizontal: false, alwaysShowVertical: false);
GUILayout.EndScrollView();
}
e.下拉菜单-Int
EditorGUILayout.IntPopup(int selectedValue, string[] displayedOptions, int[] optionValues, params GUILayoutOption[] options)
参数介绍:selectedValue-当前选中的值;displayedOpetions-显示的内容数组;optionValues-与前面数组一一对应,表示选中内容所对应的值;options-组件的尺寸等信息(需要了解可直接浏览“其他相关”标签),,返回值为当前选中的值,用法
string[] descArr = new string[] {"低质量","中质量","高质量" };
int[] optionArr = new int[] { 0, 50, 99 };
int selectValue = 0;
public void DrawContents()
{
selectValue = EditorGUILayout.IntPopup(selectValue, descArr, optionArr);
}
f.下拉菜单-Enum
EditorGUILayout.EnumPopup(Enum selected, params GUILayoutOption[] options)
参数介绍:selected-当前选中的枚举值 ;options-组件的尺寸等信息(需要了解可直接浏览“其他相关”标签),,返回值为当前选中的值,需要类型转换后赋值,用法(这里用的源定义的贴图压缩方式)
TextureImporterFormat selectedValue = TextureImporterFormat.RGBA32;
public void DrawContents()
{
selectedValue = (TextureImporterFormat)EditorGUILayout.EnumPopup(selectedValue);
}
g.下拉菜单-string
EditorGUILayout.Popup(int selectedIndex, string[] displayedOptions, GUIStyle style, params GUILayoutOption[] options)
参数介绍:selectIndex-当前选中的索引;displayOptions-显示的内容数组;options-组件的尺寸等信息(需要了解可直接浏览“其他相关”标签),。用法
string[] descArr = new string[] {"低质量","中质量","高质量" };
int[] optionArr = new int[] { 0, 50, 99 };
int selectIndex = 0;
int selectValue;
public void DrawContents()
{
selectIndex = EditorGUILayout.Popup(selectIndex,descArr);
selectValue = optionArr[selectIndex];
}
h.弹窗
EditorUtility.DisplayDialog(string title, string message, string ok, [DefaultValue("\"\"")] string cancel)
参数介绍:title-标题;message-内容;ok-确认按钮显示的内容;cancle-取消按钮显示的内容;返回值为确认按钮是否被点击,用法
public void DrawContents()
{
if (EditorUtility.DisplayDialog("提示", "内容", "确定","取消"))
{
//点击确认之后的操作
}
}
i.Scope
当布局太多时,可以用这个把一块内容现在在一个容器内,用的较少,可以用原生的,也可以自己继承父类写一个,原生的需要rect参数,可以通过子类实现各种效果,主要用于修改一个区域内的效果。下面以修改区域内全局着色颜色为例,用法
public class CustomScope : GUI.Scope
{
private readonly Color oldColor;
public CustomScope(Color color)
{
oldColor = GUI.color;
GUI.color = color;//创建的时候先存储当前全局着色颜色,再把区域内的着色颜色修改为目标色
}
protected override void CloseScope()
{
GUI.color = oldColor;//创建完成将储存的颜色取出赋值给全局着色颜色
}
}
public void DrawContents()
{
using (new CustomScope(Color.red))
{
//*****
}
}
j.进度条弹窗
//有取消按钮的进度条弹窗
EditorUtility.DisplayCancelableProgressBar(string title, string info, float progress)
//无取消按钮的进度条弹窗
EditorUtility.DisplayProgressBar(string title, string info, float progress)
//关闭进度条弹窗
EditorUtility.ClearProgressBar()
参数介绍:两个函数的参数是一样的,区别在于返回值不同。title-弹窗标题;info-弹窗进度条下显示的信息;progress-进度条当前进度。上图前者返回值为bool类型,代表有没有点击取消按钮,后者没有返回值,不可操控。
关于进度条弹窗的使用这里以有取消按钮的为例。有两种写法,一种是在循环中调用显示进度条,需要注意的是,在循环结束以及点击了取消按钮后,要关闭进度条弹窗,不然弹窗会在最外层,点击不了其他视图(菜单栏可点击)。第二种写法是将进度条的显示更新放在editor的update中,同样注意使用完要从update中删除以及关闭弹窗,代码如下
//方式一
void ProgressBarMethod()
{
int index = 0;
int max = 1000;
for (index = 0; index <= max; index++)
{
//**************
if (EditorUtility.DisplayCancelableProgressBar("标题", $"{index}/{max}",(float)index / max))
{
break;
}
}
EditorUtility.ClearProgressBar();
}
//方式二
protected void Operation()
{
AddProgressBar(true);
}
protected void AddProgressBar(bool add)
{
if (add)
{
EditorApplication.update += UpdateProgressBar;
}
else
{
EditorApplication.update -= UpdateProgressBar;
EditorUtility.ClearProgressBar();
}
}
int index = 0;
int max = 1000;
public void UpdateProgressBar()
{
bool cancle = EditorUtility.DisplayCancelableProgressBar(("标题", $"{index}/{max}",(float)index / max));
ProgressBar2Method(index);
index++;
if (cancle || index >= SetCount)
{
AddProgressBar(false);
}
}
void ProgressBar2Method(int index)
{
//********
}
方式二较方式一的区别在于是将索引index的更新放到了editor的update里面,看起来使用起来较方式一麻烦,两种方式实际效果的区别暂时没有去研究,看起来效果差不多,之后发现有区别的话再详细研究下。
持续更新中...
其他相关
a.style
上文中很多函数都有style参数,style即显示规则,可以用系统原定义的,也可以根据需求自己定义。在GUISkin下有许多默认的style,使用时调用GUI.skin.****即可。自定义style时,不能在声明时构造,需要在具体函数中构造,声明周期中即可,个人习惯放在EditorWindow的OnEnable中,用法
public GUIStyle titleStyle;
public GUIStyle descStyle;
private void OnEnable()
{
titleStyle = new GUIStyle() {
fixedHeight = 40,
fontSize = 30,
fontStyle = FontStyle.Normal,
alignment = TextAnchor.MiddleCenter,
normal = new GUIStyleState() { textColor = Color.white },
padding = new RectOffset(10, 10, 0, 0),
};
descStyle = new GUIStyle() {
alignment = TextAnchor.MiddleCenter,
padding = new RectOffset(10, 10, 0, 0),
wordWrap = true,
normal = new GUIStyleState() { textColor = Color.white }
};
}
这里定义了两个style,用到的参数不多,大概解释一下。fixedHeight-组件高度;fontsize-字体大小;fontStyle-加粗/倾斜这些;aligment-文本对齐方式,居中,左上,右下等等;normal-常态下文本显示,其中可以设置贴图,字体颜色等;padding-内间距,文本距离框体的间距。style下参数有很多,可以参考文档根据自己的需求去设计。有参构造可以传入其他style,此时可以在传入的style基础上做部分修改。
b.GUILayoutOption
主要限制组件的尺寸,有width,minWidth,maxWidth等,顾名思义即组件长度,最小长度,最大长度,比较容易理解,用时可以多重限制,调用方法
GUILayout.Width(100)
//其他类似,参数都是float值
c.GUILayout.FlexibleSpace()
插入灵活的空白元素,不使用stayle时可以用该api进行排版对齐,比如需要一个居中的非填满窗口的按钮,可以这样写,左对齐右对齐同理
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button("Add", GUILayout.MinWidth(500)))
{
}
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
持续更新中...
总结
如前言所说,本文主要是记录各个组件的基础使用方法,基础函数,以保证需要时可以快速查到,具体的使用方式要根据应用场景去查看Unity的文档,各个函数以及参数都有具体解释。