//-------------------------------------------------------- // [Author]: 第二世界 // [ Date ]: Monday, July 31, 2017 //-------------------------------------------------------- using UnityEngine; using System.Collections; using UnityEngine.EventSystems; using UnityEngine.UI; namespace vnxbqy.UI { /// /// 延迟触发点击事件的按钮,用于长按事件 /// public class DelayButton:MonoBehaviour,IPointerDownHandler,IPointerUpHandler { [SerializeField] float m_Delay = 0.5f; public float delay { get { return m_Delay; } } [SerializeField] UIEvent m_OnClick; public UIEvent 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(); } } } } } }