Unity实现RawImage图片显示视频 、灵活配置控制视频播放

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;

namespace SimpleUIFrame
{
	public class Ctrl_MovingCtrl : MonoBehaviour
	{
        //Raw Image to Show Video Images [Assign from the Editor]
        public RawImage image;
        //Video To Play [Assign from the Editor]
        public VideoClip videoToPlay;

        private VideoPlayer videoPlayer;
        private VideoSource videoSource;

        //Audio
        private AudioSource audioSource;
        //播放按钮本身
        public GameObject btnVideosPlay;

        // Use this for initialization
        void Start()
        {
            Application.runInBackground = true;

        }
        /// 
        /// 播放视频
        /// 
        public void PlayVideos()
        {
            StartCoroutine(playVideo());
            btnVideosPlay.SetActive(false);
        }
        /// 
        /// 暂停视频
        /// 
        /// 
        /// 
        public void PauseVideos()
        {
            this.gameObject.GetComponent().Pause();
        }

        /// 
        /// 继续播放视频
        /// 
        /// 
        public void ContinueVideos()
        {
            this.gameObject.GetComponent().Play();
        }


        IEnumerator playVideo()
        {
            //Add VideoPlayer to the GameObject
            videoPlayer = gameObject.AddComponent();

            //Add AudioSource
            audioSource = gameObject.AddComponent();

            //Disable Play on Awake for both Video and Audio
            videoPlayer.playOnAwake = false;
            audioSource.playOnAwake = false;

            //We want to play from video clip not from url
            videoPlayer.source = VideoSource.VideoClip;

            //Set Audio Output to AudioSource
            videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;

            //Assign the Audio from Video to AudioSource to be played
            videoPlayer.EnableAudioTrack(0, true);
            videoPlayer.SetTargetAudioSource(0, audioSource);

            //Set video To Play then prepare Audio to prevent Buffering
            videoPlayer.clip = videoToPlay;
            videoPlayer.Prepare();

            //Wait until video is prepared
            while (!videoPlayer.isPrepared)
            {
                //Debug.Log("Preparing Video");
                yield return null;
            }

            //Debug.Log("Done Preparing Video");

            //Assign the Texture from Video to RawImage to be displayed
            image.texture = videoPlayer.texture;

            //Play Video
            videoPlayer.Play();

            //Play Sound
            audioSource.Play();

            //Debug.Log("Playing Video");
            while (videoPlayer.isPlaying)
            {
                //Debug.LogWarning("Video Time: " + Mathf.FloorToInt((float)videoPlayer.time));
                yield return null;
            }

            //Debug.Log("Done Playing Video");
        }

    }


}


使用方法
①将该脚本添加给一个空物体,比如改名叫做_ScriptsMgr
②指定需要显示视频的RawImage
③指定需要播放的视频
④指定播放按钮
Unity实现RawImage图片显示视频 、灵活配置控制视频播放_第1张图片
⑤给播放按钮指定方法
Unity实现RawImage图片显示视频 、灵活配置控制视频播放_第2张图片
⑥运行点击播放按钮则播放按钮隐藏视频在RawImage对象上播放

你可能感兴趣的:(Unity基础)