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 MxRx · Aug 21, 2012 at 08:52 PM · guiinput

Rewriting a bullet count GUI to run more efficiently

ive written this code to create individual bullet icons on the screen space. Each time the weapon is fired via an Input the bullets are removed one at a time. My question is what is a better way to write this code. Im obviously new to scripting and dont quite understand how to smash code down to make it efficient.

 #pragma strict
 
 var shell : Texture;
 var grayShell : Texture;
 var numBullets : int = bulletTotal;
 var bulletTotal : int = 7;
 var screenPosX : int = 565;
 var screenPosY : int = 1040;
 
 
 
 function OnGUI () {
 
     /*
     for (var i = 0; i < numBullets; i++)
         GUI.DrawTexture ( Rect (800, 550 + i * shell.height, shell.width, shell.height) , shell, ScaleMode.ScaleAndCrop, true);
     */
     
     GUI.Box (Rect (869, 500, 200, 100), "ammoCounter");
     
     if (bulletTotal > 0){ //Gray Shells
         GUI.DrawTexture (Rect ( screenPosY, screenPosX,  grayShell.width, grayShell.height), grayShell, ScaleMode.ScaleToFit, true);
         if (bulletTotal > 1){
             GUI.DrawTexture (Rect ( screenPosY -20, screenPosX,  grayShell.width, grayShell.height), grayShell, ScaleMode.ScaleToFit, true);
             if (bulletTotal > 2){
                 GUI.DrawTexture (Rect ( screenPosY -40, screenPosX,  grayShell.width, grayShell.height), grayShell, ScaleMode.ScaleToFit, true);
                 if (bulletTotal > 3){
                     GUI.DrawTexture (Rect ( screenPosY -60, screenPosX,  grayShell.width, grayShell.height), grayShell, ScaleMode.ScaleToFit, true);
                     if (bulletTotal > 4){
                         GUI.DrawTexture (Rect ( screenPosY -80, screenPosX,  grayShell.width, grayShell.height), grayShell, ScaleMode.ScaleToFit, true);
                         if (bulletTotal > 5){
                             GUI.DrawTexture (Rect ( screenPosY -100, screenPosX,  grayShell.width, grayShell.height), grayShell, ScaleMode.ScaleToFit, true);
                             if (bulletTotal > 6){
                                 GUI.DrawTexture (Rect ( screenPosY -120, screenPosX,  grayShell.width, grayShell.height), grayShell, ScaleMode.ScaleToFit, true);
                                 if (bulletTotal > 7){
                                     GUI.DrawTexture (Rect ( screenPosY -140, screenPosX,  grayShell.width, grayShell.height), grayShell, ScaleMode.ScaleToFit, true);
                                 }
                             }
                         }        
                     }
                 }
             }
         }
     }    
     
     if (numBullets > 0){
         GUI.DrawTexture (Rect ( screenPosY, screenPosX,  shell.width, shell.height), shell, ScaleMode.ScaleToFit, true);
         if (numBullets > 1){
             GUI.DrawTexture (Rect ( screenPosY -20, screenPosX,  shell.width, shell.height), shell, ScaleMode.ScaleToFit, true);
             if (numBullets > 2){
                 GUI.DrawTexture (Rect ( screenPosY -40, screenPosX,  shell.width, shell.height), shell, ScaleMode.ScaleToFit, true);
                 if (numBullets > 3){
                     GUI.DrawTexture (Rect ( screenPosY -60, screenPosX,  shell.width, shell.height), shell, ScaleMode.ScaleToFit, true);
                     if (numBullets > 4){
                         GUI.DrawTexture (Rect ( screenPosY -80, screenPosX,  shell.width, shell.height), shell, ScaleMode.ScaleToFit, true);
                         if (numBullets > 5){
                             GUI.DrawTexture (Rect ( screenPosY -100, screenPosX,  shell.width, shell.height), shell, ScaleMode.ScaleToFit, true);
                             if (numBullets > 6){
                                 GUI.DrawTexture (Rect ( screenPosY -120, screenPosX,  shell.width, shell.height), shell, ScaleMode.ScaleToFit, true);
                                 if (numBullets > 7){
                                     GUI.DrawTexture (Rect ( screenPosY -140, screenPosX,  shell.width, shell.height), shell, ScaleMode.ScaleToFit, true);
                                 }
                             }
                         }        
                     }
                 }
             }
         }
     }    
 
 }

the gray shells part is only to show an empty.

Thank you in advance for the help

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
Best Answer

Answer by ScroodgeM · Aug 21, 2012 at 10:41 PM

#pragma strict

var shell : Texture; var grayShell : Texture; var numBullets : int = bulletTotal; var bulletTotal : int = 7; var screenPosX : int = 565; var screenPosY : int = 1040;

function OnGUI () { GUI.Box (Rect (869, 500, 200, 100), "ammoCounter"); for (var i = 0; i < bulletTotal; i++) { DrawTexture(i, numBullets > i ? shell : grayShell); } }

function DrawTexture(i : int, t : Texture2D) { GUI.DrawTexture (Rect ( screenPosY - 20 * i, screenPosX, t.width, t.height), t, ScaleMode.ScaleToFit, true); }

Comment
Add comment · Show 1 · 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 MxRx · Aug 22, 2012 at 01:15 AM 0
Share

thanks Scroodge, that was exactly what i needed

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

gui list problem 2 Answers

GUI texture touch input problem 1 Answer

Updating GUI text with GetButtonDown? 1 Answer

how to input words from array to GUI? 0 Answers

GUI Text Area that Reads / Writes / Saves 2 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