UnityPlayVideo.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.Video;
  5. public class UnityPlayVideo : MonoBehaviour, IPlayVideo
  6. {
  7. public Renderer VideoRenderer { get; private set; }
  8. public static UnityPlayVideo CreateVideo(Renderer videoRenderer)
  9. {
  10. UnityPlayVideo unityPlayVideo = R.Camera.AddComponent<UnityPlayVideo>();
  11. unityPlayVideo._playVideo = videoRenderer.gameObject.AddComponent<VideoPlayer>();
  12. unityPlayVideo._playVideo.renderMode = VideoRenderMode.MaterialOverride;
  13. unityPlayVideo._playVideo.source = VideoSource.Url;
  14. unityPlayVideo._playVideo.targetMaterialRenderer = videoRenderer;
  15. unityPlayVideo._playVideo.targetMaterialRenderer.material = Asset.LoadFromResources<Material>(string.Empty, "videoMat");
  16. unityPlayVideo._playVideo.audioOutputMode = VideoAudioOutputMode.None;
  17. return unityPlayVideo;
  18. }
  19. public Coroutine Play(string moviePath, string moiveName, bool isLooping = false)
  20. {
  21. return base.StartCoroutine(this.PlayCoroutine(moviePath, moiveName, isLooping));
  22. }
  23. private IEnumerator PlayCoroutine(string moviePath, string moiveName, bool isLooping = false)
  24. {
  25. this._playVideo.Stop();
  26. this._playVideo.url = string.Format("{0}/{1}/{2}", Application.streamingAssetsPath, moviePath, moiveName);
  27. this._playVideo.isLooping = isLooping;
  28. this._playVideo.Prepare();
  29. while (this._playVideo.isPrepared)
  30. {
  31. yield return null;
  32. }
  33. this._playVideo.Play();
  34. while (this._playVideo.isPlaying)
  35. {
  36. yield return null;
  37. }
  38. yield break;
  39. }
  40. public void Pause()
  41. {
  42. this._playVideo.Pause();
  43. }
  44. public void Resume()
  45. {
  46. this._playVideo.Play();
  47. }
  48. public void Stop()
  49. {
  50. if (this._playVideo != null)
  51. {
  52. this._playVideo.Stop();
  53. }
  54. }
  55. public void Destroy()
  56. {
  57. UnityEngine.Object.Destroy(this);
  58. }
  59. private VideoPlayer _playVideo;
  60. }