Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 Chuck 1 · Sep 21, 2011 at 04:20 PM · guibegingrouphealth bar

Complex health bar using GUI.BeginGroup for clipping?

I have a healthbar that displays hitpoints and a shield. To get this to work, I have three images (healthBarShieldFill, healthBarHPFill, and healthBarFrame) drawn on top of each other with the width of the fill images controlled by GUI.BeginGroup() calls. Since the max HP is 100, and the max Shield is 10, shieldPercent should always be <= 1, and hpPercent should always be <= shieldPercent and never reach 1. This seems to work fine except when the HP is full, and I have no idea why. I've even taken the frame image off to see if it was covering up the difference, but it was not. Can someone take a look?

         float shieldPercent = ((float)(hp + shield)) / 110.0f;
         float hpPercent = ((float)hp) / 110.0f;
         float healthBarWidth = 256.0f;
         float healthBarHeight = 32.0f;

         GUI.Label(labelRect, string.Format("Health: {0} (+{1})", hp, shield));
         labelRect.y += labelRect.height;
         GUI.Label(labelRect, string.Format("Health: {0:f2} (+{1:f2})", healthBarWidth * (hpPercent), healthBarWidth * (shieldPercent)));
         //labelRect.y += labelRect.height;
         //GUI.Label(labelRect, string.Format("Kills: {0}", kills));

         GUI.BeginGroup(new Rect((c.pixelWidth - healthBarWidth) / 2, (c.pixelHeight - healthBarHeight - 200) / 2, healthBarWidth, healthBarHeight));
         {
             GUI.BeginGroup(new Rect(0, 0, healthBarWidth * (shieldPercent), healthBarHeight));
             {
                 GUI.Box(new Rect(0, 0, healthBarWidth, healthBarHeight), healthBarShieldFill);
                 GUI.BeginGroup(new Rect(0, 0, healthBarWidth * (hpPercent), healthBarHeight));
                 {
                     GUI.Box(new Rect(0, 0, healthBarWidth, healthBarHeight), healthBarHPFill);
                 }
                 GUI.EndGroup();
             }
             GUI.EndGroup();
             GUI.Box(new Rect(0, 0, healthBarWidth, healthBarHeight), healthBarFrame);
         }
         GUI.EndGroup();

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

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Accessing a Variable in another script for (SLIDER) 1 Answer

How to hide the enemy's health bar until the player enters the enemy collier? 2 Answers

GUI button disappears in GUI group 1 Answer

Display problem with OnGUI 3 Answers

How can I view a gameObject through every other gameObject? 0 Answers


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