Many Unity programmers are very used to working with coroutines, so instead of using the prepareCompleted event, we can rewrite the preceding script by using a coroutine.
Do the following:
- Remove the PlayVideoWhenPrepared() method.
- Add a new using statement at the top of the script (so that we can refer to the IEnumerator interface):
using System.Collections;
- Replace the existing Start() method with the following:
private IEnumerator Start() {
SetupVideoAudioPlayers();
videoPlayer.Prepare();
while (!videoPlayer.isPrepared)
yield return null;
videoPlayer.Play();
}
As you can see, our Start() method has become a coroutine (returning an IEnumerator), which means that it can yield control back to Unity during its execution. In the next frame, it will resume execution at that...