今天接着上一篇GUI的分享,也简单讲一下GUILayout的实际使用。
其实说到GUI和GUILayout的关系,它们有着异曲同工之处。
好吧!废话不多讲!我们就直奔主题吧!
它们的区别:
(1)它们一样是游戏的界面的布局大神,但是使用GUI绘制界面时,需要设置控件整体显示的区域
(Rect()方法) ,使用极其的不灵活,一般还会随着内容长度的变化发生改变,最后便会直接会影响的到显示的效果,甚至还会造成区域重叠的现象;而GUILayout来制作界面,它会自动帮我计算控件需要显示的区域,保证他们不会重叠。
(2)从某种意义上来说相对于GUI,GUILayout使用起来更方便。
GUILayout 官方文档:http://www.ceeger.com/Script/GUILayout/GUILayout.html
1.新建一个unity3d 测试项目,新建GUILayoutTest.cs组件,考虑把GUILayout常用的函数显示出来;
2. GUILayoutTest.cs 组件代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GUILayoutTest : MonoBehaviour {
public string passwordToEdit = "My Password";
public string stringToEdit = "Hello World\nI've got 2 lines...";
public string stringToEditField = "Hello World";
private bool toggleTxt = false;
public int toolbarInt = 0;
public string[] toolbarStrings = new string[] {"Toolbar1", "Toolbar2", "Toolbar3"};
public float vSbarValue;
public float vSliderValue = 0.0F;
public Rect windowRect = new Rect(300, 400, 120, 50);
void OnGUI ()
{
//1.Label :显示文本内容
GUILayout.Label ("This is the text string for a Label Control");
//2.Button :显示一个菜单按钮
GUILayout.Button ("This is a Button");
//3.Box :绘制纹理
GUILayout.Box("This is a title");
//4.PasswordField :显示密码框
passwordToEdit = GUILayout.PasswordField(passwordToEdit, "*"[0], 25);
//5.RepeatButton :重复按钮
GUILayout.RepeatButton("This is a RepeatButton");
//6.TextArea :显示多行文本
stringToEdit = GUILayout.TextArea(stringToEdit, 200);
//7.TextField :显示文本字段
stringToEdit = GUILayout.TextField (stringToEditField, 25);
//8.Toggle :开关按钮
toggleTxt = GUILayout.Toggle(toggleTxt, "A Toggle text");
//9.Toolbar :工具栏
toolbarInt = GUILayout.Toolbar(toolbarInt, toolbarStrings);
//10.tooltip :工具提示
GUILayout.Button(new GUIContent("Click me", "This is the tooltip"));
GUILayout.Label(GUI.tooltip);
//11.VerticalScrollbar :垂直滚动条
vSbarValue = GUILayout.VerticalScrollbar (vSbarValue, 1.0F, 10.0F, 0.0F);
//12.VerticalSlider :垂直滑动条
vSliderValue = GUILayout.VerticalSlider (vSliderValue, 10.0F, 0.0F);
}
}
3.当然上面我只列举一部分主要常用的函数,如果大家感兴趣的话,可以继续深入探讨。例如改变布局整体的布局开始创建位置GUILayout.BeginArea (new Rect (200,200,100,100))等等;
4.最后直接运行unity编辑器看效果啦!
学习交流 : 575561285