| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Video;
- /* 教程界面(主界面功能) */
- public class VideoView : MonoBehaviour, MenuBackInterface
- {
- GameObject videoBG;
- RawImage videoBox;
- VideoPlayer videoPlayer;
- GameObject btnVideoPlay;
- Button btnVideoBack;
- GameObject bottom;
- Scrollbar progressBar;
- void Awake()
- {
- videoBG = transform.Find("VideoBG").gameObject;
- videoBox = videoBG.GetComponentInChildren<RawImage>();
- videoPlayer = videoBG.GetComponentInChildren<VideoPlayer>();
- btnVideoPlay = transform.Find("BtnPlay").gameObject;
- btnVideoBack = transform.Find("BtnBack").GetComponent<Button>();
- bottom = this.transform.Find("Bottom").gameObject;
- progressBar = bottom.transform.Find("Scrollbar").GetComponent<Scrollbar>();
- videoBG.GetComponent<Button>().onClick.AddListener(delegate() {
- if (videoPlayer.isPaused) {
- videoPlayer.Play();
- } else {
- videoPlayer.Pause();
- }
- });
- btnVideoBack.onClick.AddListener(delegate() {
- AudioMgr.ins.PlayBtn();
- Destroy(this.gameObject);
- });
- progressBar.onValueChanged.AddListener(delegate(float v) {
- double currentTime = v * videoPlayer.length;
- if (currentTime < 0.5) currentTime = 0.5;
- videoPlayer.time = currentTime;
- showVideoTime(currentTime, videoPlayer.length);
- });
- ShowUiForPause(false);
- }
- void Start()
- {
- PersistenHandler.ins?.menuBackCtr.views.Add(this);
- }
- void OnDestroy()
- {
- PersistenHandler.ins?.menuBackCtr.views.Remove(this);
- }
- public bool OnMenuBack() {
- Destroy(gameObject);
- return true;
- }
- float unpreparetime = 0;
- void Update() {
- if (videoPlayer.isPrepared) {
- if (videoPlayer.isPaused) {
- ShowUiForPause(true);
- } else {
- ShowUiForPause(false);
- }
- } else {
- //预备时间超时,则退出加载视频
- unpreparetime += Time.deltaTime;
- if (unpreparetime >= 6) {
- Destroy(this.gameObject);
- }
- }
- Texture videoTexture = videoPlayer.texture;
- if (videoTexture) {
- if (!videoBox.enabled) videoBox.enabled = true;
- videoBox.texture = videoTexture;
- if (videoPlayer.isPlaying) {
- progressBar.SetValueWithoutNotify(float.Parse((videoPlayer.time / videoPlayer.length).ToString()));
- showVideoTime();
- }
- } else {
- if (videoBox.enabled) videoBox.enabled = false;
- }
- }
- void ShowUiForPause(bool value)
- {
- btnVideoPlay.SetActive(value);
- btnVideoBack.gameObject.SetActive(value);
- bottom.SetActive(value);
- }
- void showVideoTime()
- {
- showVideoTime(videoPlayer.time, videoPlayer.length);
- }
- void showVideoTime(double t1, double t2)
- {
- progressBar.GetComponentInChildren<Text>().text = toTimeStr(t1) + " / " + toTimeStr(t2);
- }
- string toTimeStr(double time)
- {
- string str = "";
- int hour = (int)(time / 3600);
- if (hour < 10) str += "0";
- str += hour + ":";
- int min = (int)(time % 3600 / 60);
- if (min < 10) str += "0";
- str += min + ":";
- int sec = (int)(time % 60);
- if (sec < 10) str += "0";
- str += sec;
- return str;
- }
- public string url {
- set {
- this.videoPlayer.url = value;
- }
- }
- }
|