123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Video;
- using System;
- using UnityEngine.UI;
- public class VideoControl : MonoBehaviour
- {
- public GameObject Icon;
- public Material material;
- public VideoPlayer videoPlayer;
- public RawImage VideoPage;
- public RawImage FengMian;
- [SerializeField]
- private string m_VideoURL;
- public string VideoURL
- {
- get { return m_VideoURL; }
- set
- {
- m_VideoURL = value;
- if (!string.IsNullOrWhiteSpace(m_VideoURL))
- {
- videoPlayer.url =/* "file://" + */m_VideoURL;
- }
- }
- }
- public bool m_IsPlaying;
-
- private void OnEnable()
- {
- if (videoPlayer != null)
- {
- if (VideoPage.texture == null)
- {
- RenderTexture texture = new RenderTexture(1024, 1024, 16, RenderTextureFormat.ARGB32);
- VideoPage.texture = texture;
- videoPlayer.targetTexture = texture;
- }
- FengMian.gameObject.SetActive(true);
- videoPlayer.Stop();
- videoPlayer.targetTexture.Release();
- Icon.SetActive(true);
- m_IsPlaying = false;
- videoPlayer.errorReceived += OnErrorReceived;
- videoPlayer.loopPointReached += VideoPlayerEnd;
- }
-
- }
- private void VideoPlayerEnd(VideoPlayer source)
- {
- if (videoPlayer != null)
- {
- FengMian.gameObject.SetActive(true);
- videoPlayer.Stop();
- videoPlayer.targetTexture.Release();
- Icon.SetActive(true);
- m_IsPlaying = false;
- }
- }
- private void OnErrorReceived(VideoPlayer source, string message)
- {
- Debug.LogError(message);
- }
- public void PlayVideo()
- {
- Debug.Log("m_IsPlaying: " + m_IsPlaying);
- if (videoPlayer == null)
- return;
- Debug.Log(videoPlayer.url);
- if (!m_IsPlaying)
- {
- FengMian.gameObject.SetActive(false);
- videoPlayer.Play();
- Icon.SetActive(false);
- }
- else
- {
- videoPlayer.Pause();
- Icon.SetActive(true);
- }
- m_IsPlaying = !m_IsPlaying;
- }
- private void OnDisable()
- {
- videoPlayer.errorReceived -= OnErrorReceived;
- videoPlayer.loopPointReached -= VideoPlayerEnd;
- }
- }
- public class VideoInfo
- {
- public VideoClip Clip;
- public string VideoUrl;
- //public int index;
- }
|