Unity3D中播放游戏视频的方式有两种,第一种是在游戏对象中播放,就好比在游戏世界中创建一个Plane面对象,摄像机直直的照射在这个面上。第二种是在GUI层面上播放视频。播放视频其实和贴图非常相像,因为播放视频用到的MovieTexture属于贴图Texture的子类,那么本章我们就好好学习一下Unity中播放视频的这两种方式。哇咔咔~
Unity支持的播放视频格式有.mov、.mpg、.mpeg、.mp4、.avi和.asf。只需将对应的视频文件拖拽入Project视图即可,它会自动生成对应的MovieTexture对象。如下图所示,MOMO将default_video.mp4拖拽入Project视图中,如果视频中含有音频的话会对应生成audio文件,因为我的视频没有音频所以没有生成 audio文件。接着在Hierarchy视图中创建一个Plane对象视频将在它之上播放,Directional light世界定向光用于照亮整个游戏场景,最后Main Camera对象将直直的照射在Plane对象。
使用对象拖拽的形式为Mov Texture对象赋值,那么在脚本中就能直接使用它了,我们看看Test.cs脚本。
Test.cs
| 
 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 
39 
40 
41 
42 
 | 
 using UnityEngine; 
using System.Collections; 
public class Test: MonoBehaviour 
{ 
 //电影纹理 
 public MovieTexture movTexture; 
 void Start() 
 { 
 //设置当前对象的主纹理为电影纹理 
 renderer.material.mainTexture = movTexture; 
 //设置电影纹理播放模式为循环 
 movTexture.loop = true; 
 } 
 void OnGUI() 
 { 
 if(GUILayout.Button("播放/继续")) 
 { 
 //播放/继续播放视频 
 if(!movTexture.isPlaying) 
 { 
 movTexture.Play(); 
 } 
 } 
 if(GUILayout.Button("暂停播放")) 
 { 
 //暂停播放 
 movTexture.Pause(); 
 } 
 if(GUILayout.Button("停止播放")) 
 { 
 //停止播放 
 movTexture.Stop(); 
 } 
 } 
} 
 | 
如下图所示,点击按钮后轻松的实现播放、暂停、停止操作。默认视频大小大家可在编辑器直接缩放Plane对象平面,而如果需要在游戏运行中动态的缩放平面使用方法:
| 
 1 
 | 
 transform.localScale = new Vector(1,1,1); 
 | 
模型默认缩放系数为1,这里可以调节平面X、Y、Z三个方向的缩放系数,平面的大小会随之改变,对应视频的大小也会随之改变。
第二种播放视频的方式基于GUI。大家可以把刚刚创建的Plane对象以及世界定向光删除,直接将脚本绑定在摄像机对象中即可,接着我们简单的修改一下刚刚的游戏脚本。
Test.cs
| 
 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 
39 
40 
41 
42 
43 
44 
 | 
 using UnityEngine; 
using System.Collections; 
public class Test: MonoBehaviour 
{ 
 //电影纹理 
 public MovieTexture movTexture; 
 void Start() 
 { 
 //设置电影纹理播放模式为循环 
 movTexture.loop = true; 
 } 
 void OnGUI() 
 { 
 //绘制电影纹理 
 GUI.DrawTexture (new Rect (0,0, Screen.width, Screen.height),movTexture,ScaleMode.StretchToFill);   
 if(GUILayout.Button("播放/继续")) 
 { 
 //播放/继续播放视频 
 if(!movTexture.isPlaying) 
 { 
 movTexture.Play(); 
 } 
 } 
 if(GUILayout.Button("暂停播放")) 
 { 
 //暂停播放 
 movTexture.Pause(); 
 } 
 if(GUILayout.Button("停止播放")) 
 { 
 //停止播放 
 movTexture.Stop(); 
 } 
 } 
} 
 | 
在GUI中播放视频的原理是直接通过GUI调用DrawTexture方法,这里和绘制贴图很想了吧嘿嘿~ 目前播放视频的大小是屏幕的宽高,如果想动态的修改视频的宽或高直接修改new Rect() 视频显示区域即可,如下图所示,视频已经满满的填充在整个GUI中啦。怎么样Unity中播放视频简单吧? 哇咔咔~
移动平台上播放视频
经测试以上的方式在IOS和Android设备中是无法播放视频的,在移动设备上我们需要使用另外一种方式来播放。
| 
 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 
 | 
 using UnityEngine; 
using System.Collections; 
public class Test : MonoBehaviour { 
 void OnGUI() 
 { 
     if (GUI.Button (new Rect (20,10,200,50), "PLAY ControlMode.CancelOnTouch")) 
 { 
        Handheld.PlayFullScreenMovie("test.mp4", Color.black, FullScreenMovieControlMode.CancelOnInput); 
 } 
 if (GUI.Button (new Rect (20,90,200,25), "PLAY ControlMode.Full")) 
 { 
       Handheld.PlayFullScreenMovie("test.mp4", Color.black, FullScreenMovieControlMode.Full); 
 } 
 if (GUI.Button (new Rect (20,170,200,25), "PLAY ControlMode.Hidden")) 
 { 
       Handheld.PlayFullScreenMovie("test.mp4", Color.black, FullScreenMovieControlMode.Hidden); 
 } 
 if (GUI.Button (new Rect (20,250,200,25), "PLAY ControlMode.Minimal")) 
 { 
       Handheld.PlayFullScreenMovie("test.mp4", Color.black, FullScreenMovieControlMode.Minimal); 
 } 
 } 
} 
 | 
1.视频播放时触摸屏幕视频关闭
2.视频播放时弹出IOS高级控件,控制视频暂停播放 全屏等等。
3.视频播放时无法停止,当其播放完一次后自动关闭
4.视频播放时弹出IOS高级控件,可控制播放进度。
注意:将视频文件放置在Assets/StreamingAssets/路径下,经测试.MP4可用。 在IOS和Android上流畅播放游戏视频。
工程下载: http://vdisk.weibo.com/s/gb4Lx
视频加速播放(只适用于PC)
今天有朋友问我,怎么能让视频加快播放,我也学了一下,不果老外们都说这是一个BUG呵呵。
脚本绑定在摄像机对象上,并且要给摄像机添加AudioSource组件。
代码比较简单我就不注释了。
| 
 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 
 | 
 using UnityEngine; 
using System.Collections; 
public class NewBehaviourScript : MonoBehaviour  
{ 
 public MovieTexture mov; 
 void Start() 
 { 
 audio.clip = mov.audioClip; 
 audio.Play(); 
 mov.Play(); 
 } 
 void OnGUI() 
 { 
 if(GUI.Button(new Rect ( 310,0,100,50),"2倍速播放")) 
 { 
 audio.pitch = 2f; 
 } 
 if(GUI.Button(new Rect ( 410,0,100,50),"1倍速播放")) 
 { 
 audio.pitch = 1f; 
 } 
 GUI.DrawTexture(new Rect(0,0,300,300),mov); 
 } 
} 
 | 
- 本文固定链接: https://www.xuanyusong.com/archives/1019
 - 转载请注明: 雨松MOMO 2012年05月26日 于 雨松MOMO程序研究院 发表
 
                    
                






                
            
        
浙公网安备 33010602011771号