• 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
1
Question by Benifir · Dec 17, 2013 at 10:02 AM · c#healthbar

Healthbar Ratio Question

I've looked around on the forums and answers, as well as google, and couldn't find anything relevant to the issue I'm having. As the healthbar decreases in length, it also decreases in width. If you'd mind taking a look to see if there are any issues I'd appreciate it greatly.

Here is an example of what it looks like:

alt text

And here is the section of code that controlls it:

 using UnityEngine;
 using System.Collections;
 
 public class HealthBarManager : MonoBehaviour {
     public Texture2D PartyHealthbarOutline;
     public Texture2D PartyHealthbarGreen;
     public Texture2D PartyHealthbarYellow;
     public Texture2D PartyHealthbarRed;
 
     public string PartyMemberName;
     public float PartyMemberMaxHealth;
     public float PartyMemberCurHealth;
     
     private float PartyMemberHealthRatio;
     private float PartyHealthBarLength;
     private float PartyHealthBarWidth;
 
     void OnGUI () {
         GUIStyle PartyMemberGUIStyle = new GUIStyle(GUI.skin.label);
         PartyMemberGUIStyle.fontSize = 10;
         PartyMemberGUIStyle.normal.textColor = Color.white;
     
         GUI.Label(new Rect(20,70,170,80), PartyHealthbarOutline);
     
         GUI.Label(new Rect(50,75,30,20), PartyMemberName, PartyMemberGUIStyle);            //Display the Players name
     
         DeterminePartyHealthbarColor();
     }
 
     public void DeterminePartyHealthbarColor() {
         AdjustCurrentHealth(0);
         
         if(PartyMemberCurHealth >= (PartyMemberMaxHealth - (PartyMemberMaxHealth / 3))) {
             GUI.Label(new Rect(90.2f,78.49f,PartyHealthBarLength,60), PartyHealthbarGreen);
         }
         else if(PartyMemberCurHealth < (PartyMemberMaxHealth - (PartyMemberMaxHealth / 3)) & PartyMemberCurHealth >= (PartyMemberMaxHealth - ((PartyMemberMaxHealth / 3) * 2))) {
             GUI.Label(new Rect(90.2f,78.49f,PartyHealthBarLength,60), PartyHealthbarYellow);
         }
         else if(PartyMemberCurHealth < (PartyMemberMaxHealth - ((PartyMemberMaxHealth / 3) * 2))) {
             GUI.Label(new Rect(90.2f,78.49f,PartyHealthBarLength,60), PartyHealthbarRed);
         }
         else {
             Debug.LogError("Healthbar color to use is unknown!");
         }
     }
     
     public void AdjustCurrentHealth(int Adj) {
         
         PartyMemberCurHealth += Adj;
         
         if (PartyMemberCurHealth < 0)
             PartyMemberCurHealth = 0;
         if(PartyMemberCurHealth > PartyMemberMaxHealth)
             PartyMemberCurHealth = PartyMemberMaxHealth;
         if(PartyMemberMaxHealth < 1)
             PartyMemberMaxHealth = 1;
         
         PartyMemberHealthRatio = PartyMemberCurHealth / PartyMemberMaxHealth;
         PartyHealthBarLength = 93 * PartyMemberHealthRatio;
     }
 }
sgsgsgd.png (25.7 kB)
Comment
Add comment
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

1 Reply

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

Answer by Bunny83 · Dec 17, 2013 at 11:00 AM

Well, that's simple. You don't use a beckground texture for your GUIStyle but you use the Textures as "content". Unity scales the content proportionally so that it fits into the given rect. Since you made the width smaller the image doesn't fit into the rect and it's scaled down.

You want to use the textures as background images. You could create the GUIStyles on the fly (like you did already) but in general it's way easier to use a GUISkin.

Just create a new GUISkin in your project view. It will be initialized with all the default styles. Now at the very bottom there's an array called "custom styles". There you can create your own "special" GUIStyles. The nice thing about those is you can even tweak the values while you're testing in the editor.

So just add a new style by increasing the "count" variable of the array. Now you just have to expand the "normal" state of that style and drag&drop your desired texture onto the background image slot. Don't forget to give your style a unique name for example "HealthBarInner".

In your script you have to create a public variable of type GUISkin, assign your skin-asset to the variable in the inspector and add this line at the start of OnGUI:

 GUI.skin = mySkinVariable;

Once your skin is set as the "active one", you can simply pass a string of the style name for the optional GUIStyle parameter of all GUI methods.

So it might look like this:

 using UnityEngine;
 using System.Collections;
 
 public class HealthBarManager : MonoBehaviour {
 
     public GUISkin mySkin;
 
     public string PartyMemberName;
     public float PartyMemberMaxHealth;
     public float PartyMemberCurHealth;
     
     private float PartyMemberHealthRatio;
     private float PartyHealthBarLength;
     private float PartyHealthBarWidth;
     
     void Start() {
         if(PartyMemberMaxHealth < 1)
             PartyMemberMaxHealth = 1;
     }
     
     void OnGUI () {
         GUI.skin = mySkin;
         GUI.Label(new Rect(20,70,170,80), "", "HealthBarOutline");
         
         GUI.Label(new Rect(50,75,30,20), PartyMemberName, "PartyMemberText");
         
         DeterminePartyHealthbarColor();
     }
 
     public void DeterminePartyHealthbarColor() {
         AdjustCurrentHealth(0);
         
         if(PartyMemberCurHealth >= ((PartyMemberMaxHealth / 3)*2)) {
             GUI.Label(new Rect(90.2f, 78.49f, PartyHealthBarLength, 60), "", "HealthBarInnerGreen");
         }
         else if(PartyMemberCurHealth >= (PartyMemberMaxHealth / 3)) {
             GUI.Label(new Rect(90.2f, 78.49f, PartyHealthBarLength, 60), "", "HealthBarInnerYellow");
         }
         else {
             GUI.Label(new Rect(90.2f, 78.49f, PartyHealthBarLength, 60), "", "HealthBarInnerRed");
         }
     }
     
     public void AdjustCurrentHealth(int Adj) {
         
         PartyMemberCurHealth += Adj;
         PartyMemberCurHealth = Mathf.Clamp(PartyMemberCurHealth, 0, PartyMemberMaxHealth);
         
         PartyMemberHealthRatio = PartyMemberCurHealth / PartyMemberMaxHealth;
         PartyHealthBarLength = 93 * PartyMemberHealthRatio;
     }
 }

In my example your skin should have 5 custom styles:

  • HealthBarOutline // The outline of the healthbar

  • PartyMemberText // For the player name lable

  • HealthBarInnerGreen // The inner health bar style with the green texture

  • HealthBarInnerYellow // The inner health bar style with the yellow texture

  • HealthBarInnerRed // The inner health bar style with the red texture

As alternative instead of using 3 different styles, you could use one style with a white healthbar-inner texture and just use GUI.color to tint the element green / yellow / red. don't forget to reset the color to white or everything drawn afterwards will be that color too.

ps: I've simplified some of your code since there was a lot redundancy in the if conditions which makes it harder to understand.

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 Benifir · Dec 17, 2013 at 11:50 AM 1
Share

Absolutely amazing! I never thought of just creating a GUISkin for it, this simplifies it tenfold. Sorry about the redundancies, I had just prototyped it out. Anyways, thank you for taking the time to post such a thorough answer :)

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Health Bar 1 Answer

Falldamage has no effect on my HB 2 Answers

My HealthBar is not regenerating when i'm pickingup potion 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges