- Home /
The question is answered, right answer was accepted
Texture2D problem : life bar
Hello everyone !
I'm trying to make a nice life bar, but I'm having some issues to do it ...
Here are my textures :
As you can imagine, the colored bar is entirely shown when my player has full life (100). My challenge is to display a fraction of the black bar on top of the first one, considering the life of the player.
Here is my non working code :
var life:int = 100;
Graphics.DrawTexture(new Rect((0.75*Screen.width),(0.06*Screen.height),300,50), lifeBar);
Graphics.DrawTexture(new Rect((0.75*Screen.width),(0.06*Screen.height),300,50), deathBar, new Rect(deathBar.width*(1-life/100),deathBar.height,300,50),0,0,0,0);
The thing is I'm taking a rectangle from my second texture, not a parallelepiped like I would like to ... My hole problem is that the bars are leant. Is there an easy way to do what I want ?
The easy solution would be to create 100 textures, but it takes some time and it's heavy ...
EDIT : display of new textures. 1st one is health at 100, 2nd at 0, 3rd is the result with the previous code at life = 100, and 4th is result with life=70 and the following code :
Graphics.DrawTexture(new Rect((0.75*Screen.width),(0.06*Screen.height),300,50), lifeBar);
Graphics.DrawTexture(new Rect((0.75*Screen.width),(0.06*Screen.height),300*(1-life/100),50), deathBar);
This last result is wrong because of a scale problem (not the same parallels) and starts at the right (should be on the left side)
Could you clarify what exactly the problem is?
• Does your code generate any errors?
• Do both the textures get displayed?
• Does the black texture resize itself properly?
• Is the positioning correct?
A few more details would be awesome, I doubt you will have to resort to creating 100 different images ;)
P.S. Could you perhaps give us an image of what the bar currently looks like when you run the game?
I understand the mechanism, but how do you create your image like that ? I already have my life bar, how can I simply add the alpha parameter to it ?
BTW thanks for the link, very useful :)
EDIT : I don't have any clue to how to do my stuff with this alpha mask thing. I understand it is what I want but I really don't have any idea how ... I created my first life bar with red and full alpha on the left and green and 0 alpha on the right. If I change my texture with the green circle of the example, it works, but with my texture, I see very little changes when I move the cursor, and everything is gone when I get to 0. It is not progressive at all ...
EDIT bis : I just checked my texture and there is a problem with my alpha parameter :(. I'm trying to correct it ...
This solution force me to have a gameobject. But I would prefer to use Graphics.DrawTexture or GUI.DrawTexture. Does someone else has another idea ?
do you have a good reason for not wanting to use a game object?
the lifebar is an object of the game. aren't you going to want to maybe access that later? Shouldnt you store that?
If you have a solution that only requires you make it a game object and you have no good reason for not making it a game object.... $$anonymous$$ake it a game object.
Follow this Question
Related Questions
Create Texture2D and assign image to it through a script 1 Answer
Two flat textures interfere with each other when both on z=0 2 Answers
How do you create animated textures with Texture2D.Apply with any regularity? 1 Answer
Reloading texture2d from code 1 Answer
Unlit/Transparent texture leaves dark lines on the transparent edges 1 Answer