[Unity] unity3d利用MovieTexture来实现视频播放

查看:2455 |回复:5 | 2014-10-11 01:54:26

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 大西几 于 2021-2-24 16:33 编辑

unity3d利用MovieTexture来实现视频播放
2013年07月05日 ⁄ Unity3D ⁄ 共 2250字
以前也写过这样的代码,能控制播放暂停,但是功能还不够全面,比如是否要实现循环播放,还有就是停止后播放状态没有改变,就是那个暂停按钮应该变成播放状态。这下把这些都改了,功能算是比较全面了。


public var bofang:boolean = false;
public var movieTexture:MovieTexture;
function Start(){
    try{
        movieTexture.Stop();
        movieTexture.loop = false;
    }catch(e){}

}
function playMovie(){
    movieTexture.Play();
    yield WaitForSeconds(1);
    movieTexture.loop = false;
}

private var flag01:boolean = true;
private var flag02:boolean = false;

function Update(){
    if(!bofang){
        return;
    }   
   if(movieTexture.isPlaying){
      flag02= true;
   }else{
      flag02= false;
   }
   //赋值剪辑到音频源
   //与音频同步播放  
   audio.clip  =  movieTexture.audioClip;  
   //播放视频和音频   
   //movieTexture.loop = false;   
   //renderer.material.mainTexture.wrapMode = TextureWrapMode.Clamp;  
    if(flag01){
        movieTexture.Play();
        audio.Play();  
    }else{
        movieTexture.Pause();
        audio.Pause();  
    }     
}
//加上 播放暂停 关闭
//播放器
private var BFQWidth:float = 1024;
private var BFQHeight:float = 702;
//var BFQTexture:Texture;
//private var BFQGBWidth:float = 16;
//private var BFQGBHeight:float = 15;
//var BFQGBStyle:GUIStyle;
//关闭按钮  
//播放器 播放暂停
private var BFQPlayWidth:float = 44;
private var BFQPlayHeight:float = 44;
var BFQPlayStyle:GUIStyle;
//播放按钮
var BFQPauseStyle:GUIStyle;//暂停按钮

function OnGUI(){
    if(!bofang){
       return;
    }   
    //GUI.DrawTexture(new Rect(0, 0, BFQWidth, BFQHeight), BFQTexture, ScaleMode.StretchToFill);   
    GUI.DrawTexture (Rect (0,60, 1024, 702),movieTexture,ScaleMode.ScaleToFit );   
   //movieTexture.Play();   
    /*  //关闭按钮   
    if(GUI.Button (Rect (980, 23, BFQGBWidth, BFQGBHeight),"",BFQGBStyle)){
        //GameObject.Find("Data").GetComponent("Data").cameraMain.enabled = true;
        //GameObject.Find("Data").GetComponent("Data").cameraView.enabled = false;   
        bofang = false;
        guiTexture.texture = null;   
        //gameObject.GetComponent("MovieControler").enabled = false;    }  
   */   
    //播放暂停            
    if((!flag01) || (!flag02)){
         if(GUI.Button (Rect (960, 80, BFQPlayWidth, BFQPlayHeight),"",BFQPlayStyle)){
            //Debug.Log(movieTexture.duration.ToString());      
            flag01 = true;            
             movieTexture.loop = true;           
            playMovie();
        }
    }else if(flag01 && (flag02) ){
        if(GUI.Button (Rect (960, 80, BFQPlayWidth, BFQPlayHeight),"",BFQPauseStyle)){
            flag01 = false;            
        }   
    }        
}   

function GuanBi(){
    GameObject.Find("Data").GetComponent("Data").cameraMain.enabled = true;
    GameObject.Find("Data").GetComponent("Data").cameraView.enabled = false;
    bofang = false;  guiTexture.texture = null;
    gameObject.GetComponent("MovieControler").enabled = false;
}

//   确保我们有GUI 纹理和音频源 @script  RequireComponent(GUITexture) @script  RequireComponent(AudioSource)
image.png
2014-10-11 01:54:26  
 赞 赞 1

使用道具 登录

5个回答,把该问题分享到群,邀请大神一起回答。
3#
元素帖子强,满满正能量!
回复 收起回复
2018-10-17 09:55:26   回复
 赞 赞 1

使用道具 登录

4#

这世上没有什么是元素币搞不定的,如果有,那就用更多!
回复 收起回复
2018-10-17 09:59:28   回复
 赞 赞 1

使用道具 登录

5#
厉害了厉害厉害  谢楼主分享
回复 收起回复
2018-10-17 10:08:30   回复
 赞 赞 1

使用道具 登录

6#
回复 收起回复
2018-10-17 10:10:43   回复
 赞 赞 1

使用道具 登录

CG 游戏行业专业问题

Unity3D技术手机游戏引擎手游引擎
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表