Unity UGUI Button 按下,擡起,連續按下,鼠標退出事件響應函數

using UnityEngine;  
using UnityEngine.Events;  
using UnityEngine.EventSystems;  
using System.Collections;  
  
  
public class test : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler, IPointerClickHandler  
{  
    public float interval = 0.1f;  
  
  
    [SerializeField]  
    UnityEvent m_OnLongpress = new UnityEvent();  
    private bool isPointDown = false;  
    private float lastInvokeTime;  
  
  
    // Update is called once per frame  
    void Update()  
    {  
        if (isPointDown)  
        {  
            if (Time.time - lastInvokeTime > interval)  
            {  
                //觸發點擊;  
                m_OnLongpress.Invoke();  
                lastInvokeTime = Time.time;  
                Debug.Log("長按");  
            }  
        }  
    }  
  
  
    public void OnPointerDown(PointerEventData eventData)  
    {  
        m_OnLongpress.Invoke();  
  
  
        isPointDown = true;  
  
  
        lastInvokeTime = Time.time;  
        Debug.Log("鼠標按下");  
    }  
  
  
    public void OnPointerUp(PointerEventData eventData)  
    {  
        isPointDown = false;  
        Debug.Log("鼠標擡起");  
    }  
  
  
    public void OnPointerExit(PointerEventData eventData)  
    {  
        isPointDown = false;  
        Debug.Log("鼠標退出");  
    }  
    public void OnPointerClick(PointerEventData eventData)  
    {  
        isPointDown = false;  
        Debug.Log("鼠標點擊");  
    }  
}  

發佈了22 篇原創文章 · 獲贊 24 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章