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.
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.
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
- Create a UI Button inside a Canvas with an EventSystem in the scene
- Attach the TouchButton script to the Button GameObject
- Configure cooldown time and hold threshold in the inspector
- Wire up onPressed, onReleased, or onHoldStart events to your game actions
- 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.
| Field | Type | Default | Group |
|---|---|---|---|
Cooldown TimecooldownTime | float | 0.1 | Button Settings |
Hold ThresholdholdThreshold | float | 0.4 | Button Settings |
Pressed ScalepressedScale | float | 0.9 | Visual Feedback |
Scale SpeedscaleSpeed | float | 10 | Visual Feedback |
On PressedonPressed | UnityEvent | No listeners | Events |
On ReleasedonReleased | UnityEvent | No listeners | Events |
On Hold StartonHoldStart | UnityEvent | No listeners | Events |
On HoldingonHolding | UnityEvent<float> | No listeners | Events |
Public API
What other scripts can call on TouchButton.
| Member | Returns |
|---|---|
OnPointerDown(PointerEventData eventData) | void |
OnPointerUp(PointerEventData eventData) | void |
SetInteractable(bool interactable) | void |
ResetButton() | void |
IsPressed property | bool |
IsHolding property | bool |
HoldDuration property | float |
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.
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();
}
}