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 /
avatar image
0
Question by Khena_B · Apr 22, 2017 at 02:07 PM · c#ifif statement

If statement question

Hey,

Is it possible to simplify :

 if ((Input.GetButtonDown("Attack") || Input.GetButtonDown("Escape") || Input.GetButtonDown("Start")) || Input.GetButtonDown("Select"))

Isn't there a way to just have the Input.GetButtonDown once, something like :

 if (Input.GetButtonDown("Attack" || "Escape" || "Start" || "Select"))

Thanks.

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

3 Replies

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

Answer by Kishotta · Apr 22, 2017 at 07:47 PM

This is possible with a few variables. It doesn't get rid of the Input calls, but it does simplify the branch condition.

 bool Attack = Input.GetButtonDown ("Attack");
 bool Escape= Input.GetButtonDown ("Escape");
 bool Start= Input.GetButtonDown ("Start");
 bool Select= Input.GetButtonDown ("Select");
 
  if (Attack || Escape || Start || Select) {
     // ...
 }
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
avatar image
0

Answer by kritoa · Apr 23, 2017 at 07:29 AM

No. There are some specific things (like layers) where you can bitwise AND and OR things together like that but not for something like this.

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

Answer by Guichaoua · Apr 23, 2017 at 07:36 AM

Try this:

 using System.Linq
  public static bool GetAllButtonsDown(params string[] buttons)
 {
     return buttons.All( b => Input.GetButtonDown (b));
 }
 public static bool GetOneButtonsDown (params string[] buttons)
 {
     var check = (b) => Input.GetButtonDown (b);
     foreach (var but in buttons)
     if (check (but)){
         return true;
     }
     return false;
 }
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 Bunny83 · Apr 23, 2017 at 11:06 AM 3
Share

While this is possible it's quite inefficient. Why do you actually wrap GetButtonDown in a lambda expression which you recreate everytime you call that method? This approach creates quite a bit of garbage each time you call it. First the "params array", second the unnecessary lambda and finally the foreach IEnumerable.

$$anonymous$$ost methods that provide a params array have multiple specific overloads for 1,2,3 and 4 parameters and the params array is only used if more parameters are needed. In case of strings you could simply do:

 public static bool GetAnyButtonDown(string b1, string b2 = null, string b3 = null, string b4=null)
 {
     if (b1 != null && Input.GetButtonDown(b1)) return true;
     if (b2 != null && Input.GetButtonDown(b2)) return true;
     if (b3 != null && Input.GetButtonDown(b3)) return true;
     if (b4 != null && Input.GetButtonDown(b4)) return true;
     return false;
 }
 public static bool GetAnyButtonDown(params string[] more)
 {
     if (more == null)
         return false;
     for (int i = 0; i < more.Length; i++)
         if (Input.GetButtonDown(more[i]))
             return true;
     return false;
 }

Ins$$anonymous$$d of default parameters for b2, b3 and b4 you could create seperate overloads to $$anonymous$$imize the overhead, but it would be a micro optimisation. You should avoid using this with more than 4 parameters as it requires the compiler to create an array each time the method is called. If you frequently need more than 4 you may want to create more overloads.

However if you need this only one or two times, there's no point in creating such a method in the first place. The fastest and most readable implementation is:

     if (Input.GetButtonDown("Button1") ||
         Input.GetButtonDown("Button2") ||
         Input.GetButtonDown("Button3") ||
         Input.GetButtonDown("Button4")
         )
     {
         // do something
     }

If you use a proper IDE (like Visual Studio) it will even format it for you. Visual Studio can be heavily customized what style it uses for auto formatting.

avatar image meat5000 ♦ Bunny83 · Apr 23, 2017 at 12:01 PM 1
Share

Where would we be without your expertise?! You're awesome, just in case you hadn't been told lately.

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

311 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 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 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 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 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Conflicting Animator SetTrigger 0 Answers

Make script affect only one GameObject instead of making all object with this script be affected. 0 Answers

How to get around adding loads of "if" commands in my script 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