IMGUI之GUI组件学习
废话少说,这是学习教程写下的笔记 对应学习的教程地址
GUI组件和GUILayout基本一致,但是需要设置绘制的位置和大小
所以这篇只是过一遍代码,不再详细讲解。
Rect的使用及其的参数说明
Rect mRect = new Rect(0, 60, 200, 20);
四个参数分别为x、y、width、height,(x,y)就是位置,基于左上角的锚点,width和height就是宽和高,这四个参数能表达出控件的位置和大小。
Label 文本标签
Rect mRect = new Rect(0, 60, 200, 20);
void OnGUI()
{
GUI.Label(mRect, "文本");
}
TextField 文本输入框
Rect mRect = new Rect(0, 60, 200, 20);
string mTextValue = "";
void OnGUI()
{
mTextValue = GUI.TextField(mRect, mTextValue);
}
TextArea 文本输入区
Rect mRect = new Rect(0, 60, 200, 80);
string mTextValue = "";
void OnGUI()
{
mTextValue = GUI.TextArea(mRect, mTextValue);
}
PasswordField 密码框
Rect mRect = new Rect(0, 60, 200, 20);
string mPasswordValue = "";
void OnGUI()
{
mPasswordValue = GUI.PasswordField(mRect, mPasswordValue, '*');
}
Button 按钮(RepeatButton不写了)
void OnGUI()
{
if (GUI.Button(mRect, "这是一个按钮"))
{
Debug.Log("这里是按下之后要做的事");
}
}
Toggle 勾选项
bool mBoolValue = false;
void OnGUI()
{
mBoolValue = GUI.Toggle(mRect, mBoolValue,"点击勾选我");
}
Toolbar工具栏
Rect mRect = new Rect(0, 60, 200, 20);
int mToolBarIndex = 0;
void OnGUI()
{
mToolBarIndex =
GUI.Toolbar(mRect,
mToolBarIndex,
new[] { "选项1", "选项2", "选项3", "选项4" });
}
Box框
Rect mRect = new Rect(0, 60, 200, 20);
void OnGUI()
{
GUI.Box(mRect, "这是一个Box框");
}
ScrollView 滚动视图
Vector2 scrollPosition;
Rect mScrollRect = new Rect(0, 0, 200, 100);
Rect mViewRect = new Rect(0, 0, 200, 300);
Rect mTextRect = new Rect(0, 20, 200, 100);
Rect mBoxRect = new Rect(0, 0, 200, 300);
void OnGUI()
{
scrollPosition = GUI.BeginScrollView(mScrollRect, scrollPosition, mViewRect);
GUI.TextArea(mText
Rect, "测试用的Text框,不返回值");
GUI.Box(mBoxRect, "这是一个Box框");
GUI.EndScrollView();
}
参数看备注……第一个参数和第三个参数不要写反了
Slider拖动条
Rect mHorizontalSliderRect = new Rect(0, 0, 200, 300);
Rect mVerticalSliderRect = new Rect(30, 100, 200, 300);
float mLeftValue = 0;
float mRightValue = 1f;
float mSliderValue = 0;
void OnGUI()
{
mSliderValue = GUI.HorizontalSlider(mHorizontalSliderRect, mSliderValue, mLeftValue, mRightValue);
mSliderValue = GUI.VerticalSlider(mVerticalSliderRect, mSliderValue, mRightValue, mLeftValue);
}
拖动条分为水平和垂直,需要有一个变量保存拖动条的值。
Group 组
Rect mGrouprect = new Rect(0, 0, 200, 300);
Rect mTextRect = new Rect(0, 100, 200, 100);
Rect mBoxRect = new Rect(0, 50, 200, 100);
void OnGUI()
{
GUI.BeginGroup(mGrouprect,"这是一个组");
GUI.TextArea(mTextRect, "测试用的Text框,不返回值");
GUI.Box(mBoxRect, "这是一个Box框");
GUI.EndGroup();
}
将一组控件用Group包裹起来(个人觉得类似winform里的panel)
文档地址
GUILayout文档地址
|