VideoView.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Video;
  6. /* 教程界面(主界面功能) */
  7. public class VideoView : MonoBehaviour, MenuBackInterface
  8. {
  9. GameObject videoBG;
  10. RawImage videoBox;
  11. VideoPlayer videoPlayer;
  12. GameObject btnVideoPlay;
  13. Button btnVideoBack;
  14. GameObject bottom;
  15. Scrollbar progressBar;
  16. void Awake()
  17. {
  18. videoBG = transform.Find("VideoBG").gameObject;
  19. videoBox = videoBG.GetComponentInChildren<RawImage>();
  20. videoPlayer = videoBG.GetComponentInChildren<VideoPlayer>();
  21. btnVideoPlay = transform.Find("BtnPlay").gameObject;
  22. btnVideoBack = transform.Find("BtnBack").GetComponent<Button>();
  23. bottom = this.transform.Find("Bottom").gameObject;
  24. progressBar = bottom.transform.Find("Scrollbar").GetComponent<Scrollbar>();
  25. videoBG.GetComponent<Button>().onClick.AddListener(delegate() {
  26. if (videoPlayer.isPaused) {
  27. videoPlayer.Play();
  28. } else {
  29. videoPlayer.Pause();
  30. }
  31. });
  32. btnVideoBack.onClick.AddListener(delegate() {
  33. AudioMgr.ins.PlayBtn();
  34. Destroy(this.gameObject);
  35. });
  36. progressBar.onValueChanged.AddListener(delegate(float v) {
  37. double currentTime = v * videoPlayer.length;
  38. if (currentTime < 0.5) currentTime = 0.5;
  39. videoPlayer.time = currentTime;
  40. showVideoTime(currentTime, videoPlayer.length);
  41. });
  42. ShowUiForPause(false);
  43. }
  44. void Start()
  45. {
  46. PersistenHandler.ins?.menuBackCtr.views.Add(this);
  47. }
  48. void OnDestroy()
  49. {
  50. PersistenHandler.ins?.menuBackCtr.views.Remove(this);
  51. }
  52. public bool OnMenuBack() {
  53. Destroy(gameObject);
  54. return true;
  55. }
  56. float unpreparetime = 0;
  57. void Update() {
  58. if (videoPlayer.isPrepared) {
  59. if (videoPlayer.isPaused) {
  60. ShowUiForPause(true);
  61. } else {
  62. ShowUiForPause(false);
  63. }
  64. } else {
  65. //预备时间超时,则退出加载视频
  66. unpreparetime += Time.deltaTime;
  67. if (unpreparetime >= 6) {
  68. Destroy(this.gameObject);
  69. }
  70. }
  71. Texture videoTexture = videoPlayer.texture;
  72. if (videoTexture) {
  73. if (!videoBox.enabled) videoBox.enabled = true;
  74. videoBox.texture = videoTexture;
  75. if (videoPlayer.isPlaying) {
  76. progressBar.SetValueWithoutNotify(float.Parse((videoPlayer.time / videoPlayer.length).ToString()));
  77. showVideoTime();
  78. }
  79. } else {
  80. if (videoBox.enabled) videoBox.enabled = false;
  81. }
  82. }
  83. void ShowUiForPause(bool value)
  84. {
  85. btnVideoPlay.SetActive(value);
  86. btnVideoBack.gameObject.SetActive(value);
  87. bottom.SetActive(value);
  88. }
  89. void showVideoTime()
  90. {
  91. showVideoTime(videoPlayer.time, videoPlayer.length);
  92. }
  93. void showVideoTime(double t1, double t2)
  94. {
  95. progressBar.GetComponentInChildren<Text>().text = toTimeStr(t1) + " / " + toTimeStr(t2);
  96. }
  97. string toTimeStr(double time)
  98. {
  99. string str = "";
  100. int hour = (int)(time / 3600);
  101. if (hour < 10) str += "0";
  102. str += hour + ":";
  103. int min = (int)(time % 3600 / 60);
  104. if (min < 10) str += "0";
  105. str += min + ":";
  106. int sec = (int)(time % 60);
  107. if (sec < 10) str += "0";
  108. str += sec;
  109. return str;
  110. }
  111. public string url {
  112. set {
  113. this.videoPlayer.url = value;
  114. }
  115. }
  116. }