UGUI—UIMultipleClick

作者:追风剑情 发布于:2019-12-2 14:32 分类:Unity3d

示例

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

/// <summary>
/// 多次点击后触发MultipleClick事件
/// </summary>
public class UIMultipleClick : MonoBehaviour, IPointerClickHandler
{
    public int multiple = 5; //需要连续点击的次数
    public float interval = 0.3f; //点击间隔时间

    private int n;
    private float lastTime;

    [Serializable]
    // 定义按钮OnClick事件类
    public class OnMultipleEvent : UnityEvent { }
    // 防止序列化变量重命名后丢失引用
    [FormerlySerializedAs("OnMultipleClick")]
    [SerializeField]
    private OnMultipleEvent m_OnMultipleClick = new OnMultipleEvent();

    public void OnPointerClick(PointerEventData eventData)
    {
        if (n == 0 || Time.time - lastTime <= interval)
            n++;
        else
            n = 1;

        lastTime = Time.time;

        if (n >= multiple)
        {
            n = 0;
            m_OnMultipleClick.Invoke();
        }
    }
}

111111.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号