- Home /
Problems with GUI and Resolutions. (C#)
I'm adding the HUD/GUI to my game, and I'm having problems making the health bars stay with the rest of the game's HUD, it changes place depending on resolution. The scaling works fine, perfect even, but as far as position goes, it's all over the place.
[Here's what it's meant to look like.][1]
[Here's what it looks like when the resolution is too big][2]
[Here's what it looks like when the resolution is too small][3]
Not sure if it makes a difference (I hope not!), but the Main HUD and the Health bar are using separate scripts, but are drawn from the same one, the main HUD script, I'm getting the Bars position, scale, and how it changes size (e.g. if health or armour goes up or down, or some other value has changed), the main script references this when it draws the texture.
Here's my code:
Main HUD:
foreach (GUIBarScript bar in GUIBars)
{
if (bar.stayWithMainHUD)
{
if (bar.texture)
{
GUI.DrawTextureWithTexCoords(new Rect(positionAndScale.x + bar.positionRect.x, positionAndScale.y + bar.positionRect.y, bar.positionRect.width, bar.positionRect.y),
bar.texture, bar.sourceRect, bar.transparent);
}
}
else
{
if (bar.texture)
{
GUI.DrawTextureWithTexCoords(bar.positionRect, bar.texture, bar.sourceRect, bar.transparent);
}
}
}
if (texture)
{
Rect positionRect = new Rect(positionAndScale.x, positionAndScale.y, positionAndScale.width * Screen.width, positionAndScale.height * Screen.height);
GUI.DrawTexture(positionRect, texture, ScaleMode.StretchToFill, isTransparent);
}
Health Bar:
sourceRectX = (float)masterScript.health / (float)masterScript.maxHealth;
positionRect = new Rect(positionAndScale.x, positionAndScale.y, sourceRectX * (positionAndScale.width * Screen.width), positionAndScale.height * Screen.height);
sourceRect = new Rect(0,0, sourceRectX, 1);
What's causing the problem? [1]: http://i123.photobucket.com/albums/o305/Dinoco07/Problem_zps2da6fee6.png [2]: http://i123.photobucket.com/albums/o305/Dinoco07/Problem2_zpse7e089bf.png [3]: http://i123.photobucket.com/albums/o305/Dinoco07/Problem3_zpsc7cca4ca.png
Both the bar and the rest are supposed to work together. It is probably the best to draw them from within a GUI group. This means you have to draw both from the same script. Alternatively you can normalize everything relative to the screen.
That's what I did, both are being drawn from the same script. The only reason they are separate scripts is so I can individually declare their position, and how the bars will change in size, the main script draws the actual item referencing the positions etc. set by the separate scripts.
Oh I see, every coordinate, I was only multiplying the width and height, it makes sense to do X and Y as well, that fixed it, thanks!
Answer by Lockstep · Feb 05, 2013 at 12:14 AM
Your script is a bit hard to understand from the outside since i don't know where all your properties come from. But generally you can say the GUI depends on the resolution. A quick example: If you have a 640x480 resolution then a (160,120,320,240) rect will be perfectly centered and take a fourth of the screen. But if you use a 1024x768 resolution the rect will be near the top left corner and won't take even 10% of the screen. To compensate this you can either have every coordinate as a multiple of screen.width/ screen.height, or use a GUI group. Any GUI elemnt inside of the group will be drawn inside of the group with the anchor point relative to the group.
I'm glad I could help. I converted the comment into an answer. Can you mark your question as answered please to keep UnityAnswers clean?