x


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.

alt text

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.

more ▼

asked Sep 22 '11 at 10:57 PM

MrSplosion gravatar image

MrSplosion
132 45 52 61

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 instead 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!

Sep 23 '11 at 01:55 AM syclamoth
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.)

more ▼

answered Sep 23 '11 at 01:53 AM

Owen Reynolds gravatar image

Owen Reynolds
11.2k 1 7 45

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3674
x2193
x378

asked: Sep 22 '11 at 10:57 PM

Seen: 839 times

Last Updated: Sep 23 '11 at 02:00 AM