游戏制作之路(30)用代码显示文本
前面学习了怎么样使用GUI.Button()函数来显示按钮,接着下来继续学习使用代码来显示文本。
其实有了前面的知识,再来学习显示纯文本显示,就是非常容易的事情。用Rect定义一个区域,让文本在这个区域里进行显示即可。如下面的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SampleButton : MonoBehaviour {
private int count;
private Rect btnRect;
private Rect labelRect;
// Use this for initialization
void Start ()
{
count = 0;
btnRect = new Rect();
labelRect = new Rect();
}
// Update is called once per frame
void Update ()
{
}
//界面显示
private void OnGUI()
{
btnRect.x = Screen.width / 3;
btnRect.y = Screen.height * 2 / 5;
btnRect.width = Screen.width / 3;
btnRect.height = Screen.height / 5;
if (GUI.Button(btnRect, "深圳改革开放"))
{
print(count);
count++;
}
//显示标签文本
labelRect.x = Screen.width / 3 ;
labelRect.y = Screen.height * 4 / 5;
labelRect.width = Screen.width / 3;
labelRect.height = Screen.height / 5;
GUI.Label(labelRect, "纯文本显示");
}
}
然后在后面添加一段设置labelRect的坐标和大小,接着用 GUI.Label函数来显示出来,当你点击运行之后,就可以看到下面的界面:
在这个例子里,简单地学会了用代码来显示纯文本。