Skip to content

Pointer Input

Valentin Simonov edited this page Jul 30, 2017 · 4 revisions

TouchScript can work with many types of input sources, so a touch is generally called pointer in the library. At the moment of writing TouchScript can work with the following pointers: mouse, touch, pen, object.

Pointers are registered by Input Sources and can come from any type of input device. TouchScript can be used just as an input mechanism for these pointers, ignoring all gesture recognition logic.

Adding pointer input

To enable TouchScript in your scene you generally want to put at least one component from TouchScript package into this scene. It will pull and initialize all the pointer recognition logic at run time.

But the best way to do this is to add TouchManager component to your scene. You can use it to store TouchScript configuration per scene.

Another component you will need is StandardInput which will be added automatically if your scene doesn't contain one. After you have an Input Source in the scene, you can receive pointer input. To do this you need to subscribe to TouchManager's events, like so:

private void OnEnable()
{
    if (TouchManager.Instance != null) 
        TouchManager.Instance.PointersPressed += pointersPressedHandler;
}

private void OnDisable()
{
    if (TouchManager.Instance != null) 
        TouchManager.Instance.PointersPressed -= pointersPressedHandler;
}

As you can see, current instance of TouchManager singleton can be accessed via TouchManager.Instance property.

When using C#, a script should subscribe to TouchScript events in OnEnable() method and unsubscribe in OnDisable() to prevent memory leaks. Handlers you assign to touch events is where you can extend TouchScript to do the touch interaction that you'd like. Here's an example that implements the pointersPressedHandler above.

private void pointersPressedHandler(object sender, PointerEventArgs e)
{
    foreach (var pointer in e.Pointers) 
        Debug.Log(pointer.Id + " touched down at " + pointer.Position);
}
You can’t perform that action at this time.