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

1 Answer

1

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. Maybe need to re-examine option 1.