unity3D ~ 视频播放

来源:互联网 发布:拉货搬家软件 编辑:程序博客网 时间:2024/06/05 23:06
5.0.0版本之前:
电脑上必须安装quicktime播放器,
电脑上必须安装quicktime播放器,
电脑上必须安装quicktime播放器。重要的事说三遍,被这坑过。
如果你不确定是否安装了,先把视频拖到项目中,在项目里边双击一下导入的视频,能播放就是安装过了。
方法一:
1:建立一个cube和一个脚本,脚本拖到Cube,脚本代码如下:
using UnityEngine;using System.Collections;public class Sea_Movie: MonoBehaviour{//电影纹理    public MovieTexture movTexture;    void Start()    {         renderer.material.mainTexture = movTexture;//unity5.0之前用这个方法         GetComponent<MeshRenderer> ().material.mainTexture = movTexture;//unity5.0之后用这个方法。        //设置电影纹理播放模式为循环        movTexture.loop = true;    }    void OnGUI()    {        if(GUILayout.Button("播放/继续"))        {            //播放/继续播放视频            if(!movTexture.isPlaying)            {                movTexture.Play();            }        }        if(GUILayout.Button("暂停播放"))        {            //暂停播放            movTexture.Pause();        }         if(GUILayout.Button("停止播放"))        {            //停止播放            movTexture.Stop();        }            }    }


2:把MP4拖给这个脚本上的movTexture,如下图箭头的位置。
3这时你运行一下,如果遇到视频反过来了,旋转一下就可以了。还有问题是不是没有声音,我们现在要做的就是添加声音,看下图:
方法二:利用UI的方法。只是脚本不一样,其他的都一样即可。
using UnityEngine;using System.Collections;public class Sea_Movie: 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();        }            }    }
以上我也是百度的资料,然后总结一起的,站在巨人的肩膀上看更远的世界,他们是巨人,我是巨人肩上的小丑。

</pre>
0 0
原创粉丝点击