美文网首页Unity
unity 5.6及以上的VideoPlayer

unity 5.6及以上的VideoPlayer

作者: 带着面包去流浪 | 来源:发表于2018-11-15 15:13 被阅读826次

    引入命名空间:using UnityEngine.Video;

    (win7系统使用VideoPlayer可能无法播放视频,win10可以)

    RenderMode:

    1、Camera Far Plane:基于摄像机的渲染,渲染在摄像机的远平面上,需要设置用于渲染的摄像机,同时可以修改alpha通道的值做透明效果,可用于背景播放器。

    2、Camera Near Plane:基于摄像机的渲染,渲染在摄像机的近平面上,需要设置用于渲染的摄像机,同时可以修改alpha通道的值做透明效果,可用作前景播放器。

    3、Render Texture:将视频画面保存在Render Texture上,以供物体或者RawImage使用,可以用来做基于UGUI的播放器。

    4、Material Override:将视频画面复制给所选Render的Material。需要选择具有Render组件的物体,可以选择赋值的材质属性。可制作360全景视频和VR视频。

    5、Api Only:

    播放视频中的声音:

    1.添加声音组件:AudioSources

    2.VideoPlayer组件的PlayOnAwake设为false.防止出现VideoPlayer 在awake 的时候开始播放视频时声音模块还没来得及加载,而出现没有声音的情况。

    3设置AudioOutMode: AudioSource.


    代码实现:

    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;

    using UnityEngine.Video;

    public class BGVideoScript : MonoBehaviour {

        VideoPlayer videoPlayer;

        VideoClip videoClip;

        //音频组件

        AudioSource source;

        void Start () {

            videoPlayer = this.GetComponent<VideoPlayer>();

            videoPlayer.playOnAwake = false;

            source = this.GetComponent<AudioSource>();

            videoClip = Resources.Load<VideoClip>("Video/test");

            videoPlayer.SetTargetAudioSource(0, source);

            if (!videoPlayer.isPlaying)

            {

                videoPlayer.clip = videoClip;

                videoPlayer.Play();

            }

        }

    }


    加载StreamingAssets文件夹下的视频

    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;

    using UnityEngine.Video;

    public class videoplayer : MonoBehaviour {

        public VideoPlayer videoPlayer;

      public AudioSource source;

        void Start () {

            videoPlayer.playOnAwake = false;

            videoPlayer.source = VideoSource.Url;

            videoPlayer.SetTargetAudioSource(0, source);

        }

        void Update()

        {

            if (Input.GetMouseButtonDown(0))

            {

                if (videoPlayer.isPlaying)

                {

                    videoPlayer.Stop();

                }

                videoPlayer.url = GetFilePath("赵二");

                if (!string.IsNullOrEmpty(videoPlayer.url))

                {

                    videoPlayer.Play();

                }

            }

        }

        string GetFilePath(string videoName)

        {

            string filePath = Application.streamingAssetsPath + "/Video/" + videoName + ".mp4";

            return filePath;

        }

    }

    相关文章

      网友评论

        本文标题:unity 5.6及以上的VideoPlayer

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