How to "Initialize" Input System?

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:

    void Start()
    {
        EnhancedTouchSupport.Enable();
    }

    void Update()
    {
        print(Touch.activeTouches.Count);
    }

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.

            GUILayout.BeginHorizontal();
            {
                EnvironmentIndex = GUILayout.Toolbar(EnvironmentIndex, Environments, GUILayout.Width(200), GUILayout.Height(25));
            }
            GUILayout.EndHorizontal();

Can anyone from Unity InputSystem team tell me what special operation the old UI system doing to initialize the new UI system please?

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

Alternative way, if you don’t want to use Input Debugger, you can use Simulator.
9299936--1302845--upload_2023-9-13_15-51-37.png

Thank you but the problem I described happens in the app build on the iOS device; not specific to Unity Editor.

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?

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.

Maybe it’s a bug. Try to redo it in a new project, or using another version of Unity or Input System.

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.

using UnityEngine;

// This class is a hack.
// Normally we don't need to initialize InputSystem. We just need to enable an ActionMap in the InputSystem and it starts working as expected.
// But on iOS, for some reason, we can't get the correct behaviour from Touch.activeTouches.Count which is required for TouchConsolePro.
// It reports 0 (zero) even when we touch the screen as if the input system was not initialized.
// Interestingly, drawing a UI object using Unity's old UI system automatically initializes the input system and Touch.activeTouches starts working fine.
// With the script below, we are forcing the input system to be initialized on iOS.
// The problem was discussed here: https://discussions.unity.com/t/928611
// If we find a proper way to initialize the input system on iOS, we can get rid of this hack.
public class InputSystemInitializer : MonoBehaviour
{
    void OnGUI()
    {
        GUILayout.Label(".");
        Destroy(this);
    }
}
2 Likes