您当前的位置: 首页 >  unity

程序员正茂

暂无认证

  • 4浏览

    0关注

    283博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity长按按钮的实现

程序员正茂 发布时间:2019-06-19 15:17:07 ,浏览量:4

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class LongPressButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
    //按下多长时间算长按
    private float timeLongPress = 0.5f;

    //是否按下
    private bool isPointerDown = false;

    //按下时刻
    private float timePointerDown = 0;

    //回调方法
    public UnityEvent MethodCallBack;

    void Update()
    {
        float span = Time.time - timePointerDown;
        if (isPointerDown && span > timeLongPress)
        {
            MethodCallBack.Invoke();
        }
    }
    public void OnPointerDown(PointerEventData eventData)
    {
        isPointerDown = true;
        timePointerDown = Time.time;

        MethodCallBack.Invoke();
    }
    public void OnPointerUp(PointerEventData eventData)
    {
        isPointerDown = false;
    }
}

 

关注
打赏
1660743125
查看更多评论
立即登录/注册

微信扫码登录

0.0567s