我开发了一个android游戏,我创建了一个类似这样的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]);
}发布于 2014-07-03 02:36:18
解释
你的代码一团糟。我不认为它没有调整大小。我觉得它并没有达到你想要的效果。
让我们来看看是怎么回事:
你可以像设置一个新的坐标空间一样设置GUI.matrix。在本例中,它是一个坐标系,其中点(0,0)是屏幕的左上角,点(resolutionWidth,resolutionHeight)是屏幕的右下角。它看起来像logical.
这是完全错误的。您现在处于一个不依赖于实际屏幕分辨率的坐标系中。但是您使用的是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, "");
}https://stackoverflow.com/questions/24530152
复制相似问题