Skip to content

Instantly share code, notes, and snippets.

@joduplessis
Last active April 2, 2017 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joduplessis/82e742e46246dfd821584d8406ad04b9 to your computer and use it in GitHub Desktop.
Save joduplessis/82e742e46246dfd821584d8406ad04b9 to your computer and use it in GitHub Desktop.
/*
* Very simple demo showing how to play videos
* using RawTexture and Unity 5.x UI
*
* Just a note - this is PC/OSX specific
*/
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
using DG.Tweening;
public class Video : MonoBehaviour {
MovieTexture movieTexture;
RawImage videoTexture;
AudioSource audio;
void Update () {
}
void Start() {
// Load video
StartCoroutine(loadVideo());
}
IEnumerator loadVideo () {
string url = "path/to/file";
// Make the request over www
WWW wwwRequest = new WWW (url);
yield return wwwRequest;
// Assign the raw image the movie texture
// yes this throws an error - it is a bug in Unity
// https://issuetracker.unity3d.com/issues/movietexture-fmod-error-when-trying-to-play-video-using-www-class
movieTexture = (MovieTexture) wwwRequest.movie;
videoTexture.texture = movieTexture;
// Play the audio attached to this object
GetComponent<AudioSource> ().clip = movieTexture.audioClip;
// Start the video
// You could Stop, Pause, Play here
movieTexture.Play ();
// Lower the volume using the DOTTween libary
audio.DOFade (0.1f, 3.0f);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment