博客列表 >GUI界面按钮组控制器 鼠标长按GUI按钮事件监听

GUI界面按钮组控制器 鼠标长按GUI按钮事件监听

龍__遇见彩虹的博客
龍__遇见彩虹的博客原创
2017年12月26日 22:00:461326浏览
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections;
using System;
/*
 * 长按按钮事件监听
 */
public class DirectBtnController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
    public float interval = 0.1f;

    [SerializeField]
    UnityEvent m_OnLongpress = new UnityEvent();//长按
    private bool isPointDown = false;
    private float lastInvokeTime;
    void Start()
    {
    }
    void Update()
    {
        if (isPointDown)
        {
            if (Time.time - lastInvokeTime > interval)
            {
                //触发点击  do soemthing
                m_OnLongpress.Invoke();
                lastInvokeTime = Time.time;
                Debug.Log("长按中...");               
            }
        }
    }
    public void OnPointerDown(PointerEventData eventData)
    {
        m_OnLongpress.Invoke();
        isPointDown = true;
        lastInvokeTime = Time.time;
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        isPointDown = false;
    }
    public void OnPointerUp(PointerEventData eventData)
    {
        isPointDown = false;
    }
}


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议