Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question How to make a on screen button tap?

Discussion in 'Input System' started by Gamrek, Jan 11, 2021.

  1. Gamrek

    Gamrek

    Joined:
    Sep 28, 2010
    Posts:
    164
    I am trying to make a on screen button tap, which only trigger the control when user does a quick tap, if press, hold and release will do nothing. I can't get it working with my custom on screen button script. Here is what I have. It seems like when using "SendValueToControl" will not work outside the interface methods.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.EventSystems;
    4. using UnityEngine.InputSystem.Layouts;
    5. using UnityEngine.InputSystem.OnScreen;
    6. using UnityEngine.InputSystem;
    7.  
    8. public class MyOnScreenTap : OnScreenControl, IPointerDownHandler, IPointerUpHandler
    9. {
    10.     [SerializeField]
    11.     float tapHoldTime = 0.5f;
    12.     bool touching = false;
    13.     float timer;
    14.  
    15.     public void OnPointerUp(PointerEventData eventData)
    16.     {
    17.         if (touching)
    18.         {
    19.             if (timer <= tapHoldTime)
    20.             {
    21.                 StartCoroutine(SendValueToControl());
    22.             }
    23.             timer = 0f;
    24.             touching = false;
    25.         }
    26.     }
    27.  
    28.     IEnumerator SendValueToControl()
    29.     {
    30.         SendValueToControl(1.0f);
    31.         yield return null;
    32.         SendValueToControl(0f);
    33.     }
    34.  
    35.     public void OnPointerDown(PointerEventData eventData)
    36.     {
    37.         touching = true;
    38.         timer = 0f;
    39.     }
    40.  
    41.     private void OnEnable()
    42.     {
    43.         touching = false;
    44.         timer = 0f;
    45.     }
    46.  
    47.     private void OnDisable()
    48.     {
    49.         touching = false;
    50.         timer = 0f;
    51.         SendValueToControl(0f);
    52.     }
    53.  
    54.     //Update is called once per frame
    55.     void Update()
    56.     {
    57.         if (!touching) return;
    58.         timer += Time.deltaTime;
    59.     }
    60.  
    61.     ////TODO: pressure support
    62.     /*
    63.     /// <summary>
    64.     /// If true, the button's value is driven from the pressure value of touch or pen input.
    65.     /// </summary>
    66.     /// <remarks>
    67.     /// This essentially allows having trigger-like buttons as on-screen controls.
    68.     /// </remarks>
    69.     [SerializeField] private bool m_UsePressure;
    70.     */
    71.  
    72.     [InputControl(layout = "Button")]
    73.     [SerializeField]
    74.     private string m_ControlPath;
    75.  
    76.     protected override string controlPathInternal
    77.     {
    78.         get => m_ControlPath;
    79.         set => m_ControlPath = value;
    80.     }
    81. }
    Does anyone have a solution for this?