Skip to content

Instantly share code, notes, and snippets.

@demonixis
Created February 7, 2017 14:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save demonixis/b22660f0379236a4b8131d8a15bb749a to your computer and use it in GitHub Desktop.
Save demonixis/b22660f0379236a4b8131d8a15bb749a to your computer and use it in GitHub Desktop.
Playing a video with the new VideoPlayer component with Unity 5.6 beta.
public class VideoManager : MonoBehaviour
{
[SerializeField]
private RenderTexture _renderTexture = null;
[SerializeField]
private VideoClip _videoClip = null;
private IEnumerator Start()
{
Application.runInBackground = true;
var camera = Camera.main.gameObject;
var videoPlayer = camera.AddComponent<VideoPlayer>();
var audioSource = camera.AddComponent<AudioSource>();
videoPlayer.playOnAwake = false;
audioSource.playOnAwake = false;
videoPlayer.source = VideoSource.VideoClip;
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
videoPlayer.renderMode = VideoRenderMode.RenderTexture;
videoPlayer.EnableAudioTrack(0, true);
videoPlayer.SetTargetAudioSource(0, audioSource);
videoPlayer.clip = _videoClip;
videoPlayer.Prepare();
while (!videoPlayer.isPrepared)
yield return null;
videoPlayer.targetTexture = _renderTexture;
videoPlayer.Play();
audioSource.Play();
while (videoPlayer.isPlaying)
yield return null;
ScreenFader.FadeIn(2.5f, () => SceneManager.LoadScene("Menu"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment