Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to "Initialize" Input System?

Discussion in 'Input System' started by Xtro, Sep 7, 2023.

  1. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    596
    From the title, it may look like I'm a newbie to the new InputSystem but actually I'm quite experienced with it. I have been using a relatively complex ActionMap system in my standalone (Windows) based game for a long time.

    Recently, I started to use the input system on iOS for the first time and I couldn't find a solution to the problem I'm experiencing.

    For the iOS project, I don't have any action maps setup yet. I have to use Touch system directly, like this:

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         EnhancedTouchSupport.Enable();
    4.     }
    5.  
    6.     void Update()
    7.     {
    8.         print(Touch.activeTouches.Count);
    9.     }
    10.  
    But this code just prints 0 (zero) to the console on iOS device even if I touch the screen with my fingers.

    Later, I noticed that activeTouches.Count starts working fine in a very interesting way.

    To be able to debug the state of my test app, I implemented an old GUI system. As soon as I call some old UI methods, activeTouches.Count starts working.

    It's like old UI system somehow "initializes" the new input system so activeTouches.Count starts working.

    Code (CSharp):
    1.             GUILayout.BeginHorizontal();
    2.             {
    3.                 EnvironmentIndex = GUILayout.Toolbar(EnvironmentIndex, Environments, GUILayout.Width(200), GUILayout.Height(25));
    4.             }
    5.             GUILayout.EndHorizontal();
    6.  
    Can anyone from Unity InputSystem team tell me what special operation the old UI system doing to initialize the new UI system please?
     
  2. xiennastudio

    xiennastudio

    Joined:
    Aug 2, 2023
    Posts:
    19
    From what I have check, to test in Game view, open the Input Debugger from Window > Analysis > Input Debugger.
    Then enable this:
    upload_2023-9-13_15-51-3.png

    Alternative way, if you don't want to use Input Debugger, you can use Simulator.
    upload_2023-9-13_15-51-37.png
     
    Last edited: Sep 13, 2023
  3. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    596
    Thank you but the problem I described happens in the app build on the iOS device; not specific to Unity Editor.
     
  4. xiennastudio

    xiennastudio

    Joined:
    Aug 2, 2023
    Posts:
    19
    That's weird as there should not be any extra implementation needed for mobile device. On my side I don't need to use the GUILayout. Does it work fine in the Editor without the GUILayout?
     
  5. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    596
    I tried but couldn't test it in the editor. With the touch simulation enabled, Touch count is always zero in editor play mode, with or without my GUILayout hack. Weird.
     
  6. xiennastudio

    xiennastudio

    Joined:
    Aug 2, 2023
    Posts:
    19
    Maybe it's a bug. Try to redo it in a new project, or using another version of Unity or Input System.
     
  7. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    596
    Thank you for trying to help. I may have go deeper investigation if I have some free time but for now I'll live with this quick hack I implemented.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. // This class is a hack.
    4. // Normally we don't need to initialize InputSystem. We just need to enable an ActionMap in the InputSystem and it starts working as expected.
    5. // But on iOS, for some reason, we can't get the correct behaviour from Touch.activeTouches.Count which is required for TouchConsolePro.
    6. // It reports 0 (zero) even when we touch the screen as if the input system was not initialized.
    7. // Interestingly, drawing a UI object using Unity's old UI system automatically initializes the input system and Touch.activeTouches starts working fine.
    8. // With the script below, we are forcing the input system to be initialized on iOS.
    9. // The problem was discussed here: https://forum.unity.com/threads/how-to-initialize-input-system.1489489/
    10. // If we find a proper way to initialize the input system on iOS, we can get rid of this hack.
    11. public class InputSystemInitializer : MonoBehaviour
    12. {
    13.     void OnGUI()
    14.     {
    15.         GUILayout.Label(".");
    16.         Destroy(this);
    17.     }
    18. }
     
    xiennastudio and halley like this.