diff --git a/Runtime/Scripts/Helpers/WaitUntilForSeconds.cs b/Runtime/Scripts/Helpers/WaitUntilForSeconds.cs new file mode 100644 index 00000000..05d2992f --- /dev/null +++ b/Runtime/Scripts/Helpers/WaitUntilForSeconds.cs @@ -0,0 +1,70 @@ +using System; +using UnityEngine; + +namespace TezosSDK.Helpers +{ + public class WaitUntilForSeconds: CustomYieldInstruction + { + float pauseTime; + float timer; + bool waitingForFirst; + Func myChecker; + Action onInterrupt; + bool alwaysTrue; + + public WaitUntilForSeconds(Func myChecker, float pauseTime, + Action onInterrupt = null) + { + this.myChecker = myChecker; + this.pauseTime = pauseTime; + this.onInterrupt = onInterrupt; + + waitingForFirst = true; + } + + public override bool keepWaiting + { + get + { + bool checkThisTurn = myChecker(); + if (waitingForFirst) + { + if (checkThisTurn) + { + waitingForFirst = false; + timer = pauseTime; + alwaysTrue = true; + } + } + else + { + timer -= Time.deltaTime; + + if (onInterrupt != null && !checkThisTurn && alwaysTrue) + { + onInterrupt(timer); + } + alwaysTrue &= checkThisTurn; + + // Alternate version: Interrupt the timer on false, + // and restart the wait + // if (!alwaysTrue || timer <= 0) + + if (timer <= 0) + { + if (alwaysTrue) + { + return false; + } + else + { + waitingForFirst = true; + } + } + } + + return true; + } + } + } +} \ No newline at end of file diff --git a/Runtime/Scripts/Helpers/WaitUntilForSeconds.cs.meta b/Runtime/Scripts/Helpers/WaitUntilForSeconds.cs.meta new file mode 100644 index 00000000..b68a7b11 --- /dev/null +++ b/Runtime/Scripts/Helpers/WaitUntilForSeconds.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5c59c7110b7b4dc484a01aa7799d0a86 +timeCreated: 1698013317 \ No newline at end of file