VideoControl.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Video;
  5. using System;
  6. using UnityEngine.UI;
  7. namespace Navigator
  8. {
  9. public class VideoControl : MonoBehaviour
  10. {
  11. public GameObject Icon;
  12. public Material material;
  13. public VideoPlayer videoPlayer;
  14. public RawImage VideoPage;
  15. public RawImage FengMian;
  16. [SerializeField]
  17. private string m_VideoURL;
  18. public string VideoURL
  19. {
  20. get { return m_VideoURL; }
  21. set
  22. {
  23. m_VideoURL = value;
  24. if (!string.IsNullOrWhiteSpace(m_VideoURL))
  25. {
  26. videoPlayer.url =/* "file://" + */m_VideoURL;
  27. }
  28. }
  29. }
  30. public bool m_IsPlaying;
  31. private void OnEnable()
  32. {
  33. if (videoPlayer != null)
  34. {
  35. if (VideoPage.texture == null)
  36. {
  37. RenderTexture texture = new RenderTexture(1024, 1024, 16, RenderTextureFormat.ARGB32);
  38. VideoPage.texture = texture;
  39. videoPlayer.targetTexture = texture;
  40. }
  41. FengMian.gameObject.SetActive(true);
  42. videoPlayer.Stop();
  43. videoPlayer.targetTexture.Release();
  44. Icon.SetActive(true);
  45. m_IsPlaying = false;
  46. videoPlayer.errorReceived += OnErrorReceived;
  47. videoPlayer.loopPointReached += VideoPlayerEnd;
  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. }
  96. }