版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
GUI控件(二)
Label控件
label控件为标签的意思,是一种很常见的控件
构造函数
构造Label控件的函数有以下几种
public static void Label(Rect position, string text);
public static void Label(Rect position, Texture image);
public static void Label(Rect position, GUIContent content);
public static void Label(Rect position, string text, GUIStyle style);
public static void Label(Rect position, Texture image, GUIStyle style);
public static void Label(Rect position, GUIContent content, GUIStyle style);
label的构造函数和Box的构造函数差不多,参数的含义可以查看上一篇的文章。
https://blog.csdn.net/qq_39485740/article/details/102537075
样例:
using UnityEngine;
using System.Collections;
public class label_script : MonoBehaviour {
public Texture LabelTexture;
private GUIContent content;
private GUIStyle style;
// Use this for initialization
void Start () {
content = new GUIContent ("This is a Label", LabelTexture, "This is a toolTip");
style = new GUIStyle ();
style.alignment = TextAnchor.MiddleCenter;
}
// Update is called once per frame
void Update () {
}
void OnGUI () {
GUI.Label (new Rect (100, 30, Screen.width, 30), "Hello World");
GUI.Label (new Rect (100, 70, LabelTexture.width, 30), LabelTexture);
GUI.Label (new Rect (100, 110, Screen.width, 30), content);
GUI.Label (new Rect (100, 150, Screen.width, 30), "Hello Unity", style);
}
}
运行效果:
总结:
虽让Box和Label的构造函数差不多,但是运行效果还是有很大的区别的,比如:Box的效果就是默认的时候背景有黑色的框存在,并且默认的对齐方式是居中对齐。而Label周围的背景默认的时候是不存在的,并且默认的对齐方式是向左对齐。