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
[代碼]c#/cpp/oc代碼:
01 using UnityEngine; 02 using System.Collections; 03 04 public class Test: MonoBehaviour 05 { 06 07 //電影紋理 08 public MovieTexture movTexture; 09 10 void Start() 11 { 12 //設置當前對象的主紋理為電影紋理 13 renderer.material.mainTexture = movTexture; 14 //設置電影紋理播放模式為循環 15 movTexture.loop = true; 16 } 17 18 void OnGUI() 19 { 20 if(GUILayout.Button("播放/繼續")) 21 { 22 //播放/繼續播放視頻 23 if(!movTexture.isPlaying) 24 { 25 movTexture.Play(); 26 } 27 28 } 29 30 if(GUILayout.Button("暫停播放")) 31 { 32 //暫停播放 33 movTexture.Pause(); 34 } 35 36 if(GUILayout.Button("停止播放")) 37 { 38 //停止播放 39 movTexture.Stop(); 40 } 41 } 42 }新聞熱點
疑難解答