using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using UnityEngine.Events;
|
using UnityEngine.EventSystems;
|
using UnityEngine.Serialization;
|
using UnityEngine.UI;
|
|
public class LongPressButton : ButtonEx
|
{
|
public class ButtonPressEvent : UnityEvent { }
|
|
[FormerlySerializedAs("onPress")]
|
[SerializeField]
|
private ButtonPressEvent m_OnPress = new ButtonPressEvent();
|
public ButtonPressEvent onPress
|
{
|
get { return m_OnPress; }
|
set { m_OnPress = value; }
|
}
|
|
[SerializeField]
|
float m_LongPressCheckTime = 0.5f;
|
public float longPressCheckTime
|
{
|
get { return m_LongPressCheckTime; }
|
set { m_LongPressCheckTime = value; }
|
}
|
[SerializeField]
|
float m_LongPressIntervalTime = 0.1f;
|
public float longPressIntervalTime
|
{
|
get { return m_LongPressIntervalTime; }
|
set { m_LongPressIntervalTime = value; }
|
}
|
|
float m_PressTime = 0.0f;
|
float m_PressIntervalTime = 0.0f;
|
bool m_LongPress = false;
|
bool m_IsButtonDown = false;
|
|
public override void OnPointerClick(PointerEventData eventData)
|
{
|
if (m_LongPress)
|
{
|
m_LongPress = false;
|
return;
|
}
|
base.OnPointerClick(eventData);
|
}
|
|
public override void OnPointerDown(PointerEventData eventData)
|
{
|
base.OnPointerDown(eventData);
|
m_PressTime = 0.0f;
|
m_PressIntervalTime = 0.0f;
|
m_IsButtonDown = true;
|
}
|
|
public override void OnPointerUp(PointerEventData eventData)
|
{
|
base.OnPointerUp(eventData);
|
m_IsButtonDown = false;
|
}
|
|
public override void OnPointerExit(PointerEventData eventData)
|
{
|
base.OnPointerExit(eventData);
|
m_IsButtonDown = false;
|
m_LongPress = false;
|
}
|
|
private void Update()
|
{
|
if (m_IsButtonDown)
|
{
|
m_PressTime += Time.deltaTime;
|
if (m_PressTime < m_LongPressCheckTime)
|
{
|
return;
|
}
|
if (m_PressIntervalTime >= m_LongPressIntervalTime)
|
{
|
onPress.Invoke();
|
m_PressIntervalTime = 0.0f;
|
}
|
m_PressIntervalTime += Time.deltaTime;
|
m_LongPress = true;
|
}
|
}
|
|
protected override void OnEnable()
|
{
|
base.OnEnable();
|
m_LongPress = false;
|
m_IsButtonDown = false;
|
}
|
}
|