- Home /
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