Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by MadKiddocreater · Feb 15, 2017 at 01:20 PM · performance optimizationswitchif

If vs Switch

I have following code, how do I convert it into switch and would it make any difference in performance.

Comment
Add comment · Show 2
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 MadKiddocreater · Feb 15, 2017 at 01:24 PM 0
Share
 void Update ()
     {
 
 
         AnimatorStateInfo currentState = anim.GetCurrentAnimatorStateInfo (0);
 
         if (currentState.IsName ("Run")) {
 
 
             soldier.velocity = new Vector3 (0,0, -moveSpeed);
             //mf.SetActive (false);
 
 
 
 
         }
         if (currentState.IsName ("StrafeLeft")) 
         {
             soldier.velocity = new Vector3 (-moveSpeed,0,0);
 
             //mf.SetActive (true);
         
         }
         if (currentState.IsName ("StrafeRight")) 
         {
             soldier.velocity = new Vector3 (moveSpeed,0,0);
             //mf.SetActive (true);
         
         }
 
         if (currentState.IsName ("RunFire")) {
 
 
             soldier.velocity = new Vector3 (0,0, -moveSpeed);
             //mf.SetActive (true);
         
         
         }
         if (currentState.IsName ("RunBackFire")) {
 
 
             soldier.velocity = new Vector3 (0, 0, moveSpeed);
             //mf.SetActive (true);
 
         }
         if (currentState.IsName ("Fire")) {
 
 
             soldier.velocity = new Vector3 (0, 0 ,0);
         //    mf.SetActive (true);
 
         }
         if (currentState.IsName ("Die")) {
 
 
             soldier.velocity = new Vector3 (0, 0 ,0);
             //mf.SetActive (true);
             }
 
 
 
     }
avatar image Owen-Reynolds · Feb 15, 2017 at 04:54 PM 0
Share

No , it won't run faster.

2 Replies

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

Answer by Tourist · Feb 15, 2017 at 02:21 PM

 switch(myString)
 {
      case "Test": break;
      case "Other string": break;
 }

As for the performances, I'll let you do your tests. However, I would highly recommend that you keep the hashed name of each state you want to test using Animator.StringToHash and used the hashed integers as based of the switch. It would clearly be the most efficient way.

Comment
Add comment · Show 2 · 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 MadKiddocreater · Feb 15, 2017 at 02:45 PM 0
Share

i'm very weak in coding , could you please give me a full elaborated code please.

avatar image Tourist MadKiddocreater · Feb 16, 2017 at 09:54 AM 0
Share

In your class :

 private static readonly int runId = Animator.StringToHash("Run");
 .... // do the same for all states you want to check

In your function :

 AnimatorStateInfo currentState = anim.GetCurrentAnimatorStateInfo (0);
 int fullPathHash = currentState.fullPathHash;
 if(fullPathHash == runId)
 {
   // Do run stuff
 }
 else if(....)
 ...
avatar image
0

Answer by Kossuranta · Feb 15, 2017 at 02:06 PM

As AnimatorStateInfo doesn't have value for name, just IsName(string) check I can't think of simple way to make it with switch-case. It might be somewhat more efficient in same cases, but difference is really small if you fix your current code to be proper if-elseif-else.

Currently what your code does it will check every if-clause one by one even if the first one is true it will still check all that come after it. As only one can be true at once first one should be if() and all that come after that should be if else().

For example:

 if (currentState.IsName ("Run"))
 {
     soldier.velocity = new Vector3 (0,0, -moveSpeed);
 }
 else if (currentState.IsName ("StrafeLeft")) 
 {
     soldier.velocity = new Vector3 (-moveSpeed,0,0);
 }
 else if (currentState.IsName ("StrafeRight")) 
 {
     soldier.velocity = new Vector3 (moveSpeed,0,0);
 }

This code would first check if the name is "Run" and if it is true it will skip all the rest. Only if it is false it will continue to check the next one. First one will be fastest as there is only one check so you could also do small optimization by ordering the list. Technically settings the most heaviest option as first would give most stable framerates, but in practice you probably wont even notice the difference.

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

68 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Button bool doesn´t work correctly weird 1 Answer

Refactoring Nested Switch Statements 0 Answers

How to use WaitForSeconds? 2 Answers

Unparalleled problem with if-statement 0 Answers

My "if" statement doesn't work? 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