- Home /
Drawing bullets using GUITextures
Hi,
I would like to display the current amount of bullets in my clip by graphics instead of numbers only. I really don't know how to explain this, but here is a picture. As you can see in the upper right hand corner there are some cylinder shaped objects, those indicate the amount of bullets in the current clip. When the bullets diminish so does the ammo indicatior in the upper right hand corner.
I looked at the FPS tutorial and it seemed like it used many textures to display each number of bullets left. So if there is 19 bullets left the FPS tut had a texture sheet of 19 bullets and rendering that on the screen.
However this approach of having many textures seems a bit ridiculous. I imagined having only one bullet texture and drawing them a x amount of space between each other. However I cannot figure out how tackle this approach.
I really hope you get what I am saying.
Marry christmas!
Answer by brandonsbarber · Jun 20, 2013 at 03:22 PM
I know this is a long time after you asked your question, but I would just like to comment with one approach you can take in case anyone is interested.
Assuming you want to use GUITextures
as opposed to doing everything within a void OnGUI()
function, you can do this (though the idea is similar for OnGUI
).
Essentially, what I would do to minimize the number of allocations necessary would be to, in the Start
function, create an array of GUITextures
matching the max clip size you would need. I would define a constant just so that you can easily edit this later.
For instance: private const int MAX_CLIP_SIZE = 10;
private GUITexture[] clipTextures;
void Start()
{
clipTextures = new GUITexture[MAX_CLIP_SIZE];
}
Continuing on, I would create a loop to populate this array, creating empty GameObjects
and adding GUITextures to them. In one of my projects, I created a function to easily do this for me. I would also recommend having a parent object for all of these.
My function would do the following things.
Create GameObject and rename based on a specific naming convention (for this, I would choose to say "HUD_Bullet_01" with
01
representing the index in the array.Set position of the
GameObject
toVector3.forward * whatever z layering you want
. Set scale toVector3.zero
.Parent the GameObject to wherever you want.
Create a
GUITexture
component and save it.GUITexture texture = myObject.AddComponent("GUITexture") as GUITexture;
Set the
texture.texture
to whatever image you have.Set the pixel inset based on whatever positioning you need them to have (or you can do this later, depending on where you are executing this code)
Put these GUITexture objects into our array. Then, whenever someone fires a bullet (and I'm assuming you have kept track of the number in the clip), access the affected texture object in the array and set enabled
to false.
As for generating where these textures appear, just loop through with either a single or a nested for
loop (depending on if you want multiple rows). So, assuming I had a bullet texture that was 5 x 10 px
, then I would say: //This is assuming you have 50 bullet slots
for(int y = 0; y < 5; y++) //loops through y spots
{
for(int x = 0; x < 10; x++) //loops through x spots
{
//Here we have padding of 1 pixel on both sides.
//This is horrible coding practice, FYI, to use magic numbers like this.
clipTextures[y*10+x].pixelInset = new Rect((5+1)*x,(10+1)*y,5,10);
}
}
Hope that this helps you or anyone who is still interested in this problem!