UnityGUI

来源:互联网 发布:乐清知临与公立哪个好 编辑:程序博客网 时间:2024/04/29 04:40

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.tooltipGUI.Button (new Rect (10,10,100,20), new GUIContent ("Click me", "This is the tooltip"));// This line reads and displays the contents of GUI.tooltipGUI.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.

 

原创粉丝点击