Skip to content

Instantly share code, notes, and snippets.

@mnjstwins
Forked from demonixis/PlayVideoUnity56beta.cs
Created August 28, 2017 17:59
Show Gist options
  • Select an option

  • Save mnjstwins/99d06689a1b967bb870268f87abef5f8 to your computer and use it in GitHub Desktop.

Select an option

Save mnjstwins/99d06689a1b967bb870268f87abef5f8 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