找回密码
 立即注册
楼主: 杨建辉

[脚本] unity中movieTexture实现视频播放

[复制链接]
发表于 2012-11-15 22:46 | 显示全部楼层 |阅读模式
可以直接将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倍
发表于 2014-5-27 11:21 | 显示全部楼层
好东西 谢谢分享
发表于 2016-8-26 13:52 | 显示全部楼层

不错 不错 不错
发表于 2017-3-29 10:36 | 显示全部楼层
顶顶多好
发表于 2017-3-29 10:57 | 显示全部楼层
真心顶
发表于 2017-3-29 10:46 | 显示全部楼层
说的非常好
发表于 2017-3-29 10:43 | 显示全部楼层
很好哦
发表于 2017-3-29 10:42 | 显示全部楼层
不错不错
发表于 2017-3-31 22:01 | 显示全部楼层
顶顶多好
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-12 07:25 , Processed in 0.092360 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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