美文网首页
Unity-Video

Unity-Video

作者: 祝你万事顺利 | 来源:发表于2019-05-17 12:43 被阅读0次

Unity3D视频播放

Unity能够直接识别的视频格式.mov,.mpg,.mped,.mp4,.avi,.asf

旧版本(MovieTexture)导入的文件不是.ogv通过Unity中的QuickTIme进行转码。.ogv会直接识别

视频播放

旧版本通过一个Panel,设置MeshRender的Material的MainTexture为一个MovieTexture。

public class TestVideo : MonoBehaviour {
    private MeshRenderer meshRenderer;
    private AudioSource audioSource;
    public MovieTexture videoTexture;

    private void Awake()
    {
        audioSource = GetComponent<AudioSource>();
        GetComponent<MeshRenderer>().material.mainTexture = videoTexture;
    }
    
    private void OnGUI()
    {
        if (GUILayout.Button("play"))
        {
            if (!videoTexture.isPlaying)
            {
                videoTexture.Play();
                audioSource.Play();
            }
        }
        if (GUILayout.Button("Pause"))
        {
            if (videoTexture.isPlaying)
            {
                videoTexture.Pause();
                audioSource.Pause();
            }
        }
        if (GUILayout.Button("Stop"))
        {
            if (videoTexture.isPlaying)
            {
                videoTexture.Stop();
                audioSource.Stop();
            }
        }
    }
}

通过OnGUI绘制MovieTexture进行播放

public class TestVideo : MonoBehaviour {
    //private MeshRenderer meshRenderer;
    private AudioSource audioSource;
    public MovieTexture videoTexture;

    private void Awake()
    {
        audioSource = GetComponent<AudioSource>();
        //GetComponent<MeshRenderer>().material.mainTexture = videoTexture;
    }
    private void OnGUI()
    {
        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), videoTexture, ScaleMode.StretchToFill);
        if (GUILayout.Button("play"))
        {
            if (!videoTexture.isPlaying)
            {

                videoTexture.Play();
                audioSource.Play();
            }
            }
        if (GUILayout.Button("Pause"))
        {
            if (videoTexture.isPlaying)
            {
                videoTexture.Pause();
                audioSource.Pause();
            }
        }
        if (GUILayout.Button("Stop"))
        {
            if (videoTexture.isPlaying)
            {
                videoTexture.Stop();
                audioSource.Stop();
            }
        }
    }
}

新版动画系统

private VideoPlayer videoPlayer;
    private void Awake()
    {
        videoPlayer = GetComponent<VideoPlayer>();
    }

    private void OnGUI()
    {
        if (GUILayout.Button("Play", GUILayout.Width(100), GUILayout.Height(100)))
        {
            videoPlayer.Play();
        }
    }

相关文章

  • Unity-Video

    Unity3D视频播放 Unity能够直接识别的视频格式.mov,.mpg,.mped,.mp4,.avi,.as...

网友评论

      本文标题:Unity-Video

      本文链接:https://www.haomeiwen.com/subject/gmhnaqtx.html