- Home /
GUI position according to screen resolution
Hi,
I have multiple GUIs in my game. They should be at different corners/sides of the screen. But the GUI positions are calculated from the center. So they end up in different positions in different screen resolutions.
How can I put them in the correct positions, for all resolutions?
Answer by iwaldrop · Jan 31, 2013 at 04:56 AM
What you probably need to do is get your Screen width and height and do a little math to determine the scaled placement of them. You might also need to scale all of your gui elements by that same amount, so check it out.
public float targetWidth;
public float targetHeight;
private Vector2 guiScaler;
void Awake()
{
guiScaler = new Vector2(targetWidth / Screen.width, targetHeight / Screen.height);
}
Then you can use guiScaler.x and guiScaler.y to scale your gui elements appropriately.
Note: I may have gotten the division backwards... :)
great (I didn't try it yet, but will this work, as I have all my scripts in JS.)
It might not be a drag-and-drop solution for JS, but the concept will work, yes.