using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using UnityEngine.UI;
|
using Cysharp.Threading.Tasks;
|
|
/// <summary>
|
/// 启动加载窗口
|
/// </summary>
|
public class LaunchLoadingWin : MonoBehaviour
|
{
|
// 单例实例
|
private static LaunchLoadingWin _instance;
|
|
public static LaunchLoadingWin Instance
|
{
|
get
|
{
|
if (_instance == null)
|
{
|
Debug.Log("LaunchLoadingWin 界面未打开");
|
return null;
|
}
|
|
return _instance;
|
}
|
}
|
|
// UI组件引用
|
[SerializeField] public Text titleText;
|
[SerializeField] public Text tipsText;
|
[SerializeField] public Image progressBar;
|
[SerializeField] public Text progressText;
|
|
// 提示文本列表
|
[SerializeField] private List<string> tipsList = new List<string>();
|
|
// 当前提示索引
|
private int currentTipIndex = 0;
|
|
// 是否正在显示
|
|
// 计时器
|
private float tipChangeTimer = 0f;
|
private const float TIP_CHANGE_INTERVAL = 3f; // 提示切换间隔时间
|
|
private void Awake()
|
{
|
_instance = this;
|
|
// 初始化提示列表
|
InitTipsList();
|
|
// 初始化进度条
|
SetProgress(0f);
|
}
|
|
/// <summary>
|
/// 初始化提示列表
|
/// </summary>
|
private void InitTipsList()
|
{
|
tipsList.Clear();
|
tipsList.Add(FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.LoadingResources));
|
tipsList.Add(FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.CheckingVersionUpdate));
|
tipsList.Add(FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.InitializingGame));
|
tipsList.Add(FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.EnteringGameWorld));
|
}
|
|
|
/// <summary>
|
/// 打开窗口
|
/// </summary>
|
public static void OpenWindow()
|
{
|
if (null != _instance)
|
{
|
DestroyImmediate(_instance.gameObject);
|
_instance = null;
|
}
|
|
_instance = GameObject.Instantiate(Resources.Load<GameObject>("LaunchLoadingWin")).GetComponent<LaunchLoadingWin>();
|
_instance.gameObject.SetActive(true);
|
}
|
|
/// <summary>
|
/// 关闭窗口
|
/// </summary>
|
public void CloseWindow()
|
{
|
// 隐藏窗口而不是销毁
|
DestroyImmediate(gameObject);
|
_instance = null;
|
|
Debug.Log("关闭加载窗口");
|
}
|
|
/// <summary>
|
/// 设置进度
|
/// </summary>
|
/// <param name="progress">进度值(0-1)</param>
|
public void SetProgress(float progress)
|
{
|
// 限制进度值范围
|
progress = Mathf.Clamp01(progress);
|
|
// 更新进度条
|
if (progressBar != null)
|
{
|
progressBar.fillAmount = progress;
|
}
|
|
// 更新进度文本
|
if (progressText != null)
|
{
|
progressText.text = $"{(int)(progress * 100)}%";
|
}
|
}
|
|
/// <summary>
|
/// 设置提示文本
|
/// </summary>
|
/// <param name="tip">提示文本</param>
|
public void SetTip(string tip)
|
{
|
if (tipsText != null)
|
{
|
tipsText.text = tip;
|
}
|
}
|
|
|
/// <summary>
|
/// 异步显示加载进度
|
/// </summary>
|
/// <param name="duration">加载持续时间</param>
|
/// <param name="targetProgress">目标进度</param>
|
public async UniTask ShowProgressAsync(float duration, float targetProgress = 1.0f)
|
{
|
float startProgress = progressBar != null ? progressBar.fillAmount : 0f;
|
float currentProgress = startProgress;
|
float elapsedTime = 0f;
|
|
while (elapsedTime < duration)
|
{
|
elapsedTime += Time.deltaTime;
|
currentProgress = Mathf.Lerp(startProgress, targetProgress, elapsedTime / duration);
|
SetProgress(currentProgress);
|
await UniTask.Yield();
|
}
|
|
// 确保最终进度正确
|
SetProgress(targetProgress);
|
}
|
|
/// <summary>
|
/// 当前状态索引
|
/// </summary>
|
private int currentStateIndex = -1;
|
|
/// <summary>
|
/// 设置当前状态
|
/// </summary>
|
/// <param name="stateIndex">状态索引</param>
|
public void SetCurrentState(int stateIndex)
|
{
|
currentStateIndex = stateIndex;
|
|
// 根据状态索引更新提示文本
|
UpdateTipsByState();
|
}
|
|
/// <summary>
|
/// 根据当前状态更新提示文本
|
/// </summary>
|
private void UpdateTipsByState()
|
{
|
string tip = "";
|
|
// 根据状态索引设置对应的提示文本
|
switch (currentStateIndex)
|
{
|
case FirstPackLang.TextIndex.InitSDK:
|
tip = FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.InitializingSDK);
|
break;
|
case FirstPackLang.TextIndex.ExtractResources:
|
tip = FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.CheckingResources);
|
break;
|
case FirstPackLang.TextIndex.CheckVersion:
|
tip = FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.CheckingVersion);
|
break;
|
case FirstPackLang.TextIndex.HotUpdate:
|
tip = FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.PrepareUpdate);
|
break;
|
case FirstPackLang.TextIndex.EnterHotUpdateAssembly:
|
tip = FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.EnteringGame);
|
break;
|
}
|
|
// 设置提示文本
|
if (!string.IsNullOrEmpty(tip))
|
{
|
SetTip(tip);
|
}
|
}
|
}
|