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.
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
.
Follow these steps:
videoTexture.mov
file.Video_MAT
and, from the Inspector view, change its Shader to Unlit/Texture:videoTexture
to the texture slot of Video_MAT
by dragging it from the Project view into the appropriate slot.Video_MAT
to the Cube you have previously created.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).PlayVideo
.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(); } } }
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 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.
3.147.53.119