using System; using System.Collections.Generic; using System.Linq; using NBC; using RenderHeads.Media.AVProVideo; using UnityEngine; using UnityEngine.Networking; namespace NBF { public class Game : MonoBehaviour { public static Game Instance { get; private set; } private void Awake() { Instance = this; } private void Start() { Net.Instance.OnError += OnNetError; } private void OnDestroy() { Net.Instance.OnError -= OnNetError; } private void OnNetError(UnityWebRequest.Result result, string msg) { if (result == UnityWebRequest.Result.ConnectionError) { // VideoManager.Instance.Pause(); var panel = UI.Inst.GetUI(); if (panel != null && panel.IsShowing) { LoginPanel.Show(); PlayerPanel.Hide(); } } } #region 视频数据 private readonly List Videos = new List(); private bool _isLastPage = false; public int Page = 0; public const int PageSize = 5; private Action OnPreLoadNextData; public VideoRetData GetVideoDataByIndex(int index) { if (index < 0 || index >= Videos.Count) return null; if (index >= Videos.Count - 2) //如果再获取倒数3个,则需要预加载后续资源 { //需要预加载了 PreLoadNextData(); } return Videos[index]; } public void PreLoadNextData(Action callback = null) { OnPreLoadNextData = callback; GetList(Page + 1); } public bool IsLastVideo(int index) { return index == Videos.Count - 1; } private void GetList(int page) { if (page < 1) return; var dic = new Dictionary { { "page", page.ToString() }, { "PageSize", PageSize.ToString() } }; Net.Instance.Send("/api/list", dic, ParseVideo); } private void ParseVideo(HttpResult httpResult) { if (httpResult.Code != 0) return; var retData = httpResult.Data.ToObject>(); if (retData != null) { if (retData.List.Count > 0) { Videos.AddRange(retData.List); _isLastPage = false; } if (retData.Page > Page && retData.List.Count < 1) { _isLastPage = true; } Page = retData.Page; } OnPreLoadNextData?.Invoke(); } #endregion // #region 视频数据 // // private int _page = 1; // private const int PageSize = 4; // // private readonly SortedList Videos = new(); // public int CurrentVideoId = 0; // // // private readonly Queue> _actions = new Queue>(); // // private Action _action; // private int _lastNextValue = 0; // // public void GetVideoData(int next, Action callback) // { // if (Videos.Count == 0) // { // callback?.Invoke(null); // GetList(_page); // return; // } // // _lastNextValue = next; // _action = callback; // if (CurrentVideoId == 0) // { // GetVideoDataConfirm(GetFirstVideo()); // } // else // { // if (next > 0) // { // GetVideoDataConfirm(GetNextVideo()); // } // else if (next < 0) // { // GetVideoDataConfirm(GetPreviousVideo()); // } // else // { // //获取当前 // GetVideoDataConfirm(GetCurrentVideo()); // } // } // } // // public void GetVideoDataConfirm(VideoRetData videoRetData) // { // _action?.Invoke(videoRetData); // _action = null; // if (videoRetData == null) // { // return; // } // // // CurrentVideoId = videoRetData.Id; // // if (_lastNextValue > 0) // { // //最后一次操作是获取下一个, // if (IsLastVideo()) // { // //获取下一页 // GetList(_page + 1); // } // } // else // { // //最后一次操作是获取上一个, // if (IsLastVideo()) // { // //获取上一页 // GetList(_page - 1); // } // } // } // // // private void GetList(int page) // { // if (page < 1) return; // var dic = new Dictionary // { // { "page", page.ToString() }, // { "PageSize", PageSize.ToString() } // }; // Net.Instance.Send("/api/list", dic, ParseVideo); // } // // private void ParseVideo(HttpResult httpResult) // { // if (httpResult.Code != 0) return; // var retData = httpResult.Data.ToObject>(); // if (retData != null && retData.List.Count > 0) // { // _page = retData.Page; // AddVideos(retData.List); // } // } // // // #region 视频数据获取 // // /// // /// 添加视频 // /// // /// // private void AddVideos(IEnumerable videoList) // { // foreach (var video in videoList) // { // Videos.TryAdd(video.Id, video); // } // } // // /// // /// 获取下一个视频 // /// // /// // private VideoRetData GetNextVideo() // { // int currentIndex = Videos.IndexOfKey(CurrentVideoId); // // if (currentIndex >= 0 && currentIndex < Videos.Count - 1) // { // var nextVideo = Videos.Values[currentIndex + 1]; // CurrentVideoId = nextVideo.Id; // return nextVideo; // } // // return null; // } // // /// // /// 获取上一个视频 // /// // /// // public VideoRetData GetPreviousVideo() // { // int currentIndex = Videos.IndexOfKey(CurrentVideoId); // // if (currentIndex > 0) // { // var prevVideo = Videos.Values[currentIndex - 1]; // CurrentVideoId = prevVideo.Id; // return prevVideo; // } // // return null; // } // // /// // /// 获取第一个视频 // /// // /// // private VideoRetData GetFirstVideo() // { // if (Videos.Count > 0) // { // var video = Videos.Values[0]; // CurrentVideoId = video.Id; // return video; // } // // return null; // } // // /// // /// 获取最后一个视频 // /// // /// // private VideoRetData GetLastVideo() // { // if (Videos.Count > 0) // { // var video = Videos.Values[^1]; // CurrentVideoId = video.Id; // return video; // } // // return null; // } // // // /// // /// 获取当前视频 // /// // /// // private VideoRetData GetCurrentVideo() // { // return Videos.GetValueOrDefault(CurrentVideoId); // } // // // 判断是否是第一个视频 // public bool IsFirstVideo() // { // if (Videos.Count == 0) return false; // return Videos.Keys[0] == CurrentVideoId; // } // // // 判断是否是最后一个视频 // public bool IsLastVideo() // { // if (Videos.Count == 0) return false; // return Videos.Keys[^1] == CurrentVideoId; // } // // #endregion // // #endregion } }