Friday, January 21, 2011

Unity3D Coroutines.

One of the great things about Unity3D, is the ability to turn the component Start method into a coroutine, simply by changing the return type of Start() to an IEnumerator.

In the example below, the Start method is changed into a coroutine which turns on it's particle emitter, waits for T seconds, then turns it off. Without coroutines, this would be much messier.

using UnityEngine;
using System.Collections;

public class ParticleBurst : MonoBehaviour {
public float T = 1;

IEnumerator Start() {
particleEmitter.emit = true;
yield return new WaitForSeconds(T);
particleEmitter.emit = false;
}

}

No comments:

Popular Posts