Skip to content

Instantly share code, notes, and snippets.

@Vamoss
Created October 28, 2014 13:10
Show Gist options
  • Save Vamoss/5ab651cd4df2b192d949 to your computer and use it in GitHub Desktop.
Save Vamoss/5ab651cd4df2b192d949 to your computer and use it in GitHub Desktop.
Improve the Video playback on Unity
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Collections;
public class DynamicVideo : MonoBehaviour {
public string _url = "http://www.unity3d.com/webplayers/Movie/sample.ogg";
public RawImage texture;
private MovieTexture movieTexture;
private WWW www;
public bool autoPlay = true;
private bool autoPlayed = false;
public bool loop = true;
//events
UnityEvent VideoStart = new UnityEvent ();
UnityEvent VideoEnd = new UnityEvent ();
//fades
private float alpha = 1f;
public bool fadeIn = true;
public bool fadeOut = true;
public float fadeInDuration = 2.0f;
public float fadeOutDuration = 2.0f;
private bool isFadingIn;
private bool isFadingOut;
private float alphaStart;
private float fadeCounter = 0.0f;
//timer
public Text TimeCodeCurrent;
public Text TimeCodeDuration;
private float currentTime = 0;
//video complete verification
private bool isPlaying = false;
private int frameFreezedMax = 3;
private int frameFreezedCounter = 0;
private float lastCurrentTime = 0;
void Start () {
Url = _url;
}
void Update () {
//autoPlay
if (autoPlay && !autoPlayed && !movieTexture.isPlaying && movieTexture.isReadyToPlay) {
autoPlayed = true;
Play ();
}
//update alpha
if((isFadingIn || isFadingOut) && movieTexture.isReadyToPlay){
fadeCounter += Time.deltaTime;
if (isFadingIn) {
if (alpha < 1f) {
alpha = Mathf.Lerp(alphaStart, 1, fadeCounter/fadeInDuration);
} else {
//fadeIn complete
alpha = 1.0f;
isFadingIn = false;
}
}
if (isFadingOut) {
if(alpha>0f) {
alpha = Mathf.Lerp(alphaStart, 0, fadeCounter/fadeOutDuration);
}else{
//fadeOut complete
alpha = 0.0f;
isFadingOut = false;
}
}
}
texture.color = new Color (texture.color.r, texture.color.g, texture.color.b, alpha);
//update texture and timer
if (movieTexture.isPlaying) {
texture.texture = movieTexture;
currentTime += Time.deltaTime;
}
//verify video complete
if (isPlaying) {
//it checks if the movie has stop update, and after some frames it considers it is completed
frameFreezedCounter = currentTime == lastCurrentTime ? frameFreezedCounter+1 : 0;
if(frameFreezedCounter == frameFreezedMax) OnVideoEnd();
lastCurrentTime = currentTime;
}
//update timecode
if(TimeCodeCurrent) TimeCodeCurrent.text = string.Format("{0:mm:ss; mm:ss}", System.TimeSpan.FromMinutes(currentTime));
if(TimeCodeDuration) TimeCodeDuration.text = string.Format("{0:mm:ss; mm:ss}", System.TimeSpan.FromMinutes(movieTexture.duration));
}
private void OnVideoStart(){
VideoStart.Invoke ();
}
private void OnVideoEnd(){
VideoEnd.Invoke ();
if (loop) {
Stop (false);
Play (false);
} else {
Stop ();
}
}
public string Url
{
get { return _url; }
set {
_url = value;
www = new WWW(_url);
movieTexture = www.movie;
if(audio) audio.clip = movieTexture.audioClip;
autoPlayed = false;
if(fadeIn) alpha = 0.0f;
}
}
public float CurrentTime
{
get { return currentTime; }
}
public float Duration
{
get { return movieTexture.duration; }
}
public void Play(bool canFadeIn = true) {
isPlaying = true;
movieTexture.Play ();
if(audio) audio.Play ();
if(currentTime==0) OnVideoStart();
if(canFadeIn && fadeIn && alpha<1.0f) {
fadeCounter = 0;
alphaStart = alpha;
isFadingIn = true;
isFadingOut = false;
}
}
public void Stop(bool canFadeOut = true) {
isPlaying = false;
movieTexture.Stop ();
if(audio) audio.Stop ();
currentTime = 0;
lastCurrentTime = 0;
if (canFadeOut && fadeOut) {
fadeCounter = 0;
alphaStart = alpha;
isFadingOut = true;
isFadingIn = false;
}
}
public void Pause() {
isPlaying = false;
movieTexture.Pause ();
if(audio) audio.Pause ();
}
public void TooglePlayPause() {
if (!movieTexture.isReadyToPlay) return;
if (movieTexture.isPlaying) {
Pause();
} else {
Play();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment