Unity3D之GUI基础以及常用GUI控件使用

来源:互联网 发布:知乎怎么匿名提问 编辑:程序博客网 时间:2024/05/09 09:17

1.GUI基础


(1)GUI部分是每帧擦除重绘的,只应该在OnGUI中绘制GUI,按钮:GUILayout.Button(“Hello”); 只读标签:GUILayout.Label()


(2)修改控件的颜色:GUI.backgroundColor = Color.red;设置背景色,GUI.color设置前景色,影响OnGUI中之后的控件直到下次修改颜色为止。

 


2.GUILayout类是用于GUI自动布局的接口。

 

3.GUILayout布局



(1)GUILayout布局默认会伸展控件的尺寸。Button等控件支持params GUILayoutOption[] options可变参数数组,可以通过

GUILayout.Width(30)返回一个设置控件宽度的GUILayoutOption对象, Height()、 MinWidth()、MinHeight()等同理。例子:

 GUILayout.Button("Test",GUILayout.Width(500),GUILayout.Height(30))



(2)GUILayout采用线性布局,类似于StackPanel,默认是纵向布局。通过GUILayout.BeginHorizontal();开启和GUILayout.EndHorizontal()结束一个横向排列区域,同理BeginVertical() 、EndVertical()。文章出处狗刨学习网


(3)如果嫌控件太挤,可以使用GUILayout.Space(30);增加若干像素的间隙。

 

4.常用GUI控件


(1)密码框:pwd = GUILayout.PasswordField(pwd,‘*’),第二个参数为掩码字符,注意帧刷新的问题。


(2)Button只有鼠标抬起才会返回true,这样用Button做发射子弹就不能按下一直发射,可以使用RepeatButton,这样按下期间会一直返回true。案例:Button和RepeatButton实现游戏对象移动。


(3)Tab页: electedToolBarId = GUILayout.Toolbar(selectedToolBarId,new string[]{“装备”,“经验”,“队友”});  返回值为激活的按钮的序号。


(4)复选框:isMuted = GUILayout.Toggle(isMuted,“静音”);


(5)多行文本区: msgBody = GUILayout.TextArea(msgBody,GUILayout.Width(300),GUILayout.Height(300));


(6)滑动条:纵向, sliderValue = GUILayout.VerticalSlider(sliderValue,0,100);返回值为当前值,第二个参数为最小值,第二个为最大值。 HorizontalSlider()横向


(7)区域Area,相当于一个控件的盒子, Area中的控件跟着Area移动, BeginArea()开始一个区域,参数指定区域的大小和坐标, EndArea()结束区域;


(8)窗口,区域是没有边框和标题的,也不可以拖放。 GUILayout.Window(0, new Rect(50,50,200,200),AddWindow1,“我的窗口”);  第一个参数为窗口的编号,第二个为窗口大小,第三个为void WindowFunction(int windowId)委托,用来绘制窗口内容。


窗口拖拽,在WindowFunction的最后调用GUI.DragWindow()可以启用全屏拖放(给DragWindow传Rect参数可以设定可拖放的区域)。考虑帧刷新的问题,要在OnGUI把Window()的返回Rect保存才可以可拖动,Start()中设定初始位置。

 

代码如下:

using UnityEngine;
using System.Collections;

public class Cube1Control : MonoBehaviour {
public Texture texture;
public Texture2D texture2D;
public Texture2D texture2DActive;
public string userName;
public string password;
public string remark;
public bool isSuccess;
public int select=0;
public bool toggle1 = false;
public Texture2D bug1;
public Texture2D bug2;
public float h;
public Vector2 vector2;
Rect rect1 = new Rect(0, 10, 300, 500);
Rect rect2 = new Rect(600, 10, 300, 500);
public int selGridId = 0;
string[] selString = new string[] { "Grid1", "Grid2", "Grid3", "Grid4", "Grid5" };

// Use this for initialization
void Start () {
h = 40;
}

// Update is called once per frame
void Update () {

}

void win(int id)
{

GUI.Button(new Rect(10, 120, 150, 50), "点击按钮");

//使用DragWindow启用窗口拖动
GUI.DragWindow();

}

void OnGUI()
{
#region GUILayout布局
////GUILayout采用线性布局,类似于StackPanel,默认是纵向布局。通过GUILayout.BeginHorizontal();
////开启和GUILayout.EndHorizontal()结束一个横向排列区域,同理BeginVertical() 、EndVertical()。
//GUILayout.BeginHorizontal();
//GUILayout.Button("Button1", GUILayout.Width(100), GUILayout.Height(50));
//GUILayout.Button("Button2", GUILayout.Width(100), GUILayout.Height(50));
//GUILayout.EndHorizontal();

//GUILayout.BeginVertical();
////如果嫌控件太挤,可以使用GUILayout.Space(30);增加若干像素的间隙。
//GUILayout.Space(30);//Button3和Button1在垂直方向上面就会增加30个像素的间隙
//GUILayout.Button("Button3", GUILayout.Width(100), GUILayout.Height(50));
//GUILayout.Button("Button4", GUILayout.Width(100), GUILayout.Height(50));
//GUILayout.EndVertical();
#endregion

#region 常用的GUI控件
#region GUI.Button
//GUI.Button(new Rect(20, 20, 150, 30), "这是一个文字按钮");

////绘制纹理按钮
//GUI.Button(new Rect(20, 60, 150, 30), texture);//texture是在unity上面Script脚本上面拖上图片进行赋值的
////绘制一个带图片和文字按钮
//GUIContent guic = new GUIContent("按钮", texture);
//GUI.Button(new Rect(20, 100, 150, 30), guic);

////设置按钮的样式
//GUIStyle guis = new GUIStyle();
//guis.fontSize = 23;
//guis.alignment = TextAnchor.MiddleCenter;

////设置状态样式
//GUIStyleState guiss = new GUIStyleState();
//guiss.textColor = Color.white;
//guiss.background = texture2D;//设置按钮背景图片,texture2D在编辑器上拖图片赋值
//guis.normal = guiss;//设置按钮正常显示的状态
//GUIStyleState guissActive = new GUIStyleState();
//guissActive.textColor = Color.white;
//guissActive.background = texture2DActive;//设置按钮背景图片,texture2D在编辑器上拖图片赋值
//guis.active = guissActive;//设置鼠标按下去按钮上显示的状态
//guis.hover = guissActive;//设置鼠标放在按钮上显示的状态
//if (GUI.Button(new Rect(20, 140, 150, 30), "样式按钮", guis))//点击后返回true
//{
// Debug.Log("点击了按钮");
//} 
#endregion

#region GUI.Label
//GUI.color = Color.red;//全局设置颜色,设置后后面的控件都变为红色,直到重新设置颜色
//GUI.Label(new Rect(20, 180, 100, 50), "label1");
//GUI.color = Color.blue;
//GUI.Label(new Rect(20, 200, 100, 50), "label2"); 
#endregion

#region GUI.TextField GUI.PasswordField GUI.TextArea
//userName = GUI.TextField(new Rect(10, 10, 100, 30), userName);
//password = GUI.PasswordField(new Rect(10, 50, 100, 30), password,'*');
//remark = GUI.TextArea(new Rect(10, 100, 100, 30),remark);
//if (GUI.Button(new Rect(10,150,50,30),"登录"))
//{
// Debug.Log(userName + "-"+password+"-"+remark);
// if (userName.Equals("admin")&&password.Equals("123"))
// {
// isSuccess = true;
// }
// else
// {
// isSuccess = false;
// }
//}
//if (isSuccess)
//{
// GUI.Label(new Rect(10, 200, 100, 30), "登录成功!");
//}
//else
//{
// GUI.Label(new Rect(10, 200, 100, 30), "登录失败!");
//}
#endregion

#region GUI.Toolbar GUI.Toggle GUI.HorizontalSlider
//Tab页,返回值为激活的按钮的序号,三个按钮并排,select为0选中第一个按钮
//select = GUI.Toolbar(new Rect(0, 0, 300, 50), select, new string[] { "功能一", "功能二", "功能三" });
//Debug.Log(select);

//单选按钮
//GUIStyle gs = new GUIStyle();
//GUIStyleState gss = new GUIStyleState();
//gss.textColor = Color.white;
//gs.normal = gss;
//gs.active = gss;
//GUIContent contenxt = new GUIContent("开关", bug1);
//if (toggle1)
//{
// contenxt.image = bug2;
//}
//// toggle = GUI.Toggle(new Rect(10, 10, 100, 30), toggle, "是否开启声音");
//toggle1 = GUI.Toggle(new Rect(10, 10, 50, 50), toggle1, contenxt, gs);
//GUI.Label(new Rect(10, 80, 100, 30), toggle1 + "");

//水平拖动的Slider,h为Slider赋值
//h = GUI.HorizontalSlider(new Rect(0, 0, 100, 100), h, 0, 100);
//Debug.Log(h); 
#endregion

#region GUI.BeginScrollView GUI.BeginGroup GUI.Window GUI.SelectionGrid
//开始滚动视图
// public static Vector2 BeginScrollView(Rect position, Vector2 scrollPosition, Rect viewRect, bool alwaysShowHorizontal, bool alwaysShowVertical);
//position 用于滚动视图在屏幕上矩形的位置
//scrollPosition 用来显示滚动位置
//viewRect 滚动视图内使用的矩形
//vector2 = GUI.BeginScrollView(new Rect(0, 0, 200, 200), vector2, new Rect(0, 0, 200, 200), true, true);
//GUI.Button(new Rect(0, 0, 50, 50),"Button");
//GUI.EndScrollView();

//开始组 将控件都放在一组中,只要组变动,里面的控件都跟着变
//GUI.BeginGroup(new Rect(10, 100, 200, 400));
//GUI.Label(new Rect(10, 100, 100, 30), "群组视图1");
//GUI.Button(new Rect(10, 130, 100, 30), "按钮");
//GUI.EndGroup();
//GUI.BeginGroup(new Rect(200, 0, 300, 400));
//GUI.Label(new Rect(10, 100, 100, 30), "群组视图2");
//GUI.Button(new Rect(10, 130, 100, 30), "按钮");
//GUI.EndGroup();

//弹出窗口
//必须要把窗口的位置设置成全局变量,窗口里面内容在回调函数里面写
//rect1 = GUI.Window(0, rect1, win, "窗口");
//rect2 = GUI.Window(1, rect2, win, "窗口");

//选择表格
//selGridId = GUI.SelectionGrid(new Rect(10, 10, 300, 200), selGridId, selString, 2);
//Debug.Log(selGridId); 
#endregion

#region GUILayout.BeginArea
//区域就是无边框的窗口,Button控件随着区域移动
//GUILayout.BeginArea(new Rect(0, 50, 200, 200), "Area");
//GUI.Button(new Rect(0,0,100,50),"Button");
//GUILayout.EndArea(); 
#endregion

#endregion

}
}

0 0
原创粉丝点击