VideoControl.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. }
  47. }
  48. private void VideoPlayerEnd(VideoPlayer source)
  49. {
  50. if (videoPlayer != null)
  51. {
  52. FengMian.gameObject.SetActive(true);
  53. videoPlayer.Stop();
  54. videoPlayer.targetTexture.Release();
  55. Icon.SetActive(true);
  56. m_IsPlaying = false;
  57. }
  58. }
  59. private void OnErrorReceived(VideoPlayer source, string message)
  60. {
  61. Debug.LogError(message);
  62. }
  63. public void PlayVideo()
  64. {
  65. Debug.Log("m_IsPlaying: " + m_IsPlaying);
  66. if (videoPlayer == null)
  67. return;
  68. Debug.Log(videoPlayer.url);
  69. if (!m_IsPlaying)
  70. {
  71. FengMian.gameObject.SetActive(false);
  72. videoPlayer.Play();
  73. Icon.SetActive(false);
  74. }
  75. else
  76. {
  77. videoPlayer.Pause();
  78. Icon.SetActive(true);
  79. }
  80. m_IsPlaying = !m_IsPlaying;
  81. }
  82. private void OnDisable()
  83. {
  84. videoPlayer.errorReceived -= OnErrorReceived;
  85. videoPlayer.loopPointReached -= VideoPlayerEnd;
  86. }
  87. }
  88. public class VideoInfo
  89. {
  90. public VideoClip Clip;
  91. public string VideoUrl;
  92. //public int index;
  93. }