GUI Texture used for healthbarscales

Hi
I am trying to create a simple health bar.There are 2 images one gray and other green and the green(full life) is positioned over the gray(hiding it).There are some tick marks on both the textures. So I use 2 GUITextures with different images for this and in the designer I set the scale ,rotation and pixel insets of both the Textures to zero.

Their widths and heights are set appropriately in designer.And at runtime I change(only) the width of the pixel inset based on current health.So as health goes down , the width of the green texture keeps reducing.However it seems to be scaling the whole image as opposed to merely reducing width of displayed area.Any Idea what I could be doing wrong??

Or is there a simpler way of doing this?

1 Answer

1

I can see two ways of achieving the result you want.

You could create a new texture at runtime and use Texture2D.GetPixels() and Texture2D.SetPixels() to fill it with a portion of the original image but that’s CPU-expensive.

You could cut down your health image into sub-images, fill an array with them and have a function take care of displaying the appropriate images according to the current health ratio. That’s motivation-expensive :wink:

I bet/hope there are better way, but I haven’t figured them yet.