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
0
Question by LegionIsTaken · Dec 02, 2011 at 04:51 PM · graphicsguitexturedrawing

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!

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
1

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.

  1. 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.

  2. Set position of the GameObject to Vector3.forward * whatever z layering you want. Set scale to Vector3.zero.

  3. Parent the GameObject to wherever you want.

  4. Create a GUITexture component and save it. GUITexture texture = myObject.AddComponent("GUITexture") as GUITexture;

  5. Set the texture.texture to whatever image you have.

  6. 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!

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Using screencapture to reduce expensive draw calls. 2 Answers

Draw polygon from array of points 1 Answer

How to draw outlines around objects 1 Answer

(Health bar) Remove lives properly 1 Answer

Changing draw order of meshes in Graphics.DrawMesh() 1 Answer


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