x


GUI shifts about on different screen sizes - help!

I am using the code listed below to simulate a computer terminal (think Black Ops computer) however whenever I transfer the game to my other computer which has a larger screen size/resolution, the GUI gets mucked up. I was thinking of displaying the GUI in absolute pixels however I am unsure as to how I can do that with my script. Thanks in advance!

var visible;
var controlTexture : Texture2D;
var gameCode : String = "1234";
var stringToEdit : String;
var buttonMessage : String = "Enter Command";
var pladas : String = "/test";
var practice : String = "/practice";
var zombies : String = "/zombies";
var instructions : String = "/help";
function OnGUI(){
  if( visible )
  {
   stringToEdit = GUI.TextField (Rect (400, 500, 70, 25), stringToEdit, 10); 
   if(stringToEdit == "1234"){
      buttonMessage = "Begin Game";
   }else
   if(stringToEdit == "/help"){
    buttonMessage = "Instructions";
   }else
   if(stringToEdit == "/zombies"){
      buttonMessage = "Unleash the Horde!";
   }else
   if(stringToEdit == "/test"){
      buttonMessage = "Secret";
   }else
   if(stringToEdit == "/practice"){
    buttonMessage = "Begin Training";
   }else{
      buttonMessage = "Enter Command";
   }
   if(GUI.Button(Rect(500, 500, 135, 25), buttonMessage) && stringToEdit == "1234"){
      Application.LoadLevel (1);
   }else 
   if(GUI.Button(Rect(500, 500, 135, 25), buttonMessage) && stringToEdit == "/zombies"){
    Application.LoadLevel ("Zombies");
   }else
   if(GUI.Button(Rect(500, 500, 135, 25), buttonMessage) && stringToEdit == "/test"){
      Application.LoadLevel ("Test");
   }else
   if(GUI.Button(Rect(500, 500,135, 25), buttonMessage) && stringToEdit == "/help"){
    Application.LoadLevel ("Walkthrough");
   }else
   if(GUI.Button(Rect(500, 500, 135, 25), buttonMessage) && stringToEdit == "/practice"){
    Application.LoadLevel ("Practice");
   }
}
}
function Update(){
  if(Input.GetKeyDown(KeyCode.M))
  {
    visible = !visible;
  }
}
more ▼

asked Jun 27 '11 at 05:12 PM

BlueMoonGames gravatar image

BlueMoonGames
31 2 2 4

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Use GUI.BeginGroup(Rect) and GUI.EndGroup()

When you call BeginGroup(Rect), every GUI call between that and the matching EndGroup has it's coordinate system reset to (0,0) being the location defined as the start of the Rect passed to BeginGroup(Rect)

So you can have all the GUI calls inside this group look nice relative to each other, and then just move around the Rect at the start to shift all the groups around without distorting anything.

Try passing Screen.Width / 4 and Screen.Hieght / 2 into the first two values of the Rect, and your gui will remain the same size and location across all platforms.

Here's the documentation on the subject:

http://unity3d.com/support/documentation/ScriptReference/GUI.BeginGroup.html

more ▼

answered Jun 27 '11 at 05:27 PM

SilverTabby gravatar image

SilverTabby
1.9k 3 6 18

(comments are locked)
10|3000 characters needed characters left

First off, you are using absolute pixels. You may want to modify GUI.matrix based on the screen size. Something like:

GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3(Screen.width/1024f, Screen.width/1024f, 1.0));
//Assuming your "standard" width is 1024 pixels
more ▼

answered Jun 27 '11 at 05:29 PM

qwertyp gravatar image

qwertyp
164 14 15 21

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3680
x48
x43

asked: Jun 27 '11 at 05:12 PM

Seen: 695 times

Last Updated: Jun 27 '11 at 05:30 PM