杨建辉 发表于 2012-11-15 22:46

unity中movieTexture实现视频播放

可以直接将ogg格式的材质赋给物体,这样需要加代码让这个这个视频播放起来。

function Update () {
renderer.material.mainTexture.Play();
}

这里的代码,实现播放暂停以及切换视频。不过这个代码要赋给摄像机,如果赋给物体的话在编辑器里好用,但是发布出来的时候就不好用了。

public var bofang:boolean = false;

public var movieTexture:MovieTexture;

function Start(){

movieTexture.Stop();

}

private var flag01:boolean = true;
function Update(){
if(!bofang){
return;
}

// 赋值剪辑到音频源
// 与音频同步播放
audio.clip = movieTexture.audioClip;
// 播放视频和音频

movieTexture.loop = true;

//renderer.material.mainTexture.wrapMode = TextureWrapMode.Clamp;
if(flag01){
movieTexture.Play();
audio.Play();
}else{
movieTexture.Pause();
audio.Pause();
}

}

//加上 播放暂停 关闭

//播放器
private var BFQWidth:float = 650;
private var BFQHeight:float = 490;
var BFQTexture:Texture;
private var BFQGBWidth:float = 16;
private var BFQGBHeight:float = 15;
var BFQGBStyle:GuiStyle;//关闭按钮
//播放器 播放暂停
private var BFQPlayWidth:float = 22;
private var BFQPlayHeight:float = 22;
var BFQPlayStyle:GUIStyle;//播放按钮
var BFQPauseStyle:GUIStyle;//暂停按钮

function OnGUI(){
if(!bofang){
return;
}
GUI.DrawTexture(new Rect(187, 139, BFQWidth, BFQHeight), BFQTexture, ScaleMode.StretchToFill);
GUI.DrawTexture (Rect (192,144, 640, 480),movieTexture,ScaleMode.ScaleToFit );
movieTexture.Play();
//关闭按钮
if(GUI.Button (Rect (800, 155, 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){
if(GUI.Button (Rect (800, 597, BFQPlayWidth, BFQPlayHeight),"",BFQPlayStyle)){
flag01 = true;
}

}else{
if(GUI.Button (Rect (800, 597, 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)

转自:http://ilaoxu.com/the-unity-sino-movietexture-video-player.html

醉里挑灯看剑 发表于 2013-6-9 18:38

不知道什么地方出了问题,播放速度太快了,提高5-6倍

aaabbbsss12345 发表于 2014-5-27 11:21

好东西 谢谢分享

liu542083684 发表于 2016-8-26 13:52


不错 不错 不错

雪白的肚皮 发表于 2017-3-29 10:36

顶顶多好

zy_const 发表于 2017-3-29 10:57

真心顶

s724682963 发表于 2017-3-29 10:46

说的非常好

NumberG 发表于 2017-3-29 10:43

很好哦

lilili 发表于 2017-3-29 10:42

不错不错

leeliang1987 发表于 2017-3-31 22:01

顶顶多好
页: [1]
查看完整版本: unity中movieTexture实现视频播放