我开发了一个安卓游戏,我创建了一个像这样的gui盒子并添加了一个矩阵来调整这个gui到不同的屏幕分辨率,这是代码:
float resolutionWidth = 800.0f;
float resolutionHeight = 480.0f;
void OnGUI()
{
GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(Screen.width / resolutionWidth, Screen.height / resolutionHeight, 1);
GUI.Box(new Rect((Screen.width / 2) - 240f, (Screen.height / 2) - 170, 731, 447), "", WelcomeUI.customStyles[0]);
}
当我更改屏幕分辨率时,它没有调整大小的问题。我不明白这个问题 感谢您的帮助
答案 0 :(得分:0)
你的代码一团糟。我不认为它没有调整大小。我认为这不符合你的要求。
让我们看看发生了什么:
GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(Screen.width / resolutionWidth, Screen.height / resolutionHeight, 1);
您可以设置GUI.matrix
设置新的坐标空间。在您的情况下,它是一个坐标系,其中点(0,0)是屏幕的左上角,而点(resolutionWidth,resolutionHeight)是屏幕的右下角。这看似合乎逻辑。
GUI.Box(new Rect((Screen.width / 2) - 240f, (Screen.height / 2) - 170, 731, 447), "", WelcomeUI.customStyles[0]);
这是完全错误的。您现在处于一个不依赖于实际屏幕分辨率的坐标系中。但是您使用的Screen.width
和Screen.height
在这里没有任何意义。现在的屏幕中间点是(resolutionWidth / 2,resolutionHeight / 2)。此外,你的盒子的大小是731×447,几乎涵盖了所有“模型”屏幕。这里也有问题。
以下是您的简短示例:
float resolutionWidth = 800.0f;
float resolutionHeight = 480.0f;
public void OnGUI()
{
GUI.matrix = Matrix4x4.TRS(
new Vector3(0, 0, 0),
Quaternion.identity,
new Vector3(
Screen.width / resolutionWidth,
Screen.height / resolutionHeight,
1.0f));
Rect boxRect = new Rect(
resolutionWidth / 2.0f - 240f, resolutionHeight / 2.0f - 170,
480, 340);
GUI.Box(boxRect, "");
}