VideoControl.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Video;
  5. using System;
  6. using UnityEngine.UI;
  7. public class VideoControl : MonoBehaviour
  8. {
  9. public GameObject Icon;
  10. public Material material;
  11. public VideoPlayer videoPlayer;
  12. public RawImage VideoPage;
  13. public RawImage FengMian;
  14. [SerializeField]
  15. private string m_VideoURL;
  16. public string VideoURL
  17. {
  18. get { return m_VideoURL; }
  19. set
  20. {
  21. m_VideoURL = value;
  22. if (!string.IsNullOrWhiteSpace(m_VideoURL))
  23. {
  24. videoPlayer.url =/* "file://" + */m_VideoURL;
  25. }
  26. }
  27. }
  28. public bool m_IsPlaying;
  29. private void OnEnable()
  30. {
  31. if (videoPlayer != null)
  32. {
  33. if (VideoPage.texture == null)
  34. {
  35. RenderTexture texture = new RenderTexture(1024, 1024, 16, RenderTextureFormat.ARGB32);
  36. VideoPage.texture = texture;
  37. videoPlayer.targetTexture = texture;
  38. }
  39. FengMian.gameObject.SetActive(true);
  40. videoPlayer.Stop();
  41. videoPlayer.targetTexture.Release();
  42. Icon.SetActive(true);
  43. m_IsPlaying = false;
  44. videoPlayer.errorReceived += OnErrorReceived;
  45. //videoPlayer.loopPointReached += VideoPlayerEnd;
  46. if(videoPlayer.isLooping == false)
  47. videoPlayer.isLooping= true;
  48. }
  49. }
  50. private void VideoPlayerEnd(VideoPlayer source)
  51. {
  52. if (videoPlayer != null)
  53. {
  54. FengMian.gameObject.SetActive(true);
  55. videoPlayer.Stop();
  56. videoPlayer.targetTexture.Release();
  57. Icon.SetActive(true);
  58. m_IsPlaying = false;
  59. }
  60. }
  61. private void OnErrorReceived(VideoPlayer source, string message)
  62. {
  63. Debug.LogError(message);
  64. }
  65. public void PlayVideo()
  66. {
  67. Debug.Log("m_IsPlaying: " + m_IsPlaying);
  68. if (videoPlayer == null)
  69. return;
  70. Debug.Log(videoPlayer.url);
  71. if (!m_IsPlaying)
  72. {
  73. FengMian.gameObject.SetActive(false);
  74. videoPlayer.Play();
  75. Icon.SetActive(false);
  76. }
  77. else
  78. {
  79. videoPlayer.Pause();
  80. Icon.SetActive(true);
  81. }
  82. m_IsPlaying = !m_IsPlaying;
  83. }
  84. private void OnDisable()
  85. {
  86. videoPlayer.errorReceived -= OnErrorReceived;
  87. //videoPlayer.loopPointReached -= VideoPlayerEnd;
  88. }
  89. }
  90. public class VideoInfo
  91. {
  92. public VideoClip Clip;
  93. public string VideoUrl;
  94. //public int index;
  95. }