Files
5.24BuYu/Assets/MyGame/aa/UI/DefaultPanel/LoadPanel/LoadingPanel.cs
T

128 lines
4.4 KiB
C#
Raw Normal View History

2026-04-21 23:02:51 +08:00
using DG.Tweening;
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
2026-10-15 03:57:20 +08:00
public class LoadingPanel : MonoBehaviour, ILoadingPanel
2026-04-21 23:02:51 +08:00
{
public Image image_Bg;
public Action actionEnd;
public float fadeTime = 1.3f;
2026-10-10 08:59:44 +08:00
2026-10-20 23:36:47 +08:00
/// <summary>过场视频显示的 RawImage(LoadPanel 预制体子节点 0),显示常驻 VideoPlayer 投影的共享 RenderTexture</summary>
2026-10-20 15:30:30 +08:00
private RawImage videoImage;
2026-10-20 23:36:47 +08:00
/// <summary>常驻过场视频播放器(UIManager.loadVideoPlayer,挂在场景常驻 Manager 物体上;加载面板只是显示它的输出,不自己播放)</summary>
private VideoPlayer loadVideo;
2026-10-20 15:30:30 +08:00
private void Awake()
2026-10-15 03:57:20 +08:00
{
2026-10-20 15:30:30 +08:00
if (transform.childCount > 0)
videoImage = transform.GetChild(0).GetComponent<RawImage>();
2026-10-15 03:57:20 +08:00
}
/// <summary>设置背景图</summary>
public void SetBackground(Sprite sprite)
{
image_Bg.sprite = sprite;
}
/// <summary>注册播放结束回调</summary>
public void SetOnEnd(Action action)
{
actionEnd = action;
}
2026-10-10 08:59:44 +08:00
private void Start()
{
// ⭐ 视频播放期间预载大厅主面板:把 HomeUIRes 的加载提前到这段视频等待时间里,
// 登录成功 UILoginPanel.Succeed() 的 ShowAsync(HomeMainPanel) 命中缓存秒开,
// 不再出现"登录成功后进大厅白等"。
StartCoroutine(PreloadHomeMainRoutine());
2026-10-20 23:36:47 +08:00
StartCoroutine(BindLoadVideoRoutine());
2026-10-10 08:59:44 +08:00
}
2026-10-20 15:30:30 +08:00
/// <summary>播放过程中预载大厅主面板(避免大包解压和视频启动抢主线程)</summary>
2026-10-10 08:59:44 +08:00
private IEnumerator PreloadHomeMainRoutine()
{
2026-10-20 23:36:47 +08:00
// 1) 等常驻过场视频真正起播(最多等 5 秒兜底,防止视频未播时死等)
2026-10-10 14:47:01 +08:00
float waited = 0f;
2026-10-20 23:36:47 +08:00
while ((loadVideo == null || !loadVideo.isPlaying) && waited < 5f)
2026-10-10 14:47:01 +08:00
{
waited += Time.deltaTime;
yield return null;
}
yield return new WaitForSeconds(0.5f); // 让视频先渲染几帧,起播更顺
// 2) 播放过程中加载:等 UIManager 就绪后发起预载
2026-10-10 08:59:44 +08:00
while (UIManager.Instance == null)
yield return null;
yield return null; // 等 UIManager 初始化完成(地址表装载)
UIManager.Instance.PreloadPanel(UIPanel.HomeMainPanel);
}
2026-10-20 15:30:30 +08:00
/// <summary>
2026-10-20 23:36:47 +08:00
/// 常驻过场视频由 UIManager.loadVideoPlayer 播放并投影到共享 RenderTexture,
2026-10-20 15:30:30 +08:00
/// 这里等它就绪后监听 loopPointReached:视频第一遍循环播放完毕时隐藏跳过按钮。
/// (面板销毁时协程终止;OnDestroy 里也会解除监听)
/// </summary>
2026-10-20 23:36:47 +08:00
private IEnumerator BindLoadVideoRoutine()
2026-10-20 15:30:30 +08:00
{
2026-10-20 23:36:47 +08:00
while (loadVideo == null)
2026-10-20 15:30:30 +08:00
{
yield return null;
if (UIManager.Instance == null) continue;
2026-10-20 23:36:47 +08:00
loadVideo = UIManager.Instance.loadVideoPlayer;
2026-10-20 15:30:30 +08:00
}
2026-10-20 23:36:47 +08:00
loadVideo.loopPointReached += OnFirstLoopPointReached;
2026-10-20 15:30:30 +08:00
}
/// <summary>视频第一遍播完:隐藏跳过按钮并解除监听</summary>
private void OnFirstLoopPointReached(VideoPlayer vp)
{
vp.loopPointReached -= OnFirstLoopPointReached;
2026-10-20 23:36:47 +08:00
loadVideo = null;
2026-10-20 15:30:30 +08:00
HideSkipButton();
}
private void OnDestroy()
{
2026-10-20 23:36:47 +08:00
if (loadVideo != null)
loadVideo.loopPointReached -= OnFirstLoopPointReached;
2026-10-20 15:30:30 +08:00
}
private void HideSkipButton()
{
if (GameManager.Instance != null && GameManager.Instance.btn_Skip != null)
GameManager.Instance.btn_Skip.gameObject.SetActive(false);
}
2026-04-21 23:02:51 +08:00
/// <summary>
/// 进入条进度值
/// </summary>
/// <param name="count">最大100 100时直接删除</param>
public void LoadBar(int count)
{
if (count == 100)
{
2026-10-10 08:59:44 +08:00
// ⭐ 兜底再预载一次:若启动时 UIManager 尚未就绪导致首次发起无效,这里补上
if (UIManager.Instance != null)
UIManager.Instance.PreloadPanel(UIPanel.HomeMainPanel);
2026-10-20 15:30:30 +08:00
HideSkipButton();
2026-04-21 23:02:51 +08:00
Destroy(gameObject, fadeTime);
2026-10-20 15:30:30 +08:00
if (videoImage != null && videoImage.gameObject.activeSelf)
2026-04-21 23:02:51 +08:00
{
2026-10-20 15:30:30 +08:00
videoImage.DOFade(0, fadeTime);
2026-04-21 23:02:51 +08:00
}
2026-10-20 15:30:30 +08:00
if (image_Bg != null && image_Bg.gameObject.activeSelf)
2026-04-21 23:02:51 +08:00
{
image_Bg.DOFade(0, fadeTime);
}
actionEnd?.Invoke();
}
}
}