Unity3D GUI学习

本文介绍了如何在Unity3D中使用GUI系统创建交互元素,包括按钮、文本输入框、复选框及进度条等组件的基本用法。通过具体代码示例展示了各组件的设置与响应操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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");
			
		}

	}

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

文本输入框的使用:

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

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);

	}
	
}

  

复选框:

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值来决定显示的效果:

进度条的实现:

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);

	}
	
}

  效果图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值