脚本语言:C#

附上一张图说明Unity GUI编程中可用的控件:(可能有遗漏)

下面列出一些例子来说明:

1、Groups :

  在固定Layout模式中起到组织可用项的功能,它让你在屏幕的一个区域中包含多个控件。把定义的控件放在GUI.BeginGroup()和 GUI.EndGroup()这对函数中间,所有控件的位置坐标都以Groups的0坐标为起点,假如更改了group坐标,那么内部的控件也会跟随改变。

示例代码:

using UnityEngine;
using System.Collections; public class Test3 : MonoBehaviour { // Use this for initialization
void Start () {
} // Update is called once per frame
void Update () {
} void OnGUI(){
// 屏幕宽度和高度
int screenWidth = Screen.width;
int screenHeight = Screen.height; // group组大小
int groundWidth = ;
int groundHeight = ; // group组初始位置
int groupx = (screenWidth - groundWidth) / ;
int groupy = (screenHeight - groundHeight) / ; GUI.BeginGroup( new Rect(groupx,groupy,groundWidth,groundHeight));
GUI.Box( new Rect(,,groundWidth,groundHeight), "Level Select");
if(GUI.Button( new Rect(,,,),"Level 1"))
Debug.Log("Level 1");
if(GUI.Button( new Rect(,,,),"Level 2"))
Debug.Log("Level 2");
if(GUI.Button(new Rect(,,,),"Level 3"))
Debug.Log("Level 3");
GUI.EndGroup(); // 改变group坐标,group组的位置随之改变
groupx = (screenWidth - groundWidth) / ;
groupy = (screenHeight - groundHeight) / ; GUI.BeginGroup( new Rect(groupx,groupy,groundWidth,groundHeight));
GUI.Box( new Rect(,,groundWidth,groundHeight), "Level Select"); if(GUI.Button( new Rect(,,,),"Level 1"))
Debug.Log("Level 1");
if(GUI.Button( new Rect(,,,),"Level 2"))
Debug.Log("Level 2");
if(GUI.Button(new Rect(,,,),"Level 3"))
Debug.Log("Level 3");
GUI.EndGroup();
}
}

Group

2、Button:

  用来绘制响应单击事件的按钮;

(1)普通按钮示例代码:

using UnityEngine;
using System.Collections; public class GUITest1 : MonoBehaviour { // Use this for initialization
void Start () {
} // Update is called once per frame
void Update () {
} void OnGUI(){
if ((Time.time % ) < ) {
if( GUI.Button( new Rect(,,,),"Unity Button"))
print("用户单击了按钮");
}
}
}

Button1

按钮会闪烁显示;

(2)带图标按钮:

对应Main Camera:

Icon是Test2脚本中定义的public Texture 变量,直接把图片拉至Icon处即可产生对应关系。

示例代码:

using UnityEngine;
using System.Collections; public class Test2 : MonoBehaviour { public Texture icon; // Use this for initialization
void Start () {
} // Update is called once per frame
void Update () {
} void OnGUI(){
if( GUI.Button( new Rect(,,,), new GUIContent(icon) ) )
print("用户单击了按钮");
}
}

Button2

3、Box:

  Box控件用来绘制带有边框背景的文字或图片。

示例代码:

using UnityEngine;
using System.Collections; public class GUITest4 : MonoBehaviour { public Texture texture; // Use this for initialization
void Start () {
} // Update is called once per frame
void Update () {
} void OnGUI()
{
// 指定为灰色颜色
GUI.color = Color.gray;
GUI.Box (new Rect (, , Screen.width * 0.5f, Screen.height * 0.5f), "This is a title");
GUI.Box (new Rect (, , texture.width/, texture.height/), texture);
}
}

Box

4、Window:

  可拖动的窗口;

示例代码:

using UnityEngine;
using System.Collections; public class GUITest5 : MonoBehaviour { public Rect windowRect0 = new Rect(,,,); // Use this for initialization
void Start () {
} // Update is called once per frame
void Update () {
}
void OnGUI()
{
//渲染窗口ID为0
windowRect0 = GUILayout.Window(,windowRect0, DoMyWindow,"Draggable Window");
} void DoMyWindow(int windowID)
{
GUILayout.Label("This is a draggable window!");
}
}

window

5、GUILayout.beginHorizontal和GUILayout.beginVertical

  默认情况下,当使用GUILayout函数时所有的视图中的组件都会竖直排列。可以使用GUILayout.BeginHorizontal和GUILayout.EndHorizontall静态函数使控件相邻排放.每出现一次GUILayout.BeginVertical必须有对应的GUILayout.EndVertical与其对应,每出现一次GUILayout.BeginHorizontal则必须有对应的GUILayout.EndHorizontal与其对应;

示例代码:

using UnityEngine;
using System.Collections; public class GUITest6 : MonoBehaviour {
private string firstName = "First Name";
private string lastName = "Last Name";
private uint age = ;
private bool submitted = false; private Rect windowRect0; void Start(){
} void Update(){
} void OnGUI()
{
var screenWidth = Screen.width;
var screenHeight = Screen.height; var windowWidth = ;
var windowHeight = ;
var windowX = (screenWidth - windowWidth) / ;
var windowY = (screenHeight - windowHeight) / ; //将窗口放置到屏幕中间
windowRect0 = new Rect(windowX,windowY,windowWidth,windowHeight); GUILayout.Window(,windowRect0,UserForm,"User information");
} void UserForm(int windowID)
{
GUILayout.BeginVertical(); //first name
GUILayout.BeginHorizontal();
GUILayout.Label("First Name",GUILayout.Width());
firstName = GUILayout.TextField(firstName);
GUILayout.EndHorizontal(); //last name
GUILayout.BeginHorizontal();
GUILayout.Label("Last Name",GUILayout.Width());
lastName = GUILayout.TextField(lastName);
GUILayout.EndHorizontal(); //Age
GUILayout.BeginHorizontal();
GUILayout.Label("Age",GUILayout.Width());
string ageText = GUILayout.TextField(age.ToString());
uint newAge = ;
if( uint.TryParse(ageText, out newAge) )
{
age = newAge;
}
GUILayout.EndHorizontal(); if(GUILayout.Button("Submit"))
{
submitted = true;
}
if(GUILayout.Button("Reset"))
{
firstName = "First Name";
lastName = "Last Name";
age = ;
submitted = false;
}
if(submitted)
{
GUILayout.Label("submitted!");
}
GUILayout.EndVertical();
}
}
 
6、HorizontalSlider:
  水平滚动条
示例代码:
using UnityEngine;
using System.Collections; public class GUITest7 : MonoBehaviour { // Use this for initialization
void Start () {
} // Update is called once per frame
void Update () {
} private float masterVolume = 1.0f;
private float sfxVolume = 1.0f; void OnGUI()
{
int groupWidth = ;
int groupHeight = ; int screenWidth = Screen.width;
int screenHeight = Screen.height; int groupX = (screenWidth - groupWidth) / ;
int groupY = (screenHeight - groupHeight) / ; GUI.BeginGroup( new Rect(groupX,groupY,groupWidth,groupHeight));
GUI.Box( new Rect(,,groupWidth,groupHeight),"Audio Settings"); GUI.Label( new Rect(,,,),"Master Volume");
masterVolume = GUI.HorizontalSlider( new Rect(,,,), masterVolume,0.0f,1.0f);
GUI.Label(new Rect(,,,),"(" + masterVolume.ToString("f2") + ")"); GUI.Label(new Rect(,,,),"Effect Volume");
sfxVolume = GUI.HorizontalSlider(new Rect(,,,),sfxVolume,0.0f,1.0f);
GUI.Label(new Rect(,,,),"(" + sfxVolume.ToString("f2") + ")"); GUI.EndGroup();
}
}

HorizontalSlider

7、VerticalSlider:

  竖直滚动条

示例代码:

using UnityEngine;
using System.Collections; public class GUITest8 : MonoBehaviour { // Use this for initialization
void Start () { } // Update is called once per frame
void Update () { } private float[] equalizerValues = new float[]; void OnGUI()
{
int groupWidth = ;
int groupHeight = ; int screenWidth = Screen.width;
int screenHeight = Screen.height; int groupX = (screenWidth - groupWidth) / ;
int groupY = (screenHeight - groupHeight) / ; GUI.BeginGroup(new Rect(groupX,groupY,groupWidth,groupHeight));
GUI.Box(new Rect(,,groupWidth,groupHeight),"Equalizer"); for(int i = ; i < equalizerValues.Length; i++)
{
equalizerValues[i] = GUI.VerticalSlider(new Rect(i * + ,,,),equalizerValues[i],0.0f,1.0f);
}
GUI.EndGroup();
}
}

VerticalSlider

  

  推荐Unity下的2D界面用NGUI来做,更方便。

 

Unity GUI编程的更多相关文章

  1. JAVA GUI编程学习笔记目录

    2014年暑假JAVA GUI编程学习笔记目录 1.JAVA之GUI编程概述 2.JAVA之GUI编程布局 3.JAVA之GUI编程Frame窗口 4.JAVA之GUI编程事件监听机制 5.JAVA之 ...

  2. 1.JAVA之GUI编程概述

          下列内容为本人看毕向东老师java视频教程学习笔记! JAVA GUI图形用户界面编程: Windows 操作系统提供两种操作方式:                             ...

  3. 2.JAVA之GUI编程布局

    布局管理器 容器中的组件排放方式,就是布局 常见的布局管理器: **************************************************** 1.FlowLayout(流式 ...

  4. 3.JAVA之GUI编程Frame窗口

    创建图形化界面思路: 1.创建frame窗体: 2.对窗体进行基本设置: 比如大小.位置.布局 3.定义组件: 4.将组件通过add方法添加到窗体中: 5.让窗体显示,通过setVisible(tur ...

  5. 4.JAVA之GUI编程事件监听机制

    事件监听机制的特点: 1.事件源 2.事件 3.监听器 4.事件处理 事件源:就是awt包或者swing包中的那些图形用户界面组件.(如:按钮) 事件:每一个事件源都有自己特点有的对应事件和共性事件. ...

  6. 5.JAVA之GUI编程窗体事件

    我们回顾下第三篇时的内容: 在3.JAVA之GUI编程Frame窗口中窗体是无法直接关闭的,想要关闭须进程管理器结束进程方式关掉. 现在我们就来解决下这个问题. ******************* ...

  7. 6.JAVA之GUI编程Action事件

    功能:单击一个按钮实现关闭窗口: import java.awt.*; import java.awt.event.*; public class StudyAction { // 定义该图形所需的组 ...

  8. 7.JAVA之GUI编程鼠标事件

    鼠标事件: 功能: 1.基本窗体功能实现 2.鼠标移动监听,当鼠标移动到按钮上时,触发打印事件. 3.按钮活动监听,当按钮活动时,触发打印事件. 4.按钮被单击时触发打印事件. 源码如下: impor ...

  9. 8.JAVA之GUI编程键盘码查询器

    程序使用说明: 1.本程序由于是java代码编写,所以运行需安装jdk并配置好环境变量. 2. 复制java代码到记事本内,另存为Keyboard_events.java: 3.复制批处理代码到记事本 ...

随机推荐

  1. Java语言基础(三)

    Java语言基础(三) 一.    补码 (1).之所以有补码是因为要考虑成本 就是造计算机的成本 (2).下面让我们分析一下补码 以四位补码为例 <1> 高位是符号位,它决定其是正数还是 ...

  2. windows和linux双系统删除linux

    装了Windows和linux双系统的朋友,在后期要删除linux是个比较头痛的问题,因为MBR已经被linux接管,本文的目的是如何在windows 和linux双系统下,简单,完美地卸载linux ...

  3. Missing artifact com.sun:tools:jar:1.5.0

    http://java-suddy.iteye.com/blog/1821871(解决了我的问题)

  4. 强大的Core Image框架,各种滤镜处理图像

    首先介绍一下Core Image,他是一个很强大的图像处理框架,他可以让你简单的应用各种滤镜来处理图像,比如说色相,饱和度,亮度等等...他是运用GPU(CPU)实时地处理图像数据和视频的帧.而且Co ...

  5. CSS3美化表单控件

    表单的默认控件在不同的浏览器中的样式不同,用户体验很差.用CSS3可以实现表单控件的美化,可以提供更好的用户体验.不足之处就是浏览器的兼容性问题. 一.下拉控件 效果图: 下拉控件的布局结构: < ...

  6. WebService开发步骤

    WebService原理什么的百度很多我就说了,无非就是提供一个接口放在服务端,客户端实现这个接口就可以拿到自己需要的东西. 现在简单说一下用myEclipse来实现服务端和客户端.另一种WebSer ...

  7. 64位系统下System32文件系统重定向

    前言 因为一次偶然的机会,需要访问系统目录“C:/Windows/System32“文件夹下的内容,使用的测试机器上预装了win7 64系统.在程序运行中竟然发生了该文件路径不存在的问题!!通过查看网 ...

  8. 原生js实现回到顶部

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  9. asp.net MVC 从其它项目复制过来的Area里面的Controllers文件读取不到

    从其实项目复制过来的Controllers,在访问时显示不存在文件 检查一下对应的area里面的AreaRegistration文件的命名空间是否一致

  10. 响应式十日谈第一日:使用 rem 设置文字大小

    上面回顾: 在序言中我们已经提到了响应式的一些基本理念,比如: 响应式网页不仅仅是响应不同类型的设备,而且需要响应不同的用户需求.响应式的初衷是为了让信息更好的传递交流,让所有人无障碍的获取信息,同时 ...