本文转自Unity Connect博主郡墙
Editor GUI 编辑器入门,常见对GUI界面编写及快速实现对介绍,附示例代码,hand by hand ,just do it.
基础GUI
文本类
示例代码
private void BaseAPI()
{
GUILayout.Label(mLableStr);
EditorGUILayout.SelectableLabel(mLabelField);
EditorGUILayout.LabelField("LabelField", mLabelField);
mTextField = EditorGUILayout.TextField("TextField", mTextField);
mIntField = EditorGUILayout.IntField("IntField", mIntField);
mFloatField = EditorGUILayout.FloatField("FloatField", mFloatField);
mTextArea = EditorGUILayout.TextArea(mTextArea, GUILayout.Height(40));
GUIStyle tempFontStyle = new GUIStyle();
tempFontStyle.normal.background = null;
tempFontStyle.normal.textColor = Color.yellow;
tempFontStyle.fontStyle = FontStyle.BoldAndItalic;
tempFontStyle.fontSize = 18;
GUILayout.Label(mCustomStr, tempFontStyle);
}
效果预览
密码输入样式
示例代码
private void PasswordField()
{
using (new EditorGUILayout.HorizontalScope())
{
GUILayout.Label("PasswordField_1");
mPasswordField_1 = GUILayout.PasswordField(mPasswordField_1, '*');
}
mPasswordField_2 = EditorGUILayout.PasswordField("PasswordField_2", mPasswordField_2);
}
效果预览
滑动条
示例代码
private void Slider()
{
mSlider = EditorGUILayout.Slider("Slider", mSlider, 0.0f, 49.9f);
EditorGUILayout.MinMaxSlider(new GUIContent("MinMaxSlider"), ref mMinValue, ref mMaxValue, 0, 100);
}
效果预览
勾选框
示例代码
private void Toggle()
{
mToggle = GUILayout.Toggle(mToggle, "Toggle");
mToggle = EditorGUILayout.Toggle(new GUIContent("Toggle"), mToggle);
}
效果预览
Toolbar
示例代码
private void Toolbar()
{
mToolbarIndex = GUILayout.Toolbar(mToolbarIndex, mToolbarOptions);
EditorGUILayout.LabelField("Selected Toobar", mToolbarOptions[mToolbarIndex]);
}
效果预览
可选条件框的类型
示例代码
private void Popup()
{
mEnumPopup = (HaloEnum)EditorGUILayout.EnumPopup("EnumPopup", mEnumPopup);
mEnumMaskField = (HaloEnum)EditorGUILayout.EnumMaskField("EnumMaskField", mEnumMaskField);
mPopupIndex = EditorGUILayout.Popup("Popup", mPopupIndex, mPopupOptions);
using (new EditorGUILayout.HorizontalScope())
{
mIntPopupIndex = EditorGUILayout.IntPopup("IntPopup", mIntPopupIndex, mIntPopupOptions, mIntPopupSizes);
EditorGUILayout.LabelField("Selected IntPopup", mIntPopupIndex.ToString());
}
mTagField = EditorGUILayout.TagField("TagField", mTagField);
mLayerField = EditorGUILayout.LayerField("LayerField", mLayerField);
mMaskFieldIndex = EditorGUILayout.MaskField("MaskField", mMaskFieldIndex, mMaskFieldOptions);
mColorField = EditorGUILayout.ColorField("ColorField", mColorField);
}
效果预览
Unity常见基础数据类型
示例代码
private void UnityProperty()
{
Color tempOrgColor = GUI.backgroundColor;
using (new EditorGUILayout.VerticalScope(GUI.skin.box))
{
//修改背景颜色;
GUI.backgroundColor = Color.gray;
mVector3Field = EditorGUILayout.Vector3Field("Vector3Field", mVector3Field);
GUI.backgroundColor = tempOrgColor;
}
mObjectField = EditorGUILayout.ObjectField(new GUIContent("ObjectField"), mObjectField, typeof(GameObject), true);
using (new EditorGUILayout.VerticalScope(GUI.skin.box))
{
//修改背景颜色;
GUI.backgroundColor = Color.green;
mRectField = EditorGUILayout.RectField("RectField", mRectField);
GUI.backgroundColor = tempOrgColor;
}
}
效果预览
其他常用功能
效果预览
示例代码
private void OtherEdiotorUtility()
{
if (GUILayout.Button(new GUIContent("弹出系统提示消息", "调用系统级别的提示信息")))
{
ShowNotification(new GUIContent("这是来自 ShowNotification 的弹窗消息"));
}
bool tempButtonChange = false;
if (GUILayout.Button(new GUIContent("显示二级窗口", "窗口内的二级弹窗"))) { mDrawSecWindow = true; tempButtonChange = true; }
if (GUILayout.Button(new GUIContent("绘制图形", "绘制图形的示例"))) { mDrawGraphWindow = true; tempButtonChange = true; }
if (tempButtonChange && (mDrawGraphWindow || mDrawSecWindow))
{
mSecWindowRect = new Rect((int)position.width >> 2, (int)position.height >> 2, (int)position.width >> 1, (int)position.height >> 1);
}
EditorGUILayout.Space();
using (new EditorGUILayout.VerticalScope(GUI.skin.box))
{
GUILayout.Label("EditorUtility相关");
if (string.IsNullOrEmpty(mOpenFilePanel) == false)
{
EditorGUILayout.LabelField("OpenFilePanel SelectPath", mOpenFilePanel);
}
using (new EditorGUILayout.HorizontalScope(GUI.skin.scrollView))
{
if (GUILayout.Button("OpenFilePanel"))
{
mOpenFilePanel = EditorUtility.OpenFilePanel("OpenFilePanel", "Assets", "*");
}
if (GUILayout.Button("RevealInFinder"))
{
EditorUtility.RevealInFinder(mOpenFilePanel);
}
}
if (GUILayout.Button("DisplayDialog"))
{
bool tempResult = EditorUtility.DisplayDialog("这是一个系统二次确认窗口", "文本内容", "确认", "取消");
string tempTip = tempResult ? "你按下了二次确认" : "你取消了二次确认";
ShowNotification(new GUIContent(tempTip));
}
}
}
原文链接:https://connect.unity.com/p/editor-gui-bian-ji-qi-ru-men?app=true
戳上方链接下载Unity官方app,发现更多干货,在线技术答疑交友社区,还可下载本文演示代码附件哦~
网友评论