- Home /
GUI To change with screen resolution?
Before i ask my question, i would sincerely apologize for my bad grammar. English is not my native language:-)
My question is almost completely revealed in the Titel.
I would like to know, how i could make GUI resize and reposition according to what resolution it is played in.
I have already searched the forum, but either it was in JS or it was nothing a Newbie like me could understand.
I should also mention, that i am not an experienced Unity user, nor programmer.
My only experience is with C# and Basic Java..
so i would like if the answers wouldent be Hard-Coded, and not to complicated :-)
Thanks
Answer by xKroniK13x · Jun 06, 2013 at 08:49 PM
Screen.width
finds the width of a screen and Screen.height
finds height. So if you want something in the bottom right, something like this would suffice:
GUI.Button (Rect (Screen.width - x, Screen.height - y, x, y), "Button")
Where x is the width, and y is the height. If you do not subtract the x and y from the width and height, it will be offscreen, since it is the edges.
So this is my code now, and it still doesent work, it just keeps sitting at the same spot no matter the resolution. it doesent go to the middle where i want it to be, and where i set it to be in my 16:10 aspect when i change to other aspect or try to build and run it in another resolution :/
By the way, i want it to be in the middle top of the screen. CordX is set to 700 in the inspector CordY is set to 610 in the inspector.
public float CordX;
public float CordY;
public float SizeX;
public float SizeY;
public float textCordX;
public float textCordY;
public float textSizeX;
public float textSizeY;
public GUISkin curSkin;
public string curRegion;
public Texture2D regionName;
// Use this for initialization
void Start () {
curRegion = "Road to mishmosh";
}
// Update is called once per frame
void Update () {
}
void OnGUI(){
GUI.skin = curSkin;
GUI.Label (new Rect(Screen.width - CordX, Screen.height - CordY,SizeX,SizeY), regionName);
GUI.Label (new Rect (textCordX, textCordY,textSizeX,textSizeY), curRegion);
GUI.skin.label.alignment = TextAnchor.$$anonymous$$iddleCenter;
}
}
To find the middle of the top, use Screen.width / 2
... so something like this:
var paddingTop : int; //This is the space from the top of the screen to the label, in pixels.
GUI.Label (new Rect(Screen.width/2 - CordX/2, paddingTop,SizeX,SizeY), regionName);
I believe that will work. You may have to adjust the - CordX/2
, but I think it will behave correctly.
Answer by screenname_taken · Jun 08, 2013 at 12:43 AM
What i did with my project was to set my layout (GUITexture or normal gui buttons) on the screen by using a 1280*800 res as my goal lets say.
Then measure the actual screen size and divide by the res in which i built the scene.
Then multiply the element's position by that ratio.
It resized and re placed everything that had that script. Tested it with various resolutions.
The code may be in JS but it should give an idea.
private var ratio:float;
private var screenHeight:float;
function Awake () {
screenHeight=Screen.height; //save screen into float.
ratio=screenHeight/800; //ratio by which to multiply to resize the buttons.
guiTexture.pixelInset.height*=ratio;
guiTexture.pixelInset.width*=ratio;
guiTexture.pixelInset.x*=ratio;
guiTexture.pixelInset.y*=ratio;
}