- Home /
GUI Label Size and Position Changes With Resolution
Hey all,
I'm having this very frustrating problem with my HUD GUI interface. I would like to display and textured label in a portion of the screen. The problem is that the position and size get all wonky when I change the screen size/resolution.
Here is my very simple code:
#pragma strict
var missileSymbol : Texture2D;
function OnGUI()
{
var symbolWidth : float = Screen.width / 10;
var symbolHeight : float = Screen.width / 10;
var xPos : float = Screen.width / 2;
var yPos : float = Screen.height / 2;
GUI.Label (Rect(xPos, yPos, symbolWidth, symbolHeight), missileSymbol);
}
How can I fix this problem with code?
Thanks!
Answer by JDappsCR · Sep 15, 2014 at 09:29 AM
You need to have your vars "symbolWidth" "symbolHeight" "xPos" "yPos" in the Update function.
Example:
#pragma strict
var missileSymbol : Texture 2D;
function Update()
{
var symbolWidth : float = Screen.width / 10;
var symbolHeight : float = Screen.width / 10;
var xPos : float = Screen.width / 2;
var yPos : float = Screen.height / 2;
}
function OnGUI()
{
GUI.Label (Rect(xPos, yPos, symbolWidth, symbolHeight), missileSymbol);
}
Answer by LSPressWorks · Sep 15, 2014 at 12:21 AM
I had the same issue with GUItexts, instead of usign height width and offsets, use the transforms scale and position.
Set the label to 1,1,1 in the scale. Set the width, height and offsets to 0 place the label, click on the game tab at the top. Just to the left of position x or y, when you click two little arrows appear that you can drag to increase and decrease to position.
But is there a way to do that in the code? $$anonymous$$aybe I don't know enough about GUIs yet, but that would involve creating the interface as a GameObject > GUITexture as opposed to doing it with OnGUI in code. Also, I'm making this game as pixel art and I'd like to avoid changing the scale where possible since I need all the pixels to be the same size.
Your answer
![](https://koobas.hobune.stream/wayback/20220613163012im_/https://answers.unity.com/themes/thub/images/avi.jpg)