如何在Unity中用GUI Texture播放全屏视频
发表于2016-07-22
注意:本文所介绍的方法只能用于Unity Pro,因为免费版本不支持视频。
代码在Unity v3.3下创建并测试,不能在低于2.6版本下工作。
本文介绍了如何在Unity编辑器中设置GUI Texture和播放全屏视频的必要代码,我们可以在游戏的启动界面中播放工作室LOGO,游戏介绍或其它任何视频。并介绍了如何根据屏幕尺寸对视频进行缩放,最后提供了实例代码。
在设置场景或编码前,你需要找到一个视频文件,Unity支持的视频格式基本上是QuickTime播放器支持的格式。
在准备好视频文件后,在Project标签下添加名字为Resources的文件夹,右键点击里面的空白处然后选择New->Folder,可以将视频文件拖放到这个文件夹中。

视频文件的导入会话费比较长的时间。
在场景中准备好播放视频的必须组件:
1)、GUI Texture,视频就是在其上播放。
2)、Audio Source, 用于播放声音
3)、script,用于播放GUI Texture上的视频和声音,并调整视频为全屏。
组件添加到Main Camera的游戏对象上,添加好后就是这个样子的:

添加PlayVideo 脚本用于启动视频播放,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | using UnityEngine; using System.Collections; [RequireComponent (typeof (GUITexture))] [RequireComponent (typeof (AudioSource))] public class PlayVideo : MonoBehaviour { //the GUI texture private GUITexture videoGUItex; //the Movie texture private MovieTexture mTex; //the AudioSource private AudioSource movieAS; //the movie name inside the resources folder public string movieName; void Awake() { //get the attached GUITexture videoGUItex = this .GetComponent(); //get the attached AudioSource movieAS = this .GetComponent(); //load the movie texture from the resources folder mTex = (MovieTexture)Resources.Load(movieName); //set the AudioSource clip to be the same as the movie texture audio clip movieAS.clip = mTex.audioClip; //全屏显示 videoGUItex.pixelInset = new Rect(Screen.width/2, -Screen.height/2,0,0); } //On Script Start void Start() { //set the videoGUItex.texture to be the same as mTex videoGUItex.texture = mTex; //播放视频 mTex.Play(); //播放视频中的音频 movieAS.Play(); } } |
代码要求AudioSource和GUITexture已经附加到游戏对象中,如果没有,则脚本会进行添加(行4 行5),然后定义GUI Texture,Movie Texture和Audio Source对象,用于处理全屏播放视频、视频文件的读取、音频的播放(行10 到行14),行16的string用于存放resources目录下的视频文件名称。
在Awake()函数中,所有播放视频的准备工作就绪:附加 GUI Texture和Audio Source组件的处理对象(行21和行23),视频文件在代码25行加载,Audio Source剪辑在第27行设置好,最后在29行,设置GUI Texture全屏渲染。
Start()函数仅仅加载movie texture设置了GUI Texture中的texture(行36)并开始播放视频和音频。(行38 行40)
不要忘记在Inspector中设置moveName变量,这样才能从Resources目录中加载视频。
读者可能已经注意到,这段代码没有考虑视频的拉伸纵横比,以便它可以充满整个屏幕。为了保持影片的长宽比,请阅读下面部分。
拉伸视频的宽度,以及如何找到新的合适高度(letterbox)
这是最典型的情况之一,特别是PC和手机游戏,大量的不同屏幕分辨率,视频有一个宽屏格式。为了使宽屏视频适应4 :3或5:4的显示器,我们知道视频的宽度必须等于屏幕的宽度。这使得该视频的高度需要基于屏幕的宽度来计算Y轴。为了做到这一点,可以参考下面的计算公式:
video height = screen width / video’s aspect ratio;
video Y position = screen height + video height;
问题是,在Unity 3.3 GUITexture pixelInset属性X值值必须为正,而Y ,宽度和高度必须为负,这样才能使视频可以正确缩放到屏幕上。
这意味着,把它们转换成代码时必须补偿屏幕宽度,使该视频可以被正确地缩放和定位。所以,为了能够基于视频宽度缩放,上面代码中的行29可以用如下代码代替:
1 2 3 4 | //replace line 29 with the following: float newHeight = -(Screen.height-(Screen.width/(mTex.width/( float )mTex.height))); float yOffset = (-Screen.height-newHeight)/2; videoGUItex.pixelInset = new Rect(Screen.width/2, yOffset,0,newHeight); |
拉伸视频的高度,如何找到新的合适宽度
这里有一个案例: 你想在16:9的屏幕上显示4:3的视频。这一次,需要将视频的高度变成屏幕的高度。我们需要弄清楚宽度以及基于屏幕高度计算视频X轴的位置。下面是计算公式:
video width = screen height * video’s aspect ratio;
video X position = Screen.width + newWidth/2;
同样,它必须补偿pixelInset Y,宽度和高度值,以使它们都为负值。请看下面的代码(替换29行) :
1 2 3 4 | //replace line 29 with the following float newWidth = -(Screen.width-(Screen.height*(mTex.width/( float )mTex.height))); float xOffset = (Screen.width - newWidth)/2; videoGUItex.pixelInset = new Rect(xOffset, -Screen.height/2,newWidth,0); |
如果这令你困惑,可尝试在inspector中修改GUITexture的pixelInset值,这样容易搞清楚。
下载源代码:http://www.okbase.net/file/item/27567