Unity基础 GUI编程

  脚本语言: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 = 120;
        int groundHeight = 150;

        // group组初始位置
        int groupx = (screenWidth - groundWidth) / 2;
        int groupy = (screenHeight - groundHeight) / 2;  

        GUI.BeginGroup( new Rect(groupx,groupy,groundWidth,groundHeight));
        GUI.Box( new Rect(0,0,groundWidth,groundHeight), "Level Select");
        if(GUI.Button( new Rect(10,30,100,30),"Level 1"))
            Debug.Log("Level 1");
        if(GUI.Button( new Rect(10,70,100,30),"Level 2"))
            Debug.Log("Level 2");
        if(GUI.Button(new Rect(10,110,100,30),"Level 3"))
            Debug.Log("Level 3");
        GUI.EndGroup();  

        // 改变group坐标,group组的位置随之改变
        groupx = (screenWidth - groundWidth) / 4;
        groupy = (screenHeight - groundHeight) / 4;  

        GUI.BeginGroup( new Rect(groupx,groupy,groundWidth,groundHeight));
        GUI.Box( new Rect(0,0,groundWidth,groundHeight), "Level Select");  

        if(GUI.Button( new Rect(10,30,100,30),"Level 1"))
            Debug.Log("Level 1");
        if(GUI.Button( new Rect(10,70,100,30),"Level 2"))
            Debug.Log("Level 2");
        if(GUI.Button(new Rect(10,110,100,30),"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 % 2) < 1) {
            if( GUI.Button( new Rect(10,10,120,100),"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(20,20,200,120), 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 (10, 10, Screen.width * 0.5f, Screen.height * 0.5f), "This is a title");
        GUI.Box (new Rect (150, 170, texture.width/4, texture.height/4), texture);
    }
}

Box

4、Window:

  可拖动的窗口;

示例代码:

using UnityEngine;
using System.Collections;

public class GUITest5 : MonoBehaviour {

    public Rect windowRect0 = new Rect(20,20,150,0);

    // Use this for initialization
    void Start () {
    }

    // Update is called once per frame
    void Update () {
    }
    void OnGUI()
    {
        //渲染窗口ID为0
        windowRect0 = GUILayout.Window(0,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 = 0;
    private bool submitted = false;  

    private Rect windowRect0;  

    void Start(){
    }  

    void Update(){
    }

    void OnGUI()
    {
        var screenWidth = Screen.width;
        var screenHeight = Screen.height;  

        var windowWidth = 300;
        var windowHeight = 180;
        var windowX = (screenWidth - windowWidth) / 2;
        var windowY = (screenHeight - windowHeight) / 2;  

        //将窗口放置到屏幕中间
        windowRect0 = new Rect(windowX,windowY,windowWidth,windowHeight);  

        GUILayout.Window(0,windowRect0,UserForm,"User information");
    }  

    void UserForm(int windowID)
    {
        GUILayout.BeginVertical();  

        //first name
        GUILayout.BeginHorizontal();
        GUILayout.Label("First Name",GUILayout.Width(80));
        firstName = GUILayout.TextField(firstName);
        GUILayout.EndHorizontal();  

        //last name
        GUILayout.BeginHorizontal();
        GUILayout.Label("Last Name",GUILayout.Width(80));
        lastName = GUILayout.TextField(lastName);
        GUILayout.EndHorizontal();  

        //Age
        GUILayout.BeginHorizontal();
        GUILayout.Label("Age",GUILayout.Width(80));
        string ageText = GUILayout.TextField(age.ToString());
        uint newAge = 0;
        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 = 0;
            submitted = false;
        }
        if(submitted)
        {
            GUILayout.Label("submitted!");
        }
        GUILayout.EndVertical();
    }
}

Unity基础 GUI编程

时间: 2024-10-12 18:50:11

Unity基础 GUI编程的相关文章

Java基础——GUI编程(四)

继前面的,这篇主要记录菜单和文件对话框的两个小练习,来熟悉一下方法的应用,一些简单的逻辑和Swing的相关简介,以及关于GUI基础的记录. 一.创建一个菜单 import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.event.ActionEvent; import java.

C#编写Unity基础GUI之控件布局

1.GUILayout控件 1 void OnGUI() 2 { 3 GUILayout.Button("这是GUILayout按钮"); 4 } 效果: 会自动调整控件大小,自动定位在屏幕左上角: 2.组视图 1 void OnGUI() 2 { 3 GUI.BeginGroup(new Rect(Screen.width / 2, Screen.height / 2, 500, 500)); 4 5 GUI.Button(new Rect(0,0,100,100),"按钮

C#编写Unity基础GUI之按钮控件

基本方法: public static bool Button(Rect position, GUIContent content); public static bool Button(Rect position, string text); public static bool Button(Rect position, Texture image); public static bool Button(Rect position, GUIContent content, GUIStyle

C#编写Unity基础GUI之动态窗口

1.创建窗口 1 //声明窗口位置大小 2 private Rect pos = new Rect(10,60,120,50); 3 4 //回调函数 5 void Wincallback(int id) 6 { 7 if(GUI.Button(new Rect(10,20,100,20),"窗口内按钮")){ 8 print("按钮在窗口内被单击"); 9 } 10 } 11 12 void OnGUI() 13 { 14 pos = GUI.Window(0,

C#编写Unity基础GUI之控件-2

1.文本输入框 1 public string text; 2 3 void OnGUI(){ 4 text = GUI.TextField(new Rect(140, 140, 100, 40), text); 5 6 if (GUI.Button(new Rect(10, 10, 100, 50), text)) 7 { 8 print("用户单击了按钮"); 9 } 10 } 效果: 2.开关按钮控件 1 public bool toggleBool; 2 3 void OnGU

Java基础——GUI编程(三)

接着前两篇学习笔记,这篇主要介绍布局管理器和对话框两部分内容. 一.布局管理器 先拿一个小例子来引出话题,就按照我们随意的添加两个按钮来说,会产生什么样的效果,看执行结果. import java.awt.Button; import java.awt.Frame; public class Test25 { public static void main(String[] args) { Frame f = new Frame("布局管理器"); f.setSize(300, 400

C#编写Unity基础GUI之按钮控件GUILayout控件

GUILayout布局可以自动排列相关控件,例如: 1 void OnGUI() 2 { 3 GUILayout.Button("这一个GUILayout按钮"); 4 } 效果: 可以将控件的大小自动缩放成适应大小,从屏幕左上角开始排列,例如: 1 void OnGUI() 2 { 3 GUILayout.Button("这是个GUILayout按钮"); 4 GUILayout.Button("这也是个GUILayout按钮"); 5 } 效

java基础学习总结——GUI编程(二)

永不放弃,一切皆有可能!!! 只为成功找方法,不为失败找借口! java基础学习总结——GUI编程(二) 一.事件监听 测试代码一: 1 package cn.javastudy.summary; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 6 public class TestActionEvent { 7 public static void main(String args[]) { 8 Frame f = new Frame("

java基础学习总结——GUI编程(一)

永不放弃,一切皆有可能!!! 只为成功找方法,不为失败找借口! java基础学习总结——GUI编程(一) 一.AWT介绍 所有的可以显示出来的图形元素都称为Component,Component代表了所有的可见的图形元素,Component里面有一种比较特殊的图形元素叫Container,Container(容器)在图形界面里面是一种可以容纳其它Component元素的一种容器,Container本身也是一种Component的,Container里面也可以容纳别的Container. Cont