123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- //单击按钮
- using System.Diagnostics;
- using UnityEngine.EventSystems;
- public class ClickButton : ClickEvent.ClickListener
- {
- protected override void OnPointerDown(PointerEventData eventData)
- {
- isRuning = true;
- }
- protected override void OnPointerUp(PointerEventData eventData)
- {
- if (isRuning)
- {
- Reset();
- callBack?.Invoke(true);
- }
- }
- protected override void Reset()
- {
- isRuning = false;
- }
- }
- //双击按钮
- public class DoubleClickButton : ClickEvent.ClickListener
- {
- public int maxSpaceTime = 250;
- private int clickCount = 0;
- private Stopwatch stopWatch;
- private float lastPointTime = 0;
- public DoubleClickButton()
- {
- stopWatch = new Stopwatch();
- Reset();
- }
- protected override void OnPointerDown(PointerEventData eventData)
- {
- isRuning = true;
- }
- protected override void OnPointerClick(PointerEventData eventData)
- {
- stopWatch.Start();
- clickCount++;
- if (clickCount == 2)
- {
- Reset();
- callBack?.Invoke(true);
- }
- }
- protected override void Update()
- {
- if (!stopWatch.IsRunning) return;
- if (stopWatch.ElapsedMilliseconds > maxSpaceTime)
- {
- Reset();
- callBack?.Invoke(false);
- }
- }
- protected override void Reset()
- {
- isRuning = false;
- if (stopWatch.IsRunning) stopWatch.Stop();
- stopWatch.Reset();
- clickCount = 0;
- }
- }
- //长按按钮
- public class PressButton : ClickEvent.ClickListener
- {
- public int pressMinTime = 2000;
- private Stopwatch stopWatch;
- public PressButton()
- {
- stopWatch = new Stopwatch();
- }
- protected override void OnPointerDown(PointerEventData eventData)
- {
- isRuning = true;
- stopWatch.Start();
- }
- protected override void OnPointerUp(PointerEventData eventData)
- {
- Reset();
- callBack?.Invoke(false);
- }
- protected override void Update()
- {
- if (!stopWatch.IsRunning) return;
- if (stopWatch.ElapsedMilliseconds > pressMinTime)
- {
- Reset();
- callBack?.Invoke(true);
- }
- }
- protected override void Reset()
- {
- isRuning = false;
- if (stopWatch.IsRunning) stopWatch.Stop();
- stopWatch.Reset();
- }
- }
|