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