unity中生成一个GUI格子(始终居中)
1.Script程序
using UnityEngine;
using System.Collections;
public class GUITest : MonoBehaviour
{ [SerializeField]
float width;
[SerializeField]
float height;
float xMin;
float yMin;
private void Start()
{
width = 100;
height = 100;
}
void OnGUI()
{
xMin = 0.5f * Screen.width - width / 2;
yMin = 0.5f * Screen.height - height / 2;
print("height: "+ Screen.height);
print("width: "+ Screen.width);
Rect aa = new Rect(xMin, yMin, width, height);
// Make a background box
GUI.Box(aa, "GUI_TEST");
}
}
2.OnGUI函数每隔一定时间被调用一次
void OnGUI()
{}
3.C#中变量定义时顺序不定,不能再声明时使用别的变量的值

浙公网安备 33010602011771号