Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Scribe · Mar 25, 2011 at 02:30 PM · guiimageguitextureammo

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Scribe · Mar 25, 2011 at 10:01 PM 0
Share

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

avatar image DaveA · Mar 25, 2011 at 10:38 PM 0
Share

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?

avatar image DaveA · Mar 25, 2011 at 10:39 PM 0
Share

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.

avatar image Scribe · Mar 26, 2011 at 10:56 AM 2
Share

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 :)

avatar image DaveA Scribe · Mar 26, 2011 at 09:30 PM 0
Share

Glad I could help at all

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges