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 PaxForce · Oct 24, 2014 at 05:45 AM · inputswitchinputs

Input - is it OK to use SWITCH?

is it ok to use SWITCH-CASE like this or should I use rather if-else? And here goes the most important question: which is more resource efficient? Meaning which uses up less memory? My script works withou the Input.anyKeyDown condition, but it doesn't work with it...why is that?

 void KeyboardControl()
 {
     if(Input.GetKeyDown (KeyCode.R))
     {
         for(int i = 0; i<playerClonesHolder.transform.childCount; i++)
         {
             playerClonesHolder.transform.GetChild(i).FindChild ("dmgCube").GetComponent<DmgCube_scr>().ChargeStaff();
             
         }
     }
     if(Input.anyKeyDown)
     {
         switch(Input.inputString)
         {
         case "1":
             Debug.Log ("1 pressed");
             break;
         case "2":
             Debug.Log ("2 pressed");
             break;
         case "3":
             Debug.Log ("3 pressed");
             break;
         case "4":
             Debug.Log ("4 pressed");
             break;
         case "5":
             Debug.Log ("5 pressed");
             break;
         case "6":
             Debug.Log ("6 pressed");
             break;
         case "7":
             Debug.Log ("7 pressed");
             break;
         case "8":
             Debug.Log ("8 pressed");
             break;
         case "9":
             Debug.Log ("9 pressed");
             break;
         case "0":
             Debug.Log ("0 pressed");
             break;
         default:
             Debug.Log ("this is not a valid key");
             break;
         }

     }
 }
Comment
Add comment · Show 4
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 Habitablaba · Oct 24, 2014 at 05:51 AM 2
Share

I'm impressed that you've gotten to the point in your project where you're optimizing down to the difference between a switch and an if. I'm also impressed that you've managed to pin point your biggest potential performance gain to your switch statement.

avatar image PaxForce · Oct 24, 2014 at 05:55 AM 0
Share

I'm impressed by your sarcasm.

avatar image Habitablaba · Oct 24, 2014 at 05:59 AM 2
Share

Alrighty, a less snarky response, then.
Really what it comes down to is personal preference. I, for example, tend towards switch statements in cases (pun intended) like you've illustrated above. I find it to be easier to read and maintain than a giant if/else if/else chain.

It also comes down to the specific problem you are trying to solve. You may find that there is no way to make a switch/case block to suit your needs, and thus are stuck with the if/else.

But again, all things equal, I tend towards the switch.

As far as performance of the two, I have no actual idea which is faster or why, nor do I find that I care. If you are seeing performance problems, the chances that they are tied to your choice of if or switch are so low it isn't even worth considering.

avatar image PaxForce · Oct 24, 2014 at 06:06 AM 0
Share

Thank you for the constructive comment. It may seem I keep asking dumb questions, but if that's the "case", it is because I'm a beginner and I want to make sure I'm doing right everything there is to be done. So if you are absolutely sure there's only negligible difference in resource efficiency, make your comment an answer and I'll accept it. Of course I would need you to answer also the next part of my question.

2 Replies

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

Answer by PaxForce · Oct 24, 2014 at 06:29 AM

I've got it:

 for(int i = 0; i<10; i++)
 {
     if(Input.inputString == i.ToString ())
     {
         Debug.Log (i + " pressed");
     }
 }



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 Itaros · Oct 24, 2014 at 08:53 AM 1
Share

performance disaster xD

avatar image
2

Answer by Itaros · Oct 24, 2014 at 06:15 AM

Switch uses numerous optimization techniques like jump padding and selectiion by hashing. If you are using numbers(or enums or explicit numeric enums) for many possible outcomes it is much faster due to how proccessor conveyor works. If you switch by object hashing takes place and this becomes consideration factor requiring benchmarking. Regarding code in the ecxample: switch is the best way by performance and maintainability.

Additionali, I recommend to switch to unity keys enum for max performance gain

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 PaxForce · Oct 24, 2014 at 06:27 AM 0
Share

...dude...I'm a beginner...so please...

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

29 People are following this question.

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

Related Questions

index out of range with length as for loop param 1 Answer

Unity New Input System PerformInteractiveRebinding() 1 Answer

Multiple PlayerInputs fail to trigger actions 0 Answers

How to switch between four characters when a button is pressed? 1 Answer

Issue With Lists and Keycodes 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