脚本语言:C#

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

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

1、Groups :

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

示例代码:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Test3 : MonoBehaviour {
  5.  
  6. // Use this for initialization
  7. void Start () {
  8. }
  9.  
  10. // Update is called once per frame
  11. void Update () {
  12. }
  13.  
  14. void OnGUI(){
  15. // 屏幕宽度和高度
  16. int screenWidth = Screen.width;
  17. int screenHeight = Screen.height;
  18.  
  19. // group组大小
  20. int groundWidth = ;
  21. int groundHeight = ;
  22.  
  23. // group组初始位置
  24. int groupx = (screenWidth - groundWidth) / ;
  25. int groupy = (screenHeight - groundHeight) / ;
  26.  
  27. GUI.BeginGroup( new Rect(groupx,groupy,groundWidth,groundHeight));
  28. GUI.Box( new Rect(,,groundWidth,groundHeight), "Level Select");
  29. if(GUI.Button( new Rect(,,,),"Level 1"))
  30. Debug.Log("Level 1");
  31. if(GUI.Button( new Rect(,,,),"Level 2"))
  32. Debug.Log("Level 2");
  33. if(GUI.Button(new Rect(,,,),"Level 3"))
  34. Debug.Log("Level 3");
  35. GUI.EndGroup();
  36.  
  37. // 改变group坐标,group组的位置随之改变
  38. groupx = (screenWidth - groundWidth) / ;
  39. groupy = (screenHeight - groundHeight) / ;
  40.  
  41. GUI.BeginGroup( new Rect(groupx,groupy,groundWidth,groundHeight));
  42. GUI.Box( new Rect(,,groundWidth,groundHeight), "Level Select");
  43.  
  44. if(GUI.Button( new Rect(,,,),"Level 1"))
  45. Debug.Log("Level 1");
  46. if(GUI.Button( new Rect(,,,),"Level 2"))
  47. Debug.Log("Level 2");
  48. if(GUI.Button(new Rect(,,,),"Level 3"))
  49. Debug.Log("Level 3");
  50. GUI.EndGroup();
  51. }
  52. }

Group

2、Button:

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

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

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class GUITest1 : MonoBehaviour {
  5.  
  6. // Use this for initialization
  7. void Start () {
  8. }
  9.  
  10. // Update is called once per frame
  11. void Update () {
  12. }
  13.  
  14. void OnGUI(){
  15. if ((Time.time % ) < ) {
  16. if( GUI.Button( new Rect(,,,),"Unity Button"))
  17. print("用户单击了按钮");
  18. }
  19. }
  20. }

Button1

按钮会闪烁显示;

(2)带图标按钮:

对应Main Camera:

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

示例代码:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Test2 : MonoBehaviour {
  5.  
  6. public Texture icon;
  7.  
  8. // Use this for initialization
  9. void Start () {
  10. }
  11.  
  12. // Update is called once per frame
  13. void Update () {
  14. }
  15.  
  16. void OnGUI(){
  17. if( GUI.Button( new Rect(,,,), new GUIContent(icon) ) )
  18. print("用户单击了按钮");
  19. }
  20. }

Button2

3、Box:

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

示例代码:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class GUITest4 : MonoBehaviour {
  5.  
  6. public Texture texture;
  7.  
  8. // Use this for initialization
  9. void Start () {
  10. }
  11.  
  12. // Update is called once per frame
  13. void Update () {
  14. }
  15.  
  16. void OnGUI()
  17. {
  18. // 指定为灰色颜色
  19. GUI.color = Color.gray;
  20. GUI.Box (new Rect (, , Screen.width * 0.5f, Screen.height * 0.5f), "This is a title");
  21. GUI.Box (new Rect (, , texture.width/, texture.height/), texture);
  22. }
  23. }

Box

4、Window:

  可拖动的窗口;

示例代码:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class GUITest5 : MonoBehaviour {
  5.  
  6. public Rect windowRect0 = new Rect(,,,);
  7.  
  8. // Use this for initialization
  9. void Start () {
  10. }
  11.  
  12. // Update is called once per frame
  13. void Update () {
  14. }
  15. void OnGUI()
  16. {
  17. //渲染窗口ID为0
  18. windowRect0 = GUILayout.Window(,windowRect0, DoMyWindow,"Draggable Window");
  19. }
  20.  
  21. void DoMyWindow(int windowID)
  22. {
  23. GUILayout.Label("This is a draggable window!");
  24. }
  25. }

window

5、GUILayout.beginHorizontal和GUILayout.beginVertical

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

示例代码:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class GUITest6 : MonoBehaviour {
  5. private string firstName = "First Name";
  6. private string lastName = "Last Name";
  7. private uint age = ;
  8. private bool submitted = false;
  9.  
  10. private Rect windowRect0;
  11.  
  12. void Start(){
  13. }
  14.  
  15. void Update(){
  16. }
  17.  
  18. void OnGUI()
  19. {
  20. var screenWidth = Screen.width;
  21. var screenHeight = Screen.height;
  22.  
  23. var windowWidth = ;
  24. var windowHeight = ;
  25. var windowX = (screenWidth - windowWidth) / ;
  26. var windowY = (screenHeight - windowHeight) / ;
  27.  
  28. //将窗口放置到屏幕中间
  29. windowRect0 = new Rect(windowX,windowY,windowWidth,windowHeight);
  30.  
  31. GUILayout.Window(,windowRect0,UserForm,"User information");
  32. }
  33.  
  34. void UserForm(int windowID)
  35. {
  36. GUILayout.BeginVertical();
  37.  
  38. //first name
  39. GUILayout.BeginHorizontal();
  40. GUILayout.Label("First Name",GUILayout.Width());
  41. firstName = GUILayout.TextField(firstName);
  42. GUILayout.EndHorizontal();
  43.  
  44. //last name
  45. GUILayout.BeginHorizontal();
  46. GUILayout.Label("Last Name",GUILayout.Width());
  47. lastName = GUILayout.TextField(lastName);
  48. GUILayout.EndHorizontal();
  49.  
  50. //Age
  51. GUILayout.BeginHorizontal();
  52. GUILayout.Label("Age",GUILayout.Width());
  53. string ageText = GUILayout.TextField(age.ToString());
  54. uint newAge = ;
  55. if( uint.TryParse(ageText, out newAge) )
  56. {
  57. age = newAge;
  58. }
  59. GUILayout.EndHorizontal();
  60.  
  61. if(GUILayout.Button("Submit"))
  62. {
  63. submitted = true;
  64. }
  65. if(GUILayout.Button("Reset"))
  66. {
  67. firstName = "First Name";
  68. lastName = "Last Name";
  69. age = ;
  70. submitted = false;
  71. }
  72. if(submitted)
  73. {
  74. GUILayout.Label("submitted!");
  75. }
  76. GUILayout.EndVertical();
  77. }
  78. }
 
6、HorizontalSlider:
  水平滚动条
示例代码:
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class GUITest7 : MonoBehaviour {
  5.  
  6. // Use this for initialization
  7. void Start () {
  8. }
  9.  
  10. // Update is called once per frame
  11. void Update () {
  12. }
  13.  
  14. private float masterVolume = 1.0f;
  15. private float sfxVolume = 1.0f;
  16.  
  17. void OnGUI()
  18. {
  19. int groupWidth = ;
  20. int groupHeight = ;
  21.  
  22. int screenWidth = Screen.width;
  23. int screenHeight = Screen.height;
  24.  
  25. int groupX = (screenWidth - groupWidth) / ;
  26. int groupY = (screenHeight - groupHeight) / ;
  27.  
  28. GUI.BeginGroup( new Rect(groupX,groupY,groupWidth,groupHeight));
  29. GUI.Box( new Rect(,,groupWidth,groupHeight),"Audio Settings");
  30.  
  31. GUI.Label( new Rect(,,,),"Master Volume");
  32. masterVolume = GUI.HorizontalSlider( new Rect(,,,), masterVolume,0.0f,1.0f);
  33. GUI.Label(new Rect(,,,),"(" + masterVolume.ToString("f2") + ")");
  34.  
  35. GUI.Label(new Rect(,,,),"Effect Volume");
  36. sfxVolume = GUI.HorizontalSlider(new Rect(,,,),sfxVolume,0.0f,1.0f);
  37. GUI.Label(new Rect(,,,),"(" + sfxVolume.ToString("f2") + ")");
  38.  
  39. GUI.EndGroup();
  40. }
  41. }

HorizontalSlider

7、VerticalSlider:

  竖直滚动条

示例代码:

  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class GUITest8 : MonoBehaviour {
  5.  
  6. // Use this for initialization
  7. void Start () {
  8.  
  9. }
  10.  
  11. // Update is called once per frame
  12. void Update () {
  13.  
  14. }
  15.  
  16. private float[] equalizerValues = new float[];
  17.  
  18. void OnGUI()
  19. {
  20. int groupWidth = ;
  21. int groupHeight = ;
  22.  
  23. int screenWidth = Screen.width;
  24. int screenHeight = Screen.height;
  25.  
  26. int groupX = (screenWidth - groupWidth) / ;
  27. int groupY = (screenHeight - groupHeight) / ;
  28.  
  29. GUI.BeginGroup(new Rect(groupX,groupY,groupWidth,groupHeight));
  30. GUI.Box(new Rect(,,groupWidth,groupHeight),"Equalizer");
  31.  
  32. for(int i = ; i < equalizerValues.Length; i++)
  33. {
  34. equalizerValues[i] = GUI.VerticalSlider(new Rect(i * + ,,,),equalizerValues[i],0.0f,1.0f);
  35. }
  36. GUI.EndGroup();
  37. }
  38. }

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. LINUX内核调度器+linux 内存

    http://www.cnblogs.com/tolimit/p/4303052.html

  2. XML解析技术研究(一)

      摘要:XML作为过去十年中出现的最流行的技术之一,得到了广泛的应用,而其中XML解析技术是XML应用的关键.本文介绍了XML解析技术的研究动向,分析和比较了4种XML解析技术的优劣,并归纳总结了应 ...

  3. spring beans源码解读之--BeanFactory进化史

    BeanFactory是访问bean容器的根接口,它是一个bean容器的基本客户端视图. 先让我们看看beanfactory的前生后世吧! beanFactory有四个重要的子接口: SimpleJn ...

  4. Android中多线程下载列表的封装实现(含进度反馈)

    来源:http://blog.csdn.net/u011638883/article/details/17347015 实现了一下Android中的文件多线程下载模块,支持自定义线程数.断点续传.下载 ...

  5. 使用CDN加载jQuery类库后备代码

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></ ...

  6. post和get的区别?

    1. get是从服务器上获取数据,post是向服务器传送数据.2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过H ...

  7. Fluent NHibernate之旅

    Fluent NHibernate 之旅 导航篇: [原创]Fluent NHibernate之旅开篇: [原创]Fluent NHibernate之旅二--Entity Mapping: [原创]F ...

  8. JSONModel的基本使用

    JSONModel 是一个库,它能智能并且快速的创建出数据 model,你可以在你的 iOS 项目或者 OSX 项目上使用它. 使用前准备 添加 JSONModel 到你的工程中 1.需要的环境: A ...

  9. amf0解释一下

    就简单记录一下省了以后忘了,amf0其实就几种数据格式的网络传输格式,比如数字,字符串,这些格式在传输的时候他给单独序列化了一下,主要支持以下这些: #define AMF0_NUMBER ((uin ...

  10. C# WinFrom 编写正则表达式验证类

    public class ValidationRegex { /// <summary> /// 正则表达式字符串 /// </summary> public static s ...