- Home /
Another GUI question. HealthBar
I created two Texture2D objects. One which contains an empty "HealthBar" image, and one that contains a full "HealthBar" image.
What I am trying to do is draw the empty bar using GUI.DrawTexture, then draw the leftmost portion of the Full texture up to the current health of the player.
The problem I am having is getting the leftmost portion of the texture. GUI.DrawTexture doesnt appear to have an overload that lets me select a source rectangle from within the texture. Graphics.DrawTexture does, but it also requires a color, Material, and borders. The results I get using it do not look very good :(
Am I going about this the wrong way? Is there a better way to accomplish this??
Thanks, -Larry
EDIT Ok, found an example elsewhere, and have figured this out for the most part. But still having a problem. See this function I created...
function DrawBar(Left:int, Top:int, BarTexture:Texture, MaxValue:float, Value:float) { var LocationRect:Rect = Rect(Left,Top,EmptybarTexture.width,EmptybarTexture.height); var DrawWidth:int;
DrawWidth = EmptybarTexture.width*(Value/MaxValue);
GUI.BeginGroup (LocationRect);
GUI.DrawTexture (Rect (LocationRect), EmptybarTexture);
GUI.BeginGroup (new Rect (Left, Top, DrawWidth, EmptybarTexture.height));
GUI.DrawTexture(LocationRect, BarTexture);
GUI.EndGroup ();
GUI.Label(Rect((LocationRect.width/2)-10,Top-5,40,50),Value.ToString());
GUI.EndGroup ();
}
This expects that there is a variable in the script named EmptybarTexture and HealthbarTexture.
You would call this with...
DrawBar(0,0,HealthbarTexture,100,50);
The problem I am having now is if I make a 2nd call with the top parameter set to 10, this 2nd bar doesn't display at all.
If anyone has any ideas, please feel free to chime in :)
-Larry
Answer by Larry-Dietz · Apr 05, 2010 at 01:15 AM
Ok, figured it out.
Here is the revised function in case anyone else want's to use it :)
function DrawBar(BarLeft:int, BarTop:int, BarTexture:Texture, MaxValue:float, Value:float) { var LocationRect:Rect = Rect(BarLeft,BarTop,EmptybarTexture.width,EmptybarTexture.height); var DrawRect:Rect = Rect(0,0,EmptybarTexture.width,EmptybarTexture.height); var DrawWidth:int;
DrawWidth = EmptybarTexture.width*(Value/MaxValue);
GUI.BeginGroup (LocationRect);
GUI.DrawTexture (DrawRect, EmptybarTexture);
GUI.BeginGroup (new Rect (0, 0, DrawWidth, EmptybarTexture.height));
GUI.DrawTexture(DrawRect, BarTexture);
GUI.EndGroup ();
GUI.Label(Rect((DrawRect.width/2)-10,-5,40,50),Value.ToString());
GUI.EndGroup ();
}
The problem was that I didn't realize that the Left and Top were relative to the group created, so essentially, I was setting the limits, then attempting to draw outside of them. This function works as expected. Not sure why I had to set the top to -5 to get the label to display correctly, but it works :)
-Larry
I tried your script but it doesnt work for me it says unknown identifier EmptyBarTexture so if you could help me cuz im working on an RPG with a couple of friends!
You need to add the following to the top of your script, then drag the textures you want to use into these objects in the inspector.
var EmptybarTexture:Texture2D; var HealthbarTexture:Texture2D;
After you have these at the top, and the textures you want to use assigned, it should work just fine.
Use the following to draw the bar... DrawBar(0,0,HealthbarTexture,100,50);
Hope this helps, -Larry