纳金网

标题: Unity3D 学习从简单开始-GUI探索 [打印本页]

作者: Cici_coach    时间: 2017-2-23 11:15
标题: Unity3D 学习从简单开始-GUI探索
  今天为大家分享一下,如何快速认识unity3d 的原生GUI,其实说到GUI,它是Unity中一个强大的脚本API,可以快速的创建的简单的菜单UI,所以我们有必要了解一下它是如何使用的。然而文章极其的简单,相信一看便知其意。学习交流:575561285.
   GUI 官方文档:http://www.ceeger.com/Script/GUI/GUI.html
   1.新建一个unity3d 测试项目,新建GUITest.cs组件,考虑把GUI常用的函数做一个列举。
1.png
   2.GUITest.cs 组件代码如下:
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;

  4. public class GUITest : MonoBehaviour {


  5.         public string passwordToEdit = "My Password";
  6.         public string stringToEdit = "Hello World\nI've got 2 lines...";
  7.         public string stringToEditField = "Hello World";
  8.         private bool toggleTxt = false;

  9.         public int toolbarInt = 0;
  10.         public string[] toolbarStrings = new string[] {"Toolbar1", "Toolbar2", "Toolbar3"};

  11.         public float vSbarValue;
  12.         public float vSliderValue = 0.0F;

  13.         void OnGUI ()
  14.         {
  15.                 //1.Label :显示文本内容
  16.                 GUI.Label (new Rect (100,0,400,50), "This is the text string for a Label Control");
  17.                 //2.Button :显示一个菜单按钮
  18.                 GUI.Button (new Rect (100,70, 200, 50), "This is a Button");
  19.                 //3.Box :绘制纹理
  20.                 GUI.Box(new Rect(0,0,Screen.width,Screen.height),"This is a title");
  21.                 //4.PasswordField :显示密码框
  22.                 passwordToEdit = GUI.PasswordField(new Rect(100, 140, 200, 20), passwordToEdit, "*"[0], 25);
  23.                 //5.RepeatButton :重复按钮
  24.                 GUI.RepeatButton(new Rect(100, 210, 200, 50), "This is a RepeatButton");
  25.                 //6.TextArea :显示多行文本
  26.                 stringToEdit = GUI.TextArea(new Rect(100, 280, 200, 60), stringToEdit, 200);
  27.                 //7.TextField :显示文本字段
  28.                 stringToEdit = GUI.TextField (new Rect(100, 350, 200, 20), stringToEditField, 25);
  29.                 //8.Toggle :开关按钮
  30.                 toggleTxt = GUI.Toggle(new Rect(100, 420, 100, 30), toggleTxt, "A Toggle text");
  31.                 //9.Toolbar :工具栏
  32.                 toolbarInt = GUI.Toolbar(new Rect(100, 490, 250, 30), toolbarInt, toolbarStrings);
  33.                 //10.tooltip :工具提示
  34.                 GUI.Button(new Rect(100, 560, 100, 20), new GUIContent("Click me", "This is the tooltip"));
  35.                 GUI.Label(new Rect(300, 560, 100, 40), GUI.tooltip);
  36.                 //11.VerticalScrollbar :垂直滚动条
  37.                 vSbarValue = GUI.VerticalScrollbar (new Rect(100, 630, 100, 60), vSbarValue, 1.0F, 10.0F, 0.0F);
  38.                 //12.VerticalSlider :垂直滑动条
  39.                 vSliderValue = GUI.VerticalSlider (new Rect(400, 630, 100, 60), vSliderValue, 10.0F, 0.0F);

  40.         }
  41. }
复制代码
   3.最后运行看整体效果哈!
3.png    






欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5