Skip to main content
added 417 characters in body
Source Link
Ed Marty
  • 5.3k
  • 1
  • 14
  • 15

https://docs.unity3d.com/ScriptReference/CustomYieldInstruction.html

class WaitForMoves : CustomYieldInstruction { private int movesLeft; public WaitForMoves(int moves) { movesLeft = moves; Game.moveEvent.AddListener(OnMoveComplete); } void OnMoveComplete() { movesLeft—-; if (movesLeft <= 0) Game.moveEvent.RemoveListener(OnMoveComplete); } public override bool keepWaiting { get { return movesLeft > 0; } } } 

And to use it, just as you wanted:

yield return new WaitForMoves(2); 

If you have a global move counter in Game, then you wouldn’t need to have a listener at all:

class WaitForMoves : CustomYieldInstruction { private int targetMoves; public WaitForMoves(int moves) { targetMoves = moves + Game.moveCounter; } public override bool keepWaiting { get { return Game.moveCounter < targetMoves; } } } 

https://docs.unity3d.com/ScriptReference/CustomYieldInstruction.html

class WaitForMoves : CustomYieldInstruction { private int movesLeft; public WaitForMoves(int moves) { movesLeft = moves; Game.moveEvent.AddListener(OnMoveComplete); } void OnMoveComplete() { movesLeft—-; if (movesLeft <= 0) Game.moveEvent.RemoveListener(OnMoveComplete); } public override bool keepWaiting { get { return movesLeft > 0; } } } 

And to use it, just as you wanted:

yield return new WaitForMoves(2); 

https://docs.unity3d.com/ScriptReference/CustomYieldInstruction.html

class WaitForMoves : CustomYieldInstruction { private int movesLeft; public WaitForMoves(int moves) { movesLeft = moves; Game.moveEvent.AddListener(OnMoveComplete); } void OnMoveComplete() { movesLeft—-; if (movesLeft <= 0) Game.moveEvent.RemoveListener(OnMoveComplete); } public override bool keepWaiting { get { return movesLeft > 0; } } } 

And to use it, just as you wanted:

yield return new WaitForMoves(2); 

If you have a global move counter in Game, then you wouldn’t need to have a listener at all:

class WaitForMoves : CustomYieldInstruction { private int targetMoves; public WaitForMoves(int moves) { targetMoves = moves + Game.moveCounter; } public override bool keepWaiting { get { return Game.moveCounter < targetMoves; } } } 
Source Link
Ed Marty
  • 5.3k
  • 1
  • 14
  • 15

https://docs.unity3d.com/ScriptReference/CustomYieldInstruction.html

class WaitForMoves : CustomYieldInstruction { private int movesLeft; public WaitForMoves(int moves) { movesLeft = moves; Game.moveEvent.AddListener(OnMoveComplete); } void OnMoveComplete() { movesLeft—-; if (movesLeft <= 0) Game.moveEvent.RemoveListener(OnMoveComplete); } public override bool keepWaiting { get { return movesLeft > 0; } } } 

And to use it, just as you wanted:

yield return new WaitForMoves(2);