//-------------------------------------------------------- // [Author]: 玩个游戏 // [ Date ]: Monday, July 31, 2017 //-------------------------------------------------------- using UnityEngine; using System.Collections; using UnityEngine.EventSystems; using UnityEngine.UI; using System; /// /// 延迟触发点击事件的按钮,用于长按事件 /// public class DelayButton:MonoBehaviour,IPointerDownHandler,IPointerUpHandler { [SerializeField] float m_Delay = 0.5f; public float delay { get { return m_Delay; } } [SerializeField] Action m_OnClick; public Action onClick { get { return m_OnClick; } } float timer = 0f; bool down = false; public void OnPointerDown(PointerEventData eventData) { timer = 0f; down = true; } public void OnPointerUp(PointerEventData eventData) { down = false; } private void OnEnable() { timer = 0f; down = false; } private void LateUpdate() { if(down && timer < delay) { timer += Time.deltaTime; if(timer > delay) { if(onClick != null) { onClick?.Invoke(); } } } } }