Touch Button

Mobile-friendly UI button with press, release, and hold events plus visual feedback. Includes configurable cooldown and hold duration tracking for responsive touch controls.

File
TouchButton.cs
Unity
2022.3+
Level
beginner
Size
3.1 KB · 128 lines
Added
2026-03-23
License
MIT
View source

No APIs that Unity 6.0 to 6.6 made obsolete or removed. How this is checked

What it adds to your GameObject

A MonoBehaviour component: attach it to a GameObject. This is the Inspector you get, read from the source: 8 serialized fields with their defaults.

Inspector
Script TouchButton
Button Settings
Cooldown Time 0.1
Hold Threshold 0.4
Visual Feedback
Pressed Scale 0.9
Scale Speed 10
Events
On Pressed ()
List is Empty
On Released ()
List is Empty
On Hold Start ()
List is Empty
On Holding (float)
List is Empty

When to use it

Use for mobile game action buttons — shoot, jump, interact, or any button that needs hold-to-charge mechanics. Great for hyper-casual games with single-button controls and mobile RPGs with ability buttons that distinguish between tap and hold.

Setup

  1. Create a UI Button inside a Canvas with an EventSystem in the scene
  2. Attach the TouchButton script to the Button GameObject
  3. Configure cooldown time and hold threshold in the inspector
  4. Wire up onPressed, onReleased, or onHoldStart events to your game actions
  5. Adjust pressedScale for visual press feedback

Features

  • Press and release events
  • Hold detection with duration
  • Visual press feedback (scale)
  • Configurable cooldown
  • Works with Unity UI system

Inspector fields

Every field Touch Button exposes in the Inspector, with the default it starts at.

FieldTypeDefaultGroup
Cooldown TimecooldownTimefloat0.1Button Settings
Hold ThresholdholdThresholdfloat0.4Button Settings
Pressed ScalepressedScalefloat0.9Visual Feedback
Scale SpeedscaleSpeedfloat10Visual Feedback
On PressedonPressedUnityEventNo listenersEvents
On ReleasedonReleasedUnityEventNo listenersEvents
On Hold StartonHoldStartUnityEventNo listenersEvents
On HoldingonHoldingUnityEvent<float>No listenersEvents

Public API

What other scripts can call on TouchButton.

MemberReturns
OnPointerDown(PointerEventData eventData)void
OnPointerUp(PointerEventData eventData)void
SetInteractable(bool interactable)void
ResetButton()void
IsPressed propertybool
IsHolding propertybool
HoldDuration propertyfloat

Requirements

  • Uses uGUI (UnityEngine.UI): needs a Canvas in the scene.
  • Uses the UI event system: the scene needs an EventSystem object.

Common mistakes

An EventSystem must exist in the scene or IPointerDown/Up events won't fire at all. The pressedScale multiplies the original scale, so if your button is already scaled to 0.5, a pressedScale of 0.9 results in 0.45 — not 0.9. The cooldownTime prevents rapid re-presses, but setting it too high makes the button feel unresponsive in fast-paced games.

Source code

TouchButton.cs, 128 lines, MIT licensed. Paste it into a new C# file of the same name.

TouchButton.cs
C#
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class TouchButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    [Header("Button Settings")]
    [SerializeField] private float cooldownTime = 0.1f;
    [SerializeField] private float holdThreshold = 0.4f;

    [Header("Visual Feedback")]
    [SerializeField] private float pressedScale = 0.9f;
    [SerializeField] private float scaleSpeed = 10f;

    [Header("Events")]
    public UnityEvent onPressed;
    public UnityEvent onReleased;
    public UnityEvent onHoldStart;
    public UnityEvent<float> onHolding;

    public bool IsPressed { get; private set; }
    public bool IsHolding { get; private set; }
    public float HoldDuration { get; private set; }

    private RectTransform rectTransform;
    private Vector3 originalScale;
    private Vector3 targetScale;
    private float cooldownTimer;
    private bool holdFired;

    private void Awake()
    {
        rectTransform = GetComponent<RectTransform>();
        originalScale = rectTransform.localScale;
        targetScale = originalScale;
    }

    private void Update()
    {
        // Scale animation
        rectTransform.localScale = Vector3.Lerp(
            rectTransform.localScale, targetScale, Time.deltaTime * scaleSpeed);

        // Cooldown
        if (cooldownTimer > 0f)
            cooldownTimer -= Time.deltaTime;

        // Hold tracking
        if (IsPressed)
        {
            HoldDuration += Time.deltaTime;

            if (!holdFired && HoldDuration >= holdThreshold)
            {
                holdFired = true;
                IsHolding = true;
                onHoldStart?.Invoke();
            }

            if (IsHolding)
            {
                onHolding?.Invoke(HoldDuration);
            }
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        if (cooldownTimer > 0f) return;

        IsPressed = true;
        HoldDuration = 0f;
        holdFired = false;
        IsHolding = false;
        targetScale = originalScale * pressedScale;

        onPressed?.Invoke();
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (!IsPressed) return;

        IsPressed = false;
        IsHolding = false;
        targetScale = originalScale;
        cooldownTimer = cooldownTime;

        onReleased?.Invoke();
    }

    public void SetInteractable(bool interactable)
    {
        enabled = interactable;

        Image img = GetComponent<Image>();
        if (img != null)
        {
            Color c = img.color;
            c.a = interactable ? 1f : 0.5f;
            img.color = c;
        }

        if (!interactable && IsPressed)
        {
            IsPressed = false;
            IsHolding = false;
            targetScale = originalScale;
        }
    }

    public void ResetButton()
    {
        IsPressed = false;
        IsHolding = false;
        HoldDuration = 0f;
        holdFired = false;
        cooldownTimer = 0f;
        targetScale = originalScale;
        rectTransform.localScale = originalScale;
    }

    private void OnDisable()
    {
        ResetButton();
    }
}

Next step

Pinch-to-Zoom CameraSmooth pinch-to-zoom camera control for mobile devices supporting both orthographic and perspective cameras. Features configurable zoom limits and optional smoothing.