您的位置:首页 > 移动开发 > Unity3D

01.Unity GUI基础

2015-01-30 15:15 274 查看
using UnityEngine;
using System.Collections;

public class GUI : MonoBehaviour
{
private string strl;
private string pwd;
private int selectedToolBarld;
private bool isMuted;
private string msgBody;
private float value;
private Rect windowRect;
private bool showWindow;
// Use this for initialization
void Start()
{
//Debug.Log("from start");//start方法只执行一次
//start方法中一般都是写一些初始化的东西
strl = "";
pwd = ""; //记得初始化  不然会出现Object reference not set to an instance of an object
selectedToolBarld = 0;
isMuted = false;
msgBody = "这里是多行文本框,你点个回车试试";
value = 0f;
showWindow = false;
windowRect = new Rect(300, 300, 100, 50);
}

// Update is called once per frame
void Update()
{
//Debug.Log("from update");//update方法每一帧执行一次

}

void OnGUI()
{
//文本域
GUILayout.Label("文本域");

//开启一个横向排列区域
GUILayout.BeginHorizontal();
GUILayout.Label("用户名:");
//文本框
//文本框这样直接写是不会出现东西的
//帧:每一帧都会擦除上一帧绘制的内容,并将内容重新绘制
//GUILayout.TextField("文本框");
strl = GUILayout.TextField(strl,GUILayout.Width(100));
//结束一个横向排列区域
GUILayout.EndHorizontal();

//开启一个横向排列区域
GUILayout.BeginHorizontal();
GUILayout.Label("密码:");
//密码框
pwd = GUILayout.PasswordField(pwd,'*',GUILayout.Width(100));
//结束一个横向排列区域
GUILayout.EndHorizontal();

//按钮
GUILayout.Button("我是按钮", GUILayout.Width(100));

//Tab页
selectedToolBarld = GUILayout.Toolbar(selectedToolBarld, new string[] { "装备","经验","队友"});
switch (selectedToolBarld)
{
case 0: showLabel(selectedToolBarld);break;
case 1: showLabel(selectedToolBarld); break;
case 2: showLabel(selectedToolBarld); break;
case 3: showLabel(selectedToolBarld); break;
default:
break;
}

//复选框
isMuted = GUILayout.Toggle(isMuted, "静音");

//多行文本框
msgBody = GUILayout.TextArea(msgBody);

//滑动条
//开启一个横向排列区域
GUILayout.BeginHorizontal();
GUILayout.Label("垂直滑动条"+value);
value = GUILayout.VerticalSlider(value, 0f, 100f);
value = GUILayout.VerticalScrollbar(value, 10f, 800f, 1f);
//结束一个横向排列区域
GUILayout.EndHorizontal();

//绘制窗口
if (GUILayout.Button("绘制窗口"))
{
showWindow = true;
}
if (showWindow)
{
windowRect = GUILayout.Window(1, windowRect, AddWindow, "设备");//所有关于GUI绘制的代码,都要直接或间接的写到OnGUI方法中
}
}

private void AddWindow(int id)
{
//id表示窗口的编号,可以让一个脚本中弹出多个窗口,这些窗口可以共用一个回调函数
if (id == 1)
{
if (GUILayout.Button("选择武器,关闭窗口"))
{
showWindow = false;//关闭,其实就是不再绘制
}

}
//GUI.DragWindow();//new Rect(0,0,1000,1000)
}

private void showLabel(int selectedToolBarld)
{
GUILayout.Label("我是按钮" + selectedToolBarld, GUILayout.Width(100));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: