lvjincheng 3 سال پیش
والد
کامیت
2ba61df6f6
3فایلهای تغییر یافته به همراه40 افزوده شده و 2 حذف شده
  1. 0 2
      Assets/JC/JCLib.cs
  2. 29 0
      Assets/JC/Untiy/Debouncer.cs
  3. 11 0
      Assets/JC/Untiy/Debouncer.cs.meta

+ 0 - 2
Assets/JC/JCLib.cs

@@ -65,8 +65,6 @@ namespace JC.CS
             return false;
         }
     }
-    //防抖器
-    // public class Debouncer {}
 }
 namespace JC.Unity
 {

+ 29 - 0
Assets/JC/Untiy/Debouncer.cs

@@ -0,0 +1,29 @@
+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);
+        }
+    }
+}

+ 11 - 0
Assets/JC/Untiy/Debouncer.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: cfb13a64796f4144c9fc99ece43f8b8d
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: