scaling/aniamted life bar

i’ve been looking through the forums and have been unable to find any information on this.

i have a pretty standard image, and all i want to do is scale it in the x with code. but if i attempted to adjust the x scale of my image it scales in the y as well.

here is the code i’m using to display the image

GUI.Label(Rect(0,0,healthPercentageScale,PlayerHealthBar10.height), PlayerHealthBar10);

anyway, does anyone have any suggestions to accomplish this.

IIRC GUI.Label() will always scale uniformly. Use GUI.DrawTexture() instead, which offer the ScaleMode option to specify how the texture should be scaled.

nvm thank you, that workedperfectly

i was just testing it out, and when the scale gets below 30% it starts to go transparent, and after going below 12% it actually starts to scale in the y, any reason this happen or ways to prevent it from happening?

edit-----

tried scale and crop and it seems to work, still unsure of why it happened with the other though

Calisk,

Would you mind posting that final code snippet you used for the scaling bar?

I am trying to do the same thing, but my bar is jumping instead of a smooth transition.

My code:

GUI.DrawTexture(Rect(403,119,WidthChange,10), blackBG, ScaleMode.StretchToFill, true, 0);

for(WidthChange = 0; WidthChange <= 100; WidthChange++)
{
PauseMe();
}

function PauseMe()
{
yield new WaitForSeconds (1);
return;
}

not sure if that’s the best way to pause.

Thanks!

  • Jon

You probably want to use a GUIStyle instead. That gives you lots of options for how it should scale.

try something like:

var playerHealthStyle : GUIStyle;

function OnGUI () {
    GUI.Label (Rect (0,0,playerHealth, 20), "", playerHealthStyle);
}

You will get a Player Health Style in the inspector. Assign the texture to it’s normal->background slot.

Now, you can get some non-scaling pixels using the border property.

Thanks Nicholas!

I ended up using the Math functions to grow the size, but I will probably play around with it again in the future. I will try the label then.

Thanks again for posting your response.

Best,

  • Jon