Loading and playing external movie files

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).

Getting ready

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.

How to do it...

To load and play an external movie file, perform the following steps:

  1. In the Project view, create a new folder and rename it to Resources.
  2. Place the videoTexture.theora.ogg file into the Resources folder.
  3. In the Project view, click on the Create button and add a GUI Texture to the scene. Then, rename it to videoTexture:
    How to do it...
  4. Through the Inspector view, remove the default GUI Texture from videoTexture by selecting None in the Texture field:
    How to do it...
  5. In the Project view, create a new C# script and rename it to LoadMovie.
  6. Open the script in your editor and replace everything with the following code:
    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();
        }
    }
  7. Save your script and attach it to the videoTexture game object by dragging it from the Project view to the videoTexture game object in the Hierarchy view.
  8. Once the script is attached to the camera, let's check its properties. As Video Source, select Resources_folder. Fill out File Name by typing in the name of the video file in the Resources folder (in our case, videoTexure.theora):
    How to do it...
  9. Play the scene. The texture will be loaded and displayed on the screen.

How it works...

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.

There's more...

Next, you will find some information on how to fine-tune and customize this recipe:

Loading videos from the Internet

If you want to load the file from the Web, select Internet as Video Source, and fill out the File Url field with the complete file URL (unless the Relative Path option is checked).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.103.210