Ok so I had a go at understanding the github document you sent me, the animation one, this is what I've gathered from it.
- You've initialised a CancellationTokenSource as a global variable, then in start you call AnimateExample.Forget(). Forget() is used to ignore task completion on the initial call.
My guess is that this was done so that because start is not an async function, with forget we can call AnimateExample without throwing an error or warning.
And we don't want start to have to wait on initialising the animation.
- In AnimateExample(), we first start by yielding a task and awaiting for Animate().
This works by first calling CancelAnimation() to make sure that we don't have a stack of cancellation token's, preserving memory.
After this call we assign value to the animateTokenSource object with CreateLinkedTokenSource, meaning animateTokenSource acts as a pointer to this, with destroyCancellationToken acting as a parameter.
We then extract the cancellation token itself from animateTokenSource, using animateTokenSource.Token.
We then initialise the starting position as the current position of the object, that this script is attached to. As well as a float value that starts at 0, where from the equation in the while loops with Time.deltaTime, it kinda acts as a percentage of how much of the animation is done.
- Then in the while loop, this continues until either the Cancellation of Animate() is requested via a token - possibly from another method or class? Or once lerp's value no longer fits the inequality, I'm guessing that the 0 is a typo and meant to be a 1, otherwise this while loop would never run?
This while loop runs frame by frame, passing the cancellation token between each frame.
-
After this while loop, we then have one last if statement that checks whether the async was cancelled via the token.
-
Finally, the animation is run again with different parameters after checking that the script wasn't destroyed?