UGUI—UIMultipleClick

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

示例

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.Serialization;
  8.  
  9. /// <summary>
  10. /// 多次点击后触发MultipleClick事件
  11. /// </summary>
  12. public class UIMultipleClick : MonoBehaviour, IPointerClickHandler
  13. {
  14. public int multiple = 5; //需要连续点击的次数
  15. public float interval = 0.3f; //点击间隔时间
  16.  
  17. private int n;
  18. private float lastTime;
  19.  
  20. [Serializable]
  21. // 定义按钮OnClick事件类
  22. public class OnMultipleEvent : UnityEvent { }
  23. // 防止序列化变量重命名后丢失引用
  24. [FormerlySerializedAs("OnMultipleClick")]
  25. [SerializeField]
  26. private OnMultipleEvent m_OnMultipleClick = new OnMultipleEvent();
  27.  
  28. public void OnPointerClick(PointerEventData eventData)
  29. {
  30. if (n == 0 || Time.time - lastTime <= interval)
  31. n++;
  32. else
  33. n = 1;
  34.  
  35. lastTime = Time.time;
  36.  
  37. if (n >= multiple)
  38. {
  39. n = 0;
  40. m_OnMultipleClick.Invoke();
  41. }
  42. }
  43. }

111111.png

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号