Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How to have Input controls setup for Unity Editor and Mobile?

Discussion in 'Scripting' started by farazk86, Aug 14, 2019 at 10:16 AM.

  1. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    33
    I'm making my game for android and only recently have added on screen controls and buttons.

    The problem is that it appears I can only choose one control method and cant have both. I want to have the keyboard controls so that I can still test and develop my game by pressing the play button in Unity but I also want the mobile controls to work when testing on mobile/android.

    For example for the
    Shoot()
    function I have this which works on keyboard and in Unity.

    Code (CSharp):
    1. void Shoot()
    2.     {
    3.         if (Input.GetButtonDown("Fire"))
    4.         {
    5.             if (MoreMountains.InfiniteRunnerEngine.SoundManager.Instance != null)
    6.             {
    7.                 // we play that sound once
    8.                 MoreMountains.InfiniteRunnerEngine.SoundManager.Instance.PlaySound(shotGun, transform.position, true, 0.3f);
    9.             }
    10.             GameObject bullet_ = Instantiate(bulletPrefab, transform.position, transform.rotation);
    11.             Destroy(bullet_, 1.0f);
    12.         }
    13.     }
    But whenever I have to test on mobile I have to replace the line
    Input.GetButtonDown("Fire")
    with
    CrossPlatformInputManager.GetButtonDown("Fire")
    for this and the movement functions.

    I know that Unity provides functions like
    Application.platform
    , but that is evaluated at runtime. and I hear its not recommended to have this many checks at runtime.

    Ive tried using

    Code (CSharp):
    1. #if UNITY_ANDROID
    2. //do something
    3. #else
    4. //do something else
    5. #endif
    but for some reason it only reads the first if block and ignores the else.

    I'm sure this is a common scenario. How do you guys handle this?

    Thanks
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,022
    Add your own control class and combine data from multiple sources.

    Code (CSharp):
    1. class MyInputSystem {
    2.   public bool Fire() {
    3.     return Input.GetKey(KeyCode.Space) || myButtons.isPressed;
    4.   }
    5. }
     
    gononono64 likes this.
  3. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    33
    Thanks for the reply, but I dont really understand this. Sorry, How would I implement this?
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    944
    I haven't used CrossPlatformInputManager, and I'm kind of offended if something with that name only works on specific platforms.

    But based on the data you've presented, your easiest option is just to put both of those input checks into the same "if" statement, combined with the logical-or operator ||

    Code (CSharp):
    1. if (Input.GetButtonDown("Fire") ||
    2.     CrossPlatformInputManager.GetButtonDown("Fire"))
    3. {
    4.     // Do Stuff
    5. }
    If you're going to do this a lot, then you probably want to wrap that in a helper function.

    Code (CSharp):
    1. public bool FireButtonDown()
    2. {
    3.     return Input.GetButtonDown("Fire") ||
    4.         CrossPlatformInputManager.GetButtonDown("Fire");
    5. }
    6.  
    7.  
    8.  
    9. if (FireButtonDown())
    10. {
    11.     // Do Stuff
    12. }
    If the name of the button is always the same in both input managers, then you can write a single helper function for all buttons

    Code (CSharp):
    1. public bool GetButtonDown(string buttonName)
    2. {
    3.     return Input.GetButtonDown(buttonName) ||
    4.         CrossPlatformInputManager.GetButtonDown(buttonName);
    5. }
    6.  
    7.  
    8.  
    9. if (GetButtonDown("Fire"))
    10. {
    11.     // Do Stuff
    12. }
    And of course, if you want to get really fancy, you could expand that out into a whole intermediate input processing layer that converts Unity's raw inputs into whatever "logical" inputs you want to think about when actually writing your input-handling code. That's probably a bit beyond the scope of this thread, though.



    Also, note that #if UNITY_ANDROID will be true inside the editor if the editor's target platform is currently set to Android. If you want something to work specifically in the editor, you can use #if UNITY_EDITOR
     
    gononono64 and farazk86 like this.
  5. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    33

    These are very helpful. Many thanks :)