Unity3D GUI学习

时间:2021-07-20 19:41:31

Unity3D内置有GUI,

首先,使用GUI实现一个按钮,并且点击实现触发,

	void OnGUI()
{
//GUI.Button (new Rect (10,10,50,50), "nihaoa ");
if(GUI.Button(new Rect (50, 50, 50, 50),"Button")) {
Debug.Log("wo shi yi ge an niu"); } }

 这里屏幕上会创建一个按钮,点击按钮,会出现下面那句话:

Unity3D GUI学习

文本输入框的使用:

注意这里的赋值要赋值给自己,不然每一帧显示,会把前面的值刷掉的

using UnityEngine;
using System.Collections; public class getbutton : MonoBehaviour { // Use this for initialization public Rect rec;
public string text;
void Start () {
text = "请输入";
} // Update is called once per frame
void Update () { } void OnGUI()
{ text = GUI.TextField(new Rect(0, 0, 100, 100), text); } }

  Unity3D GUI学习

复选框:

using UnityEngine;
using System.Collections; public class getbutton : MonoBehaviour { // Use this for initialization public bool toogbaleT = true;
public bool toogbaleM = false;
void Start () { } // Update is called once per frame
void Update () { } void OnGUI()
{ toogbaleT = GUI.Toggle(new Rect(0, 0, 50, 50), toogbaleT, "体育");
toogbaleM = GUI.Toggle(new Rect(55, 55, 50, 50), toogbaleM, "美术"); } }

可以实现,选择和取消的效果,每一次进行点击,都会刷新toogbaleT值来决定显示的效果:

Unity3D GUI学习

进度条的实现:

using UnityEngine;
using System.Collections; public class getbutton : MonoBehaviour { // Use this for initialization public float hsliaervalue = 0f; void Start () { } // Update is called once per frame
void Update () { } void OnGUI()
{ hsliaervalue = GUI.HorizontalSlider(new Rect(140, 210, 100, 30), hsliaervalue, 0, 10); } }

  效果图:

Unity3D GUI学习