UnityGUI

1.OnGUI()函数每一帧被调用一次(只要包含它的脚本是被启用的),就像Update()函数那样。

2.GUI.Box不可点击,GUI.Button可点击。

GUI.Button只在鼠标按下按钮之后松开时(松开时也必须放在按钮上)返回true。

3.OnGUI函数每一帧调用一次,在调用的每一帧,若该函数内有创建控件的逻辑时则创建控件,无则自动销毁控件,无需我们添加别的控制代码

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour {

	void OnGUI () {
		if (Time.time % 2 < 1) {
			if (GUI.Button (new Rect (10,10,200,20), "Meet the flashing button")) {
				print ("You clicked me!");
			}
		}
	}
}

这是Button闪烁的功能。在Time.time%2>=1时,控件不在屏幕上显示。
4.创建控件可用两个类:GUI,GUILayout

5.所有UnityGUI控件均工作在屏幕空间(Screen Space),该屏幕空间对应于播放器的像素分辨率。

6.UnityGUI的坐标系统是基于屏幕左上角的,如上例中的new Rect(10,10,200,20),这个矩形的左上角位置相对于屏幕左上角位置坐标为(10,10),其宽为200,高为20,右下角位置为相对于屏幕左上角位置坐标为(210,30).

7.可以使用Screen.width和Screen.height属性来获得播放器中屏幕空间的可用大小。

8.

显示文本,用string

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour {

	void OnGUI () {
		GUI.Label (new Rect (0,0,100,50), "This is the text string for a Label Control");
	}

}


 

显示图片,用Texture2D

public Texture2D controlTexture;
  ...

void OnGUI () {
	GUI.Label (new Rect (0,0,100,50), controlTexture);
}

同时显示图片和文本,或者tooltip,可以用GUIContent对象

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour {

	public Texture2D icon;

	void OnGUI () {
		GUI.Box (new Rect (10,10,100,50), new GUIContent("This is text", icon));
	}

}

 

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour {

	void OnGUI () {
		// This line feeds "This is the tooltip" into GUI.tooltip
		GUI.Button (new Rect (10,10,100,20), new GUIContent ("Click me", "This is the tooltip"));

		// This line reads and displays the contents of GUI.tooltip
		GUI.Label (new Rect (10,40,100,20), GUI.tooltip);
	}

}


 

using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour {

	public Texture2D icon;

	void OnGUI () {
		GUI.Button (new Rect (10,10,100,20), new GUIContent ("Click me", icon, "This is the tooltip"));
		GUI.Label (new Rect (10,40,100,20), GUI.tooltip);
	}

}


9.控件类型

Label :non-interactive

 

 

 

1.guitexture
2.gui
3.guilayout


2.gui

GUI controls
The OnGUI() function gets called every frame as long as the containing script is enabled - just like the Update() function.
Since the OnGUI() code gets called every frame?
Since the OnGUI() code gets called every frame, you don't need to explicitly create or destroy GUI controls. The line that

declares the Control is the same one that creates it. If you need to display Controls at specific times, you can use any

kind of scripting logic to do so.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UnityGUI是Unity游戏引擎中的一个GUI系统,它是一种基于对象的图形用户界面系统,用于在游戏中创建交互式的用户界面。它允许开发者创建按钮、文本框、滑块等交互元素,并通过事件和回调函数来处理用户的输入和交互。 UnityGUI的源码提供了UnityGUI系统的实现细节,可以用于了解和学习UnityGUI的工作原理。通过阅读源码,开发者可以理解UnityGUI是如何创建、渲染和处理UI元素的,以及它是如何与游戏场景和其他组件进行交互的。 在UnityGUI的源码中,可以找到一些核心的类和函数,比如GUI类、GUILayout类和GUIStyle类等。GUI类提供了一系列静态函数来创建和绘制UI元素,比如按钮、标签和文本框。GUILayout类则提供了一组函数来实现自动布局,以便更方便地创建复杂的UI布局。GUIStyle类则包含了一些样式信息,比如字体、颜色和边框等。 通过查看源码,可以了解到UnityGUI的绘制原理和性能优化技巧。比如,可以看到UnityGUI是如何使用批处理技术将多个UI元素合并为一次渲染调用,以减少渲染开销。还可以了解到UnityGUI是如何处理用户的输入事件,并通过回调函数来实现相应的响应。 总之,UnityGUI的源码是一个宝贵的学习资源,可以帮助开发者更好地理解和使用UnityGUI系统。通过深入研究源码,开发者可以从中汲取经验和技巧,提高UI设计和开发的能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值