Playing videos inside a scene

TV sets, projectors, monitors.... If you want complex animated materials in your level, you can play video files as texture maps. In this recipe, we will learn how to apply a video texture to a cube. We will also implement a simple control scheme that plays or pauses the video whenever that cube is clicked on.

Getting ready

Unity imports video files through Apple Quicktime. If you don't have it installed in your machine, please download it at http://www.apple.com/quicktime/download/.

Also, if you need a video file to follow this recipe, please use the videoTexture.mov included in the folder 1632_04_08.

How to do it...

Follow these steps:

  1. Add a cube to the scene through the GameObject | 3D Object | Cube menu.
  2. Import the provided videoTexture.mov file.
  3. From the Project view, use the Create drop-down menu to create a new Material. Rename it Video_MAT and, from the Inspector view, change its Shader to Unlit/Texture:
    How to do it...
  4. Apply videoTexture to the texture slot of Video_MAT by dragging it from the Project view into the appropriate slot.
  5. Apply the Video_MAT to the Cube you have previously created.
  6. Expand videoTexture on the Project view to reveal its correspondent Audio Clip. Then, apply that audio clip to the Cube (you can do it by dragging it from the Project view to the Cube in the Hierarchy view, or a Scene view).
    How to do it...
  7. Select the Cube. Make sure there is a Collider component visible from the Inspector view. In case there isn't one, add it via the Component | Physics | Box Collider menu. Colliders are needed for mouse collision detection.
  8. Now we need to create a script for controlling the movie texture and associated audio clip. From Project view, use the Create drop-down menu to add a C# Script. Name it PlayVideo.
  9. Open the script and replace it with the following code:
    using UnityEngine;
    using System.Collections;
    
    [RequireComponent(typeof(AudioSource))]
    
    public class PlayVideo : MonoBehaviour {
    
      public bool loop = true;
      public bool playFromStart = true;
      public MovieTexture video;
        public AudioClip audioClip;
      private AudioSource audio;
    
      void Start () {
        audio = GetComponent<AudioSource> ();
    
        if (!video)
          video = GetComponent<Renderer>().material.mainTexture as MovieTexture;
    
        if (!audioClip)
            audioClip = audio.clip;
    
        video.Stop ();
        audio.Stop ();
        video.loop = loop;
        audio.loop = loop;
    
        if(playFromStart)
          ControlMovie();
      }
      
      void OnMouseUp(){
        ControlMovie();
      }
    
      public void ControlMovie(){
      
        if(video.isPlaying){
          video.Pause();
          audio.Pause();
        } else {
          video.Play();
          audio.Play();
        }
      }
    }
  10. Save your script and attach it to the Cube.
  11. Test your scene. You should be able to see the movie being played in the cube face, and also pause/play it by clicking on it.

How it works...

By default, our script makes the movie texture play in loop mode. There is, however, a Boolean variable than can be changed through the Inspector panel, where it is represented by a check box. Likewise, there is a check box that can be used to prevent the movie from playing when the level starts.

There's more...

There are some other movie texture commands and parameters that can be played with. Don't forget to check out Unity's scripting guide at http://docs.unity3d.com/Documentation/ScriptReference/MovieTexture.html.

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

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