2014-07-02 69 views
0

我developped一个机器人比赛,我创造了这样一个GUI框,并增加了一个矩阵来调整这个GUI来型动物的屏幕分辨率,这里是代码:Android的屏幕分辨率团结

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

“这意味着你的OnGUI执行可能被称为每帧(每个事件的通话)几次欲了解更多信息GUI事件参见Event引用。如果MonoBehaviour的启用属性设置为false,则不会调用OnGUI()。“ –

+0

@ marco-acierno这是一个魔法咒语还是什么?我已经读了两遍,但我没有注意到任何影响。也许现在是月球的错误阶段:D –

回答

0

说明

你的代码是一团糟。我认为这不是调整大小。我认为它只是没有做到你想要的。

让我们看看这是怎么回事:

  1. GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(Screen.width/resolutionWidth, Screen.height/resolutionHeight, 1); 
    

    您可以在设置GUI.matrix如设置一个新的坐标空间的东西。在你的情况下,它是一个坐标系,其中point(0,0)是屏幕的左上角,point(resolutionWidth,resolutionHeight)是屏幕的右下角。看起来合乎逻辑。

  2. GUI.Box(new Rect((Screen.width/2) - 240f, (Screen.height/2) - 170, 731, 447), "", WelcomeUI.customStyles[0]); 
    

    这是完全错误的。您现在处于不依赖实际屏幕分辨率的坐标系中。但是您使用的是Screen.widthScreen.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, ""); 
} 
+0

你好Sergey Krusch; 非常感谢您的帮助,但我不明白您如何在代码480和340中输入值,您如何更改它们?谢谢 – user3655933

+0

你是什么意思“如何”?使用键盘输入...作为选项 –