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 when the cube is clicked.
Unity imports video files through
Apple Quicktime. If you don't have it installed on 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
file included in the 0423_11_03
folder.
To play a video inside a scene, perform the following steps:
videoMaterial
.PlayVideoTexture
and open it in your editor.using UnityEngine; using System.Collections; [RequireComponent (typeof (AudioSource))] public class PlayVideoTexture : MonoBehaviour { public bool loopVideo = true; public bool startPlaying = true; public MovieTexture videoTexture; void Start(){ videoTexture.loop = loopVideo; renderer.material.mainTexture = videoTexture; audio.clip = videoTexture.audioClip; if(startPlaying) ControlMovie(); } void OnMouseUp(){ ControlMovie(); } void ControlMovie(){ if(videoTexture.isPlaying) videoTexture.Pause(); else { videoTexture.Play(); audio.Play(); } } }
By default, our script makes the movie texture play in the Loop mode. There is, however, a Boolean variable that can be changed through the Inspector view, where it is represented by a checkbox. Likewise, there is a checkbox 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.143.17.27