| 1234567891011121314151617181920212223242526272829 |
- using System;
- using System.Collections;
- using UnityEngine;
- namespace JC.Unity {
- //防抖器
- public class Debouncer {
- private Delegate dg;
- private object[] args;
- private float delayTime;
- private Coroutine coroutine;
- public Debouncer(Delegate dg, float delayTime) {
- this.dg = dg;
- this.delayTime = delayTime;
- }
- public void Invoke(params object[] args) {
- if (coroutine != null) {
- CoroutineStarter.Stop(coroutine);
- coroutine = null;
- }
- this.args = args;
- CoroutineStarter.Start(Execute());
- }
- private IEnumerator Execute() {
- yield return new WaitForSecondsRealtime(delayTime);
- dg?.Method?.Invoke(dg.Target, args);
- }
- }
- }
|