Tutorial by Examples: coroutines

Generators can be used to implement coroutines: # create and advance generator to the first yield def coroutine(func): def start(*args,**kwargs): cr = func(*args,**kwargs) next(cr) return cr return start # example coroutine @coroutine def adder(sum = 0): ...
First it's essential to understand that, game engines (such as Unity) work on a "frame based" paradigm. Code is executed during every frame. That includes Unity's own code, and your code. When thinking about frames, it's important to understand that there is absolutely no guarantee of w...
There are three MonoBehaviour methods that can be made coroutines. Start() OnBecameVisible() OnLevelWasLoaded() This can be used to create, for example, scripts that execute only when the object is visible to a camera. using UnityEngine; using System.Collections; public class RotateObje...
Coroutines can yield inside themselves, and wait for other coroutines. So, you can chain sequences - "one after the other". This is very easy, and is a basic, core, technique in Unity. It's absolutely natural in games that certain things have to happen "in order". Almost every...
const promiseReturningFunction = Promise.coroutine(function* (file) { const data = yield fs.readFileAsync(file) // this returns a Promise and resolves to the file contents return data.toString().toUpperCase() }) promiseReturningFunction('file.txt').then(console.log)

Page 1 of 1