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
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
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中啦。
移動平台上播放視頻
經測試以上的方式在IOS和Android設備中是無法播放視頻的,在移動設備上我們需要使用另外一種方式來播放。
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上流暢播放游戲視頻。
視頻加速播放(只適用於PC)
腳本綁定在攝像機對象上,並且要給攝像機添加AudioSource組件。
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);
}
}

