- Home /
GUI dependent on an int variable
Hello again,
I have made an ammo script that keeps track of the amount of ammo that the gun has and displays it using GUI.Label
this is the current script I have:
var projectile : Transform; var shootSpawn : Transform; var clips : int = 10; // how many clips the player has var clipSize : int = 6; // how many bullets in 1 clip private var ammo : int; // how many bullets left in the current clip
function Start() { ammo = clipSize; // Start with this many bullets. }
function Update() { if(Input.GetButtonDown("Fire1")) { if(ammo > 0) { var clone:Transform = Instantiate(projectile, shootSpawn.position, shootSpawn.rotation); ammo--; // Subtract 1 bullet } else { Reload(); } } }
function Reload() { if(clips > 0) { clips--; ammo = clipSize; } else { // No more clips left... } }
function OnGUI () { GUI.Label (Rect (5, 20, 200, 50), "I"ammo + ""(clipSize - ammo)); GUI.Label (Rect (5, 5, 200, 50), ""+clips); }
this works perfectly in calculating the clips and bullets however i want to display the GUI differently.
At the moment it shows a bullet as an 'I' but i want to have an image for each bullet,
1) should i make an image of a single bullet and show it as many times as there are bullets or should I make a texture with a whole clip of bullets and shrink the GUI to show one less each time
2) how would i implement this using my script, I'm guessing I need to use GUI.DrawTexture or possibly GUITexture but how would I make it produce a row of Images dependent on the 'ammo' variable of my script
Thanks for any help you can give
Scribe
P.S I want to keep the number of clips displayed as a number I only want to change the ammo GUI
Answer by DaveA · Mar 25, 2011 at 08:10 PM
You know, I was going to go with option 1, but considering how GUI is coded, option 2 might be easier after all. Because you'd only have to make about one GUI call to render that.
I suppose you could render a grid or toolbar, but those are overkill for your purposes.
thanks for the reply, I have tried shrinking a texture but i can never get it to cut off part of it, only to compress it or cut the same amount from both sides. Would you know how I would set up a GUI to do this
I've not tried it, but GUITexture inherits from Texture, and you can play with the wrapmode on that. If you set it to Repeat, maybe?
It's like you want to get at the UV's of it. Seems like there should be a way. A worst-case would be to use many textures, each with the number of bullets in it, and swap those. Ech. $$anonymous$$aybe need to re-exa$$anonymous$$e option 1.
Hey guys, I thought I'd post the working script incase its of use to anyone so here it is:
var projectile : Transform; var shootSpawn : Transform; var clips : int = 5; // how many clips the player has var clipSize : int = 4; // how many bullets in 1 clip var ammo : int; // how many bullets left in the current clip var TexSize = 16; //whole texsize / clipsize var bulletTex : Texture2D;
function Start() { ammo = clipSize; // Start with this many bullets. }
function Update() { if(Input.GetButtonDown("Fire1")) { if(ammo > 0) { var clone:Transform = Instantiate(projectile, shootSpawn.position, shootSpawn.rotation); ammo--; // Subtract 1 bullet } else { Reload(); } } }
function Reload() { if(clips > 0) { clips--; ammo = clipSize; // Play sound/animation ? } else { // No more clips left... } }
function OnGUI () { GUI.BeginGroup (new Rect(5, 5, TexSize*ammo, 53)); //TexSize*number GUI.DrawTexture(new Rect (0, 0, 64, 53), bulletTex, Scale$$anonymous$$ode.ScaleToFit); GUI.EndGroup (); //GUI.Label (Rect (5, 20, 200, 50), "I"ammo + ""(clipSize - ammo)); GUI.Label (Rect (5, 5, 200, 50), ""+clips); }
I ended up making a GUI Group with my texture in it as sizing the group doesn't stretch/compress the textures drawn inside so it allows for resizing of the texture
I used a texture with as many bullets draw as there are in my clip which as its a sniper was only four. I then made it the right pixel size in photoshop which ended up being 64x53 in my case.
I created 2 new variables: bulletTex was the Texture2D of my bullets texture and TexSize which is equal to (the pixel width of the texture)/clipSize as this gets the pixel width per bullet drawing on my texture.
I then just made the width of my GUI group = TexSize (one bullet pixel width) times ammo (how many bullets left in the clip)
hope this is of help to someone and thanks DaveA for your help, I have accepted and voted up your answer :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613114825im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer
GUITexture help, can't drag and drop from hierarchy? 0 Answers
How will i get animated gif images in scene? 6 Answers
GUI texture touch input problem 1 Answer
How can I move Text and Image component in unison inside of Canvas 0 Answers