In this recipe, we will load an external image video and play it on the screen. Also, we will have the option of loading it from two different sources: the Web or Unity Default Resources (a library created once the game is built).
For this recipe, you'll need a video clip in OGG/Theora format. We have provided one, named videoTexture.theora.ogg
in the 0423_11_05
folder. If you wish to use your own video files,you might need a converter. In this case, a good option is
Miro Video Converter, available at www.mirovideoconverter.com.
Also, if you want to load the video clip from the Web, you will need access to a provider where you can host the files. In this case, please note that the image file should be stored in the same domain as the Unity file.
To load and play an external movie file, perform the following steps:
Resources
.videoTexture.theora.ogg
file into the Resources
folder.videoTexture
:LoadMovie
.using UnityEngine; using System.Collections; using System.IO; [RequireComponent (typeof (AudioSource))] public class LoadMovie : MonoBehaviour { public enum source {Resources_folder, Internet}; public source VideoSource = source.Resources_folder; public string fileName = ""; public string fileUrl = ""; public bool relativePath = false; private MovieTexture movieTexture; private string url; void Start (){ if(VideoSource != source.Resources_folder){ GetData("external"); } else { GetData("default"); } } void GetData ( string mode ){ if(mode == "web"){ url = fileUrl; if(relativePath){ url = Application.dataPath + "/" + fileUrl; } StartCoroutine(LoadWWW()); } else if(mode == "default"){ movieTexture = (MovieTexture) Resources.Load(fileName);; StartCoroutine(PlayMovie()); } } IEnumerator LoadWWW (){ Debug.Log(url); WWW www = new WWW (url); yield return www; movieTexture = www.movie; StartCoroutine(PlayMovie()); } IEnumerator PlayMovie(){ while (!movieTexture.isReadyToPlay) yield return 0; Debug.Log(movieTexture); guiTexture.texture = movieTexture; audio.clip = movieTexture.audioClip; Debug.Log("Movie Dimensions: " + movieTexture.width +", " + movieTexture.height); float movieAspectRatio = movieTexture.width / movieTexture.height; float screenAspectRatio = Screen.width / Screen.height; int movieWidth; int movieHeight; if (movieAspectRatio >= screenAspectRatio){ movieWidth = Screen.width; movieHeight = Mathf.RoundToInt(movieWidth / movieAspectRatio); } else { movieHeight = Screen.height; movieWidth = Mathf.RoundToInt(movieHeight * movieAspectRatio); } guiTexture.pixelInset = new Rect(-(movieWidth * 0.5f), -(movieHeight * 0.5f), movieWidth, movieHeight); movieTexture.Play(); audio.Play(); } void OnGUI (){ if(GUI.Button( new Rect(0, Screen.height - 20, 60, 20),"Play")) movieTexture.Play(); if(GUI.Button( new Rect(60, Screen.height - 20, 60, 20),"Pause")) movieTexture.Pause(); if(GUI.Button( new Rect(120, Screen.height - 20, 60, 20),"Stop")) movieTexture.Stop(); } }
Resources
folder (in our case, videoTexure.theora
):As soon as the scene starts, the script calls the GetData()
function, which loads the movie file using the appropriate method, according to the preferences expressed in the component's options in the Inspector view.
Next, you will find some information on how to fine-tune and customize this recipe:
18.116.118.229