Ensuring a movie is prepared before playing
In the preceding recipe, the movie has time to be prepared since the game waits until we press the jump/Spacebar key. If we are using scripting to set up a video player for a video clip, we need to do some initial work before the video is ready to play. Unity provides the prepareCompleted event for this, which allows us to register a method to be invoked once VideoPlayer is ready to play.
Getting ready
This recipe follows on from the previous one, so make a copy of that and work on the copy.
How to do it...
To ensure a movie is prepared before it’s played by subscribing to a prepareCompleted event, do the following:
- Add a UI
RawImageto the scene by going to GameObject | UI | Raw Image. - Create a new, empty GameObject named
video-object. - Create a C# script class named
PrepareCompletedand attach an instance of the script as a component to thevideo-objectGameObject:using UnityEngine...