VideoView.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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
  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. float unpreparetime = 0;
  45. void Update() {
  46. if (videoPlayer.isPrepared) {
  47. if (videoPlayer.isPaused) {
  48. ShowUiForPause(true);
  49. } else {
  50. ShowUiForPause(false);
  51. }
  52. } else {
  53. //预备时间超时,则退出加载视频
  54. unpreparetime += Time.deltaTime;
  55. if (unpreparetime >= 6) {
  56. Destroy(this.gameObject);
  57. }
  58. }
  59. Texture videoTexture = videoPlayer.texture;
  60. if (videoTexture) {
  61. if (!videoBox.enabled) videoBox.enabled = true;
  62. videoBox.texture = videoTexture;
  63. if (videoPlayer.isPlaying) {
  64. progressBar.SetValueWithoutNotify(float.Parse((videoPlayer.time / videoPlayer.length).ToString()));
  65. showVideoTime();
  66. }
  67. } else {
  68. if (videoBox.enabled) videoBox.enabled = false;
  69. }
  70. }
  71. void ShowUiForPause(bool value)
  72. {
  73. btnVideoPlay.SetActive(value);
  74. btnVideoBack.gameObject.SetActive(value);
  75. bottom.SetActive(value);
  76. }
  77. void showVideoTime()
  78. {
  79. showVideoTime(videoPlayer.time, videoPlayer.length);
  80. }
  81. void showVideoTime(double t1, double t2)
  82. {
  83. progressBar.GetComponentInChildren<Text>().text = toTimeStr(t1) + " / " + toTimeStr(t2);
  84. }
  85. string toTimeStr(double time)
  86. {
  87. string str = "";
  88. int hour = (int)(time / 3600);
  89. if (hour < 10) str += "0";
  90. str += hour + ":";
  91. int min = (int)(time % 3600 / 60);
  92. if (min < 10) str += "0";
  93. str += min + ":";
  94. int sec = (int)(time % 60);
  95. if (sec < 10) str += "0";
  96. str += sec;
  97. return str;
  98. }
  99. public string url {
  100. set {
  101. this.videoPlayer.url = value;
  102. }
  103. }
  104. }