0
votes

I'm trying to get touch events in the editor. However I'm not getting any events from the input action.

I have a very simple script:

using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.EnhancedTouch;
 
public class GameManager : MonoBehaviour
{
    public InputAction _touch;
 
    void Awake()
    {
        TouchSimulation.Enable();
 
        _touch.started += OnTouch;
        _touch.performed += OnTouch;
        _touch.canceled += OnTouch;
    }
 
    void OnTouch(InputAction.CallbackContext context)
    {
        Debug.Log(context.ReadValueAsObject());
    }
}

So a simple InputAction _touch and I subscribe to all its events. I also enable touch simulation in the Awake function using TouchSimulation.Enable().

Then in the inspector I set it up like this:

Inspector

The settings of the Input Action are:

Input Action

And those of the Primary Touch are:

Primary Touch

When I click and drag my mouse in the game view nothing happens, no events are fired.

What am I doing wrong? What more do I need for getting touch events and simulating them in the editor?

I'm running Unity 2019.4.

Thanks!

2

2 Answers

1
votes

I found the problem, apparently you need to enable the input action first:

void Awake()
{
    _touch.started += OnTouch;
    _touch.performed += OnTouch;
    _touch.canceled += OnTouch;
    _touch.Enable();
}

It might also be a good idea to enable/disable in the OnEnable and OnDisable callbacks for the monobehavour.

0
votes

Quote from the documentation: https://docs.unity3d.com/Packages/[email protected]/manual/Touch.html

Touch Simulation Touch input can be simulated from input on other kinds of Pointer devices such as Mouse and Pen devices. To enable this, you can either add the TouchSimulation MonoBehaviour to a GameObject in your scene or simply call TouchSimulation.Enable somewhere in your startup code.

void OnEnable()
{
    TouchSimulation.Enable();
} 

In the editor, you can also enable touch simulation by toggling "Simulate Touch Input From Mouse or Pen" on in the "Options" dropdown of the Input Debugger.

TouchSimulation will add a Touchscreen device and automatically mirror input on any Pointer device to the virtual touchscreen device.