Files
BabyVideo/Assets/Scripts/Game.cs
2026-02-12 22:15:15 +08:00

325 lines
9.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<PlayerPanel>();
if (panel != null && panel.IsShowing)
{
LoginPanel.Show();
PlayerPanel.Hide();
}
}
}
#region
private readonly List<VideoRetData> Videos = new List<VideoRetData>();
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<string, string>
{
{ "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<PageListData<VideoRetData>>();
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<int, VideoRetData> Videos = new();
// public int CurrentVideoId = 0;
//
// // private readonly Queue<Action<VideoRetData>> _actions = new Queue<Action<VideoRetData>>();
//
// private Action<VideoRetData> _action;
// private int _lastNextValue = 0;
//
// public void GetVideoData(int next, Action<VideoRetData> 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<string, string>
// {
// { "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<PageListData<VideoRetData>>();
// if (retData != null && retData.List.Count > 0)
// {
// _page = retData.Page;
// AddVideos(retData.List);
// }
// }
//
//
// #region 视频数据获取
//
// /// <summary>
// /// 添加视频
// /// </summary>
// /// <param name="videoList"></param>
// private void AddVideos(IEnumerable<VideoRetData> videoList)
// {
// foreach (var video in videoList)
// {
// Videos.TryAdd(video.Id, video);
// }
// }
//
// /// <summary>
// /// 获取下一个视频
// /// </summary>
// /// <returns></returns>
// 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;
// }
//
// /// <summary>
// /// 获取上一个视频
// /// </summary>
// /// <returns></returns>
// public VideoRetData GetPreviousVideo()
// {
// int currentIndex = Videos.IndexOfKey(CurrentVideoId);
//
// if (currentIndex > 0)
// {
// var prevVideo = Videos.Values[currentIndex - 1];
// CurrentVideoId = prevVideo.Id;
// return prevVideo;
// }
//
// return null;
// }
//
// /// <summary>
// /// 获取第一个视频
// /// </summary>
// /// <returns></returns>
// private VideoRetData GetFirstVideo()
// {
// if (Videos.Count > 0)
// {
// var video = Videos.Values[0];
// CurrentVideoId = video.Id;
// return video;
// }
//
// return null;
// }
//
// /// <summary>
// /// 获取最后一个视频
// /// </summary>
// /// <returns></returns>
// private VideoRetData GetLastVideo()
// {
// if (Videos.Count > 0)
// {
// var video = Videos.Values[^1];
// CurrentVideoId = video.Id;
// return video;
// }
//
// return null;
// }
//
//
// /// <summary>
// /// 获取当前视频
// /// </summary>
// /// <returns></returns>
// 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
}
}