Skip to content

Instantly share code, notes, and snippets.

@lycoris102
Created July 25, 2016 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lycoris102/96dff82f2abf3944b4dbbf0cd4a629de to your computer and use it in GitHub Desktop.
Save lycoris102/96dff82f2abf3944b4dbbf0cd4a629de to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections;
public class ButtonExtention : Button
{
public UnityEvent onLongPress = new UnityEvent();
public float longPressIntervalSeconds = 1.0f;
private float pressingSeconds = 0.0f;
private bool isEnabledLongPress = true;
private bool isPressing = false;
void Update()
{
if (isPressing && isEnabledLongPress)
{
pressingSeconds += Time.deltaTime;
if (pressingSeconds >= longPressIntervalSeconds)
{
onLongPress.Invoke();
isEnabledLongPress = false;
}
}
}
public override void OnPointerDown(UnityEngine.EventSystems.PointerEventData eventData)
{
base.OnPointerDown(eventData);
isPressing = true;
}
public override void OnPointerUp (UnityEngine.EventSystems.PointerEventData eventData)
{
base.OnPointerUp(eventData);
pressingSeconds = 0.0f;
isEnabledLongPress = true;
isPressing = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment