Unity UGUI点击、拖动等事件

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
// 别忘记引入
using System.Collections;


public class UIOnClickTest : 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("鼠标点击");
    }
}

发布了41 篇原创文章 · 获赞 36 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/yzx5452830/article/details/77855013