Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to differentiate a tap or a hold on the UI button

Discussion in 'UGUI & TextMesh Pro' started by Deadshot1994, Mar 30, 2021.

  1. Deadshot1994

    Deadshot1994

    Joined:
    Sep 6, 2015
    Posts:
    25
    Hi,

    I have an app and in this app, I have a camera button that I would like to take a picture/video depending on whether the user tapped/held the camera button.

    I have tried many things, but I, unfortunately, don't have a very good and working solution.

    Here's a snippet of my code:

    Code (CSharp):
    1.  
    2. if (Input.touchCount > 0)
    3.         {
    4.             foreach (Touch t in Input.touches)
    5.             {
    6.                 if (t.phase == TouchPhase.Began && RectTransformUtility.RectangleContainsScreenPoint(cameraButton, t.position))
    7.                 {
    8.                     replayCam.StartTimerTrack();
    9.                 }
    10.                 if (t.phase == TouchPhase.Stationary && RectTransformUtility.RectangleContainsScreenPoint(cameraButton, t.position))
    11.                 {
    12.                     isHolding = true;
    13.                 }
    14.                 if (t.phase == TouchPhase.Ended)
    15.                 {
    16.                     if (time <= holdTime)
    17.                     {
    18.                         StartCoroutine(replayCam.RecordFrame());
    19.                         time = 0;
    20.                     }
    21.                     if (isHolding)
    22.                     {
    23.                         replayCam.CheckRecording();
    24.                         isHolding = false;
    25.                     }
    26.                 }
    27.             }
    28.         }
    29.  
    Any help is kindly appreciated