I have a Health Bar in my Unity game and it's implemented as a GUITexture
with gradient image from red to green.
Now I can reduce it's width from max width to 0, but is still scaled gradient.
public void UpdateHealthBar(int hitPoints) {
healthBar.pixelInset = new Rect(
healthBar.pixelInset.x, healthBar.pixelInset.y,
3* hitPoints, healthBar.pixelInset.height);
}
But I want to hide (make transparent) the right part of this health bar in game progress.
How can I do this? Thanks!
Or you can use GUI.DrawTextureWithTexCoords()
public Texture2D texture;
public float hp = 100f;
public const float hpMax = 100f;
void OnGUI()
{
GUI.DrawTextureWithTexCoords (new Rect (0, 0, hp, 20), texture, new Rect (0f, 0f, hp/hpMax, 1f));
}