• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Cobradabest · Feb 04, 2013 at 10:54 PM · c#guiresolutionhud

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lockstep · Feb 04, 2013 at 11:29 PM 0
Share

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.

avatar image Cobradabest · Feb 04, 2013 at 11:31 PM 0
Share

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.

avatar image Cobradabest · Feb 05, 2013 at 09:05 AM 0
Share

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!

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lockstep · Feb 05, 2013 at 11:46 AM 0
Share

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?

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there anyway to only show a part of a GUI image during the game? (C#) 1 Answer

"NullReferenceException" while trying to draw a texture (C#) 1 Answer

How to dynamically change the text in Unity(Augmented Reality + NYARtoolkit(C#)) ? 0 Answers

C# How to Drag and Scale with Mouse Window 0 Answers

Equivelant of GUI.DrawSprite() ? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges