//-------------------------------------------------------- // [Author]: 第二世界 // [ Date ]: Monday, July 31, 2017 //-------------------------------------------------------- using UnityEngine; using System.Collections; using UnityEngine.EventSystems; using UnityEngine.Events; using UnityEngine.UI; namespace vnxbqy.UI { public class AttackButton : Button { const float PRESS_TRIGGERTIME = 0.2f; UIEvent m_AttackEvent = new UIEvent(); public UIEvent attackEvent { get { return m_AttackEvent; } } bool m_IsDown = false; public bool isDown { get { if (Application.isMobilePlatform) { return Input.touchCount > 0 && m_IsDown; } else { return Input.GetMouseButton(0) && m_IsDown; } } } bool clickResponse = false; float pressTime = 0f; bool m_IsPress = false; public void AddListener(UnityAction _action) { if (attackEvent != null) { attackEvent.AddListener(_action); } } public void RemoveListener() { if (attackEvent != null) { attackEvent.RemoveAllListeners(); } } public override void OnPointerDown(PointerEventData eventData) { base.OnPointerDown(eventData); m_IsDown = true; m_IsPress = false; clickResponse = true; pressTime = Time.time + PRESS_TRIGGERTIME; } public override void OnPointerUp(PointerEventData eventData) { base.OnPointerUp(eventData); m_IsDown = false; m_IsPress = false; clickResponse = false; } protected override void OnDisable() { m_IsDown = false; m_IsPress = false; clickResponse = false; } private void LateUpdate() { if (!isDown) { return; } if (Time.time > pressTime) { m_IsPress = true; } if (!m_IsPress && clickResponse) { if (attackEvent != null) { attackEvent.Invoke(); } EffectMgr.Instance.PlayUIEffect(1021, 1500, this.transform, false); clickResponse = false; } if (m_IsPress) { if (attackEvent != null) { attackEvent.Invoke(); } } } } }