在做游戲或者是虛擬漫游一般都會用到在開始的時候加載一段視頻,這個視頻可能一個介紹整個游戲或者是整個項目的。在加載完了之后自動的跳轉到主畫面或一個場景,在前在網上百度了一下找到的大部分都是win的好不容易找到了ios的。
?
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
[csharp] view plaincopyprint?
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();?? ????????}?? ????}?? }?? 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對象平面,而如果需要在游戲運行中動態的縮放平面使用方法:
?
[csharp] view plaincopyprint?
transform.localScale?=?new ?Vector(1,1,1);?? transform.localScale = new Vector(1,1,1);
?
模型默認縮放系數為1, 這里可以調節平面X、Y、Z三個方向的縮放系數,平面的大小會隨之改變,對應視頻的大小也會隨之改變。
?
?
?第二種播放視頻的方式基于GUI。大家可以把剛剛創建的Plane對象以及世界定向光刪除,直接將腳本綁定在攝像機對象中即可,接著我們簡單的修改一下剛剛的游戲腳本。
Test.cs
[csharp] view plaincopyprint?
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();?? ????????}?? ????}?? ?? }?? 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中播放視頻簡單吧? 哇咔咔~
?
?
補充:2012年10月21日 經測試以上的方式在IOS和Android設備中是無法播放視頻的,在移動設備上我們需要使用兩外一種方式來播放。
[csharp] view plaincopyprint?
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);?? ????????}?? ?? ????}?? ?? }?? 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
轉自?http://www.xuanyusong.com/archives/1019
如果你 感覺
Handheld.PlayFullScreenMovie("Style_hd.mp4" ,Color .black, FullScreenMovieControlMode.Full);
這個方法不受控制,不好用還可以使用
iPhoneUtils.PlayMovie("Style_hd.mp4", Color.black, iPhoneMovieControlMode.Hidden,iPhoneMovieScalingMode.AspectFit);
來源http://game.ceeger.com/Script/iPhoneUtils/iPhoneUtils.PlayMovie.html
跳轉就很簡單了在執行完這個語句之后在添加一句
Application .LoadLevelAsync(1 );
比較詳細的代碼
function Start () {
?Handheld.PlayFullScreenMovie("Style_hd.mp4" ,Color .black, FullScreenMovieControlMode.Full);
? //Handheld.PlayFullScreenMovie("Style_hd.mp4", Color.black, FullScreenMovieControlMode.Hidden);
//iPhoneUtils.PlayMovie("Style_hd.mp4", Color.black, iPhoneMovieControlMode.Hidden,iPhoneMovieScalingMode.AspectFit);
Application .LoadLevelAsync(1 );
}
轉載于:https://www.cnblogs.com/xiao-wei-wei/archive/2013/03/30/2989967.html
總結
以上是生活随笔 為你收集整理的unityios开发--加载视频以及加载完成之后自动跳转 . 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。