-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add WaintUntilForSeconds yield instruction
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using UnityEngine; | ||
|
||
namespace TezosSDK.Helpers | ||
{ | ||
public class WaitUntilForSeconds: CustomYieldInstruction | ||
{ | ||
float pauseTime; | ||
float timer; | ||
bool waitingForFirst; | ||
Func<bool> myChecker; | ||
Action<float> onInterrupt; | ||
bool alwaysTrue; | ||
|
||
public WaitUntilForSeconds(Func<bool> myChecker, float pauseTime, | ||
Action<float> 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; | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.