Search Unity

Access wildcarded multiple touches

Discussion in 'Input System' started by Rs, Jan 16, 2021.

  1. Rs

    Rs

    Joined:
    Aug 14, 2012
    Posts:
    74
    If I set the binding path to <Touchscreen>/touch*/press like the docs say, how do I access the touches array in code?

    For context, I've generated the class for the input asset (ARInputActions). In the Action Editor I have (to simplify) 1 action called ScreenTouches which is set to Action Type = Pass Through and Control Type = Any.

    Here's the skimmed class that should access touches via Input Actions. See the Debug.Log that reads with an Hamletic tone and translates to: how do I extract all touches from the context?

    But I guess the question could be generalized to how do I access wildcarded Actions?

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Assertions;
    6. using UnityEngine.XR.ARFoundation;
    7. using UnityEngine.InputSystem;
    8. using UnityEngine.InputSystem.Controls;
    9.  
    10. public class ARInteractor : MonoBehaviour
    11. {
    12.     ARInputActions arInputActions;
    13.  
    14.    void OnEnable() {
    15.         arInputActions.Enable();
    16.     }
    17.  
    18.     void OnDisable() {
    19.         arInputActions.Disable();
    20.     }
    21.  
    22.     void Awake()
    23.     {
    24.         arInputActions = new ARInputActions();
    25.     }
    26.  
    27.      void Start() {
    28.      
    29.         arInputActions.TouchActions.TouchPress.performed += context => { Debug.Log("WHAT TO DO? WHAT TO DOOOO?"); };
    30.  
    31.     }
    32.  
    33. }
    34.  
     
    Last edited: Jan 16, 2021
  2. Rs

    Rs

    Joined:
    Aug 14, 2012
    Posts:
    74
    To add to that, I half-blindly tried the following hoping it would give some results but I get exceptions due to wrong type being given:

    Code (CSharp):
    1.  
    2. // does it send one TouchControl value? (Error: TValue must be non-nullable)
    3. arInputActions.TouchActions.TouchPress.performed += _ => { Debug.Log(_.ReadValue<TouchControl>().touchId); }
    4. // does it send one touch at a time? (Runtime errors)
    5. arInputActions.TouchActions.TouchPress.performed += _ => { Debug.Log("Touch #: " + ((TouchControl)_.ReadValueAsObject()).touchId); };
    6. // does it send all touches at once? (Runtime errors)
    7. arInputActions.TouchActions.TouchPress.performed += _ => { Debug.Log("N Touches: " + _.ReadValue<ReadOnlyArray<TouchControl>>().Count); };
    8.  
    (Read the "_" as context)

    In particular, I expected ReadValue<TouchControl> to work but the method wants a non-nullable type so TouchControl isn't accepted.
     
    Last edited: Jan 16, 2021