- Home /
GUI Positioning Problems
How do I make a GUI so that no matter what the screen width is the GUI is always positioned in a certain place? This picture show the problem I'm having. I want the health bar to not overlap the buttons like it does in the smaller resolutions.
Here is the script:
var background : Texture2D;
var array : float[];
function OnGUI(){
GUI.DrawTexture(Rect(Screen.width - Screen.width / 2 - array[0], 10, Screen.width / 10 + array[1], 20), background, ScaleMode.StretchToFill, true, 0);
}
What am I doing wrong?
Thanks.
In smaller resolutions, it will always overlap, because you are using absolute values in your width coordinate! You should multiply all your values by Screen.width, and then use proportions ins$$anonymous$$d of exact numbers. On the other hand, this only happens on really small screens, so possibly you should do a quick check to see if it would overlap, and then switch between the two methods!
Answer by Owen-Reynolds · Sep 23, 2011 at 01:53 AM
Looks like you're mixing percents and pixels. The part Screen.width - Screen.width/2
(which is a long way to write width/2) is mostly saying 50%. I'm guessing array[0] and array[1] are in pixels. Is 1/2-way minus 100 pixels to the right of 30% plus 60 pixels? Who knows?
Easier to think of everything as percents first -- lifebar is from 40% to 60%, or 50% +/-array[i], which are percents. Then one *Screen.width
at the very end. Then, if you really need, look at the final pixel result (say you need at least a 5 pixel border.)