- Home /
Is it possible to have GUI keep it's position across all resolutions?
Hi,
I just wanted to know if it's possible, and if so, how. I tried researching it some, but the closest I got was using Screen.Height and Screen.Width(or something like that).
I was thinking that maybe I could somehow create my "own" coordinate system for the GUI, by using the screen height and width(kind of create a plane and place GUI on it). What that basically means is that I would make a new button at the position * screen height / screen width.
Basically, I want some GUI's location to be persistent across all resolutions.
Would that method work? Or is it more complicated than that?
(Note: I didn't actually try using the method I was talking about.)
Thank you,
SeeSharp.
Answer by Ian-McCleary · Apr 28, 2014 at 01:45 AM
You can go by a coordinate system where the number you put in the X,Y are part of the resolution scale but this often puts the GUI of people with 1080p and higher resolution scales awkwardly in the middle of the screen. Make sure all GUI script is in a OnGUI function. Here is an example of set resolution.
GUI.Button(Rect(450,350,50,50))
Now to keep everything the same you can make GUI that subtracts pixels from the total screen height and length to make it a little more compatible across different screen resolutions. Here is an example of that.
GUI.Button(Rect(Screen.width*0.5-50,200-20,100,40)))
just make sure that whatever you put in the place of 50 is half of the number you put in 100 and same with 20 and 40.
Now im pretty sure there is no way to get you GUI exactly the same on every resolution but the second option is much closer and used more often by game makers.
I hope this helped you in some way or another and please leave me a comment if it didnt!
Hi,
Thank you for your answer!
I currently don't have time to try it out, but I'll make sure to try it!
I have a question though: Is the method you provided more "accurate" than the one I mentioned?(a new button at the position * screen height / screen width)
Thank you,
SeeSharp.
Answer by Jeff-Kesselman · Apr 28, 2014 at 01:50 AM
Yes, you can keep it in the same relative position across all resolutions.
The trick is what is called "normalized coordinates". These are coordinates that are between 0 and 1 where 0,0 is one corner of the screen and 1,1 is the opposite corner.
You can get normalized coordinates by dividing the x and y pixel positions by the current width and height.
You then multiply the normalized coordinates times the actual width and height when the game sets up.
An equivalent thing to do is this:
Lay your gui out with pixel coordinates at a well known resolution.
Store that resolution along with the X and Y yo hare using for each gui element's coordinates.
When it comes time to draw, take the stored position coordinates, divide by the stored width and height, and multiply by the current width and height
Hi,
Thank you for your answer! I will make sure to try it out!
Thank you,
SeeSharp.
If it helps you out,please vote it up and mark it as the answer :)