2025-08-19 20:22:10 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using Logging;
|
|
|
|
|
using TMPro;
|
|
|
|
|
using UnityEngine;
|
2025-08-20 17:55:22 +08:00
|
|
|
|
using UnityEngine.SceneManagement;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
|
|
|
|
namespace Base
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public class Launcher : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public GameObject loadingUI;
|
|
|
|
|
public Gradient progressBar; // 渐变色条
|
|
|
|
|
public TMP_Text describeText; // 描述文本
|
|
|
|
|
|
|
|
|
|
public float duration = 0.5f; // 过渡时间
|
|
|
|
|
public float fadeDuration = 2f; // 不透明度渐隐的时间
|
|
|
|
|
private float _currentProgress = 0f; // 当前进度
|
|
|
|
|
|
|
|
|
|
private Color textColor;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private readonly string[] _loadingSteps =
|
|
|
|
|
{
|
|
|
|
|
"初始化日志", "正在载入定义", "正在加载图片资源", "正在切割瓦片", "正在加载区分派系",
|
|
|
|
|
"正在加载物品"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public float Progress
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_currentProgress = value;
|
|
|
|
|
if (value < 0.5f)
|
|
|
|
|
{
|
|
|
|
|
progressBar.color2 = Color.Lerp(Color.black, Color.white, value * 2);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
progressBar.color2 = Color.white;
|
|
|
|
|
progressBar.color1 = Color.Lerp(Color.black, Color.white, (value - 0.5f) * 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
progressBar.Refresh();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float Opacity
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
var alpha = (byte)(value * 255);
|
|
|
|
|
progressBar.color1.a = alpha;
|
|
|
|
|
progressBar.color2.a = alpha;
|
|
|
|
|
describeText.color = value > 0.5f ? Color.Lerp(new Color(1, 1, 1, 0), textColor, (value - 0.5f) * 2) : new Color(1, 1, 1, 0);
|
|
|
|
|
progressBar.Refresh();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
2025-08-20 17:55:22 +08:00
|
|
|
|
if (!Program.Instance.needLoad)
|
|
|
|
|
return;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
Base.Setting.Instance.Init();
|
|
|
|
|
#if !DEBUG
|
2025-08-20 17:55:22 +08:00
|
|
|
|
duration = Base.Setting.Instance.CurrentSettings.progressStepDuration;
|
|
|
|
|
fadeDuration = Base.Setting.Instance.CurrentSettings.exitAnimationDuration;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
#endif
|
2025-08-20 17:55:22 +08:00
|
|
|
|
Load();
|
|
|
|
|
Program.Instance.needLoad = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Load()
|
|
|
|
|
{
|
2025-08-19 20:22:10 +08:00
|
|
|
|
loadingUI.SetActive(true);
|
|
|
|
|
textColor = describeText.color;
|
|
|
|
|
StartCoroutine(LoadAllManagers());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator LoadAllManagers()
|
|
|
|
|
{
|
|
|
|
|
for (var i = 0; i < _loadingSteps.Length; i++)
|
|
|
|
|
{
|
2025-08-20 17:55:22 +08:00
|
|
|
|
// 更新描述文本(放在try外部)
|
2025-08-19 20:22:10 +08:00
|
|
|
|
describeText.text = _loadingSteps[i];
|
|
|
|
|
|
|
|
|
|
// 获取当前阶段的目标进度
|
|
|
|
|
var targetProgress = (float)(i + 1) / _loadingSteps.Length;
|
|
|
|
|
|
2025-08-20 17:55:22 +08:00
|
|
|
|
// 平滑过渡到下一个阶段(放在try外部)
|
2025-08-19 20:22:10 +08:00
|
|
|
|
yield return SmoothTransitionTo(targetProgress);
|
|
|
|
|
|
2025-08-20 17:55:22 +08:00
|
|
|
|
// 初始化对应的管理器(使用单独的方法处理可能抛出的异常)
|
|
|
|
|
yield return InitializeManagerSafely(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 加载完成后的处理
|
|
|
|
|
describeText.text = "加载完成!";
|
|
|
|
|
Progress = 1f;
|
|
|
|
|
|
|
|
|
|
// 开始渐隐效果
|
|
|
|
|
yield return FadeOutProgressBar();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator InitializeManagerSafely(int stepIndex)
|
|
|
|
|
{
|
|
|
|
|
bool initSuccess = false;
|
|
|
|
|
System.Exception initException = null;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
switch (stepIndex)
|
2025-08-19 20:22:10 +08:00
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
UnityLogger.Init();
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
Managers.DefineManager.Instance.Init();
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
Managers.PackagesImageManager.Instance.Init();
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
Managers.TileManager.Instance.Init();
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
|
|
|
|
Managers.AffiliationManager.Instance.Init();
|
|
|
|
|
break;
|
|
|
|
|
case 5:
|
|
|
|
|
Managers.ItemResourceManager.Instance.Init();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-08-20 17:55:22 +08:00
|
|
|
|
initSuccess = true;
|
|
|
|
|
}
|
|
|
|
|
catch (System.Exception ex)
|
|
|
|
|
{
|
|
|
|
|
initException = ex;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 17:55:22 +08:00
|
|
|
|
if (!initSuccess && initException != null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError($"初始化第 {stepIndex + 1} 个管理器时出错: {initException.Message}\n{initException.StackTrace}");
|
|
|
|
|
describeText.text = $"{_loadingSteps[stepIndex]} (初始化失败)";
|
|
|
|
|
|
|
|
|
|
// 这里可以添加重试逻辑
|
|
|
|
|
// yield return RetryInitialize(stepIndex);
|
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
|
2025-08-20 17:55:22 +08:00
|
|
|
|
// 确保协程继续执行
|
|
|
|
|
yield return null;
|
2025-08-19 20:22:10 +08:00
|
|
|
|
}
|
|
|
|
|
private IEnumerator SmoothTransitionTo(float targetProgress)
|
|
|
|
|
{
|
|
|
|
|
var startProgress = _currentProgress;
|
|
|
|
|
var elapsedTime = 0f;
|
|
|
|
|
|
|
|
|
|
while (elapsedTime < duration)
|
|
|
|
|
{
|
|
|
|
|
elapsedTime += Time.deltaTime;
|
|
|
|
|
var t = Mathf.SmoothStep(0f, 1f, elapsedTime / duration); // 使用 SmoothStep 实现平滑过渡
|
|
|
|
|
Progress = Mathf.Lerp(startProgress, targetProgress, t);
|
|
|
|
|
yield return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 确保最终进度达到目标值
|
|
|
|
|
Progress = targetProgress;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator FadeOutProgressBar()
|
|
|
|
|
{
|
|
|
|
|
var elapsedTime = 0f;
|
|
|
|
|
|
|
|
|
|
while (elapsedTime < fadeDuration)
|
|
|
|
|
{
|
|
|
|
|
elapsedTime += Time.deltaTime;
|
|
|
|
|
var t = Mathf.SmoothStep(0f, 1f, elapsedTime / fadeDuration); // 使用 SmoothStep 实现平滑渐隐
|
|
|
|
|
Opacity = 1f - t; // 不透明度从 1 到 0
|
|
|
|
|
yield return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 确保最终不透明度为 0
|
|
|
|
|
Opacity = 0f;
|
|
|
|
|
loadingUI.SetActive(false);
|
|
|
|
|
}
|
2025-08-20 17:55:22 +08:00
|
|
|
|
|
|
|
|
|
public static void ToScene(string scene)
|
|
|
|
|
{
|
|
|
|
|
SceneManager.LoadScene(scene);
|
|
|
|
|
}
|
2025-08-19 20:22:10 +08:00
|
|
|
|
}
|
|
|
|
|
}
|