Unity3D播放视频

点击这里下载视频转换器,该转换器会把其他格式转化为ogv格式,直接被unity识别,不需要安装quicktime不需要重启,更为重要的是比把mov导入unity的要清晰。




```usingUnityEngine;

usingSystem.Collections;

publicclassMovieTest : MonoBehaviour

{

//电影纹理

publicMovieTexture movTexture;

voidStart()

{

//设置当前对象的主纹理为电影纹理

renderer.material.mainTexture = movTexture;

//设置电影纹理播放模式为循环

movTexture.loop =true;

//StartCoroutine(DownLoadMovie());

}

voidOnGUI()

{

if(GUILayout.Button("播放/继续"))

{

//播放/继续播放视频

if(!movTexture.isPlaying)

{

movTexture.Play();

}

}

if(GUILayout.Button("暂停播放"))

{

//暂停播放

movTexture.Pause();

}

if(GUILayout.Button("停止播放"))

{

//停止播放

movTexture.Stop();

}

}

IEnumerator DownLoadMovie()

{

WWW www =newWWW("http://127.0.0.1/Head.ogv");

yieldreturnwww;

Debug.Log(Time.time);

movTexture = www.movie;

//设置当前对象的主纹理为电影纹理

renderer.material.mainTexture = movTexture;

movTexture.loop =true;

}

}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

加声音

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

usingUnityEngine;

usingSystem.Collections;

publicclassMovieTest : MonoBehaviour

{

//电影纹理

publicMovieTexture movTexture;

//声音

publicAudioSource movAudio;

voidStart()

{

//设置当前对象的主纹理为电影纹理

renderer.material.mainTexture = movTexture;

//设置电影纹理播放模式为循环

movTexture.loop =true;

//StartCoroutine(DownLoadMovie());

}

voidOnGUI()

{

if(GUILayout.Button("播放/继续"))

{

//播放/继续播放视频

if(!movTexture.isPlaying)

{

movTexture.Play();

movAudio.Play();

}

}

if(GUILayout.Button("暂停播放"))

{

//暂停播放

movTexture.Pause();

movAudio.Pause();

}

if(GUILayout.Button("停止播放"))

{

//停止播放

movTexture.Stop();

movAudio.Stop();

}

}

IEnumerator DownLoadMovie()

{

WWW www =newWWW("http://127.0.0.1/Head.ogv");

yieldreturnwww;

Debug.Log(Time.time);

movTexture = www.movie;

//设置当前对象的主纹理为电影纹理

renderer.material.mainTexture = movTexture;

movTexture.loop =true;

}

}


注:1280*720分辨率的视频以此方法播放不会产生黑边。

以前只是测试,没有实际使用,发现多次点击播放后会有内存泄露。如下方法解决


usingUnityEngine;

usingSystem.Collections;

publicclassWebMovie : MonoBehaviour {

//电影纹理

publicMovieTexture movTexture;

WWW www;

voidStart()

{

}

voidOnGUI()

{

if(GUILayout.Button("加载视频"))

{

StartCoroutine(DownLoadMovie());

}

if(GUILayout.Button("播放/继续"))

{

//播放/继续播放视频

if(!movTexture.isPlaying)

{

movTexture.Play();

}

}

if(GUILayout.Button("暂停播放"))

{

//暂停播放

movTexture.Pause();

}

if(GUILayout.Button("停止播放"))

{

//停止播放

movTexture.Stop();

}

if(GUILayout.Button("卸载内存"))

{

renderer.material.mainTexture =null;

movTexture =null;

www =null;

Resources.UnloadUnusedAssets();

}

}

IEnumerator DownLoadMovie()

{

www =newWWW("http://127.0.0.1/mumaren.ogv");

yieldreturnwww;

Debug.Log(Time.time);

movTexture = www.movie;

//设置当前对象的主纹理为电影纹理

renderer.material.mainTexture = movTexture;

movTexture.loop =true;

}

}```

就是销毁www对象,但是在这之前,要去掉别的对象对www的引用,比如材质球的主贴图、声明的movTexture,或许还有audioClip。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容