x


How to implement an ammo counter using textured quads? [Scripting doubts]

I am trying to implement a counter who will show current ammo in an iphone game (similar to a gui item).

The approach i have been testing is by using 10 quads for each digit (0-9) who all share the same material/texture (to batch them into 1 draw call), the reason for not using a guitext or similar element is that i need a small animation on each of them, similar to a "pop-up" on each digit everytime the counter changes its value.

Example: Lets imagine the maximum ammo i can have at any given time is 99. For this i have a setup of 10 quads for the the first units digit and another 10 quads for the dozens digit. All of them have the renderer disabled, except for the ones who actually show the digits i want.

My doubt: I want to collect an integer variable that represents the current ammo (ie: 24) and separate it into "2" and "4", so that my quad group representing the dozens turns on the renderer showing "2" and the other group shows "4".

Anyone can point me in what to research, or any example of a correct approach? My current code test (limited by scripting knowledge) will soon become a mess Sad (imagine this for 100 cases lol)

    if(ammo==24){

//show "2" on dozens counter
counterA0.renderer.enabled = false;
counterA1.renderer.enabled = false;
counterA2.renderer.enabled = true; //quad showing "2" for dozens
counterA3.renderer.enabled = false;
counterA4.renderer.enabled = false;
counterA5.renderer.enabled = false;
counterA6.renderer.enabled = false;
counterA7.renderer.enabled = false;
counterA8.renderer.enabled = false;
counterA9.renderer.enabled = false;

//show "4" on units counter
counterB0.renderer.enabled = false;
counterB1.renderer.enabled = false;
counterB2.renderer.enabled = false;
counterB3.renderer.enabled = false;
counterB4.renderer.enabled = true; //quad showing "4" for units
counterB5.renderer.enabled = false;
counterB6.renderer.enabled = false;
counterB7.renderer.enabled = false;
counterB8.renderer.enabled = false;
counterB9.renderer.enabled = false;
}

Thanks in advance!

more ▼

asked Feb 21 '10 at 07:30 AM

Septien gravatar image

Septien
3 2 2 5

What kind of "Animation" do you need for your counter? A GUI element would make it much easier and depending on what you need exactly, this could possibly created with some GUI element.

Feb 21 '10 at 10:02 AM Sebas

The animation i am using for the counter is similar to a zoom-in - zoom-out. ie: when a digit changes its value, its quad does a brief animation that changes its scale from 1 to 1.5 to 1 again, then stop.

Feb 21 '10 at 10:41 AM Septien

@Septien, did you got this done ? if so could you contact me , im wondering and been searching around for some time and have not found something like this, maybe its because im not using correct words on saint google, if you could please let me know

Apr 17 '12 at 04:37 PM nonaxanon
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

The solution I give is far from a good one, but it uses the counterA/B0-9 as you've defined and should be easy to understand (works from 0 .. 99 but it should be easy to add a digit).

var digitA = (ammo / 10) % 10;
var digitB = (ammo % 10);

switch (digitA) {
    case 0 : counterA0.renderer.enabled = false; break;
    case 1 : counterA1.renderer.enabled = false; break;
    case 2 : counterA2.renderer.enabled = false; break;
    case 3 : counterA3.renderer.enabled = false; break;
    case 4 : counterA4.renderer.enabled = false; break;
    case 5 : counterA5.renderer.enabled = false; break;
    case 6 : counterA6.renderer.enabled = false; break;
    case 7 : counterA7.renderer.enabled = false; break;
    case 8 : counterA8.renderer.enabled = false; break;
    case 9 : counterA9.renderer.enabled = false; break;
}

switch (digitB) {
    case 0 : counterB0.renderer.enabled = false; break;
    case 1 : counterB1.renderer.enabled = false; break;
    case 2 : counterB2.renderer.enabled = false; break;
    case 3 : counterB3.renderer.enabled = false; break;
    case 4 : counterB4.renderer.enabled = false; break;
    case 5 : counterB5.renderer.enabled = false; break;
    case 6 : counterB6.renderer.enabled = false; break;
    case 7 : counterB7.renderer.enabled = false; break;
    case 8 : counterB8.renderer.enabled = false; break;
    case 9 : counterB9.renderer.enabled = false; break;
}

As I said, this works, with the setup you have now, and maybe for now you should leave it by that until you've learned more about unity and scripting. To give you a lead to a better solution:

Instead to have for every possible value per digit another gameobject a simple optimization would be to have 10 different materials each with another digit and just change the material for the digit gameobject. If you put the 10 materials into an array you can just change the material using the calculated digit (digitA or digitB vars) as index instead of using those long switch statements.

more ▼

answered Feb 21 '10 at 11:22 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

Thanks, Jaap! I understand your point. Regarding the materials, i was using this approach since its an iphone game i just had 1 material/texture for all the quads, so they all got batched into 1 draw call.

Can you just explain me how to read this part of your code? "var digitA = (ammo / 10) % 10; var digitB = (ammo % 10); "

are the "%10" any kind of operators?

Feb 21 '10 at 11:46 AM Septien
(comments are locked)
10|3000 characters needed characters left

what about changing the texture?

like from the docs:

var someTexture : Texture2D; guiTexture.texture = someTexture;

so, define 10 texture for each digit and swap the texture of the tenth and the, well other digit dont know how its called in english ;-)

greetings, thomas

more ▼

answered Feb 21 '10 at 12:57 PM

thomas gravatar image

thomas
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5093
x3694
x2001
x492
x472

asked: Feb 21 '10 at 07:30 AM

Seen: 1705 times

Last Updated: Apr 17 '12 at 04:37 PM