using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
namespace TcgEngine.UI
{
///
/// 启动
///
public class SplashScreenManager : MonoBehaviour
{
[Header("UI组件")]
public Text statusText;
public Slider progressBar;
public Text progressText;
public Image logoImage;
public CanvasGroup mainCanvasGroup;
[Header("设置")]
public string nextSceneName = "LoginMenu";
public float fadeInDuration = 1f;
public float fadeOutDuration = 1f;
public float minimumDisplayTime = 3f;
[Header("音效")]
public AudioClip splashMusic;
private float startTime;
private void Start()
{
startTime = Time.time;
InitializeUI();
StartCoroutine(RunSplashSequence());
}
private void InitializeUI()
{
// 设置初始状态
if (mainCanvasGroup != null)
mainCanvasGroup.alpha = 0f;
UpdateStatus("启动中...");
UpdateProgress(0f);
// 播放音乐
if (splashMusic != null)
{
AudioTool.Get().PlayMusic("splash", splashMusic, 0.6f);
}
}
private IEnumerator RunSplashSequence()
{
// 淡入
yield return StartCoroutine(FadeIn());
// 获取版本信息
string version = GetCurrentVersion();
// 显示版本信息
UpdateStatus($"版本 {version}");
UpdateProgress(0.1f);
yield return new WaitForSeconds(0.5f);
// 开始资源初始化
yield return StartCoroutine(InitializeResources());
// 确保最少显示时间
yield return StartCoroutine(WaitForMinimumTime());
// 完成并跳转
yield return StartCoroutine(CompleteAndTransition());
}
private IEnumerator FadeIn()
{
if (mainCanvasGroup == null) yield break;
float elapsed = 0f;
while (elapsed < fadeInDuration)
{
elapsed += Time.deltaTime;
mainCanvasGroup.alpha = Mathf.Lerp(0f, 1f, elapsed / fadeInDuration);
yield return null;
}
mainCanvasGroup.alpha = 1f;
}
private IEnumerator InitializeResources()
{
UpdateStatus("正在初始化资源系统...");
UpdateProgress(0.2f);
yield return new WaitForSeconds(0.3f);
// 直接在主线程中调用ResourceDownloader
ResourceDownloader downloader = null;
try
{
downloader = ResourceDownloader.Get();
// 订阅事件以获取进度更新
downloader.OnDownloadProgress += OnDownloadProgress;
downloader.OnExtractProgress += OnExtractProgress;
downloader.OnDownloadComplete += OnDownloadComplete;
downloader.OnExtractComplete += OnExtractComplete;
downloader.OnDownloadError += OnDownloadError;
UpdateStatus("正在检查资源版本...");
UpdateProgress(0.3f);
}
catch (System.Exception e)
{
Debug.LogError($"[SplashScreenManager] 资源初始化异常: {e.Message}");
UpdateStatus("资源初始化失败,使用现有资源");
UpdateProgress(0.9f);
yield break;
}
// 使用协程包装异步调用
yield return StartCoroutine(CheckAndDownloadResources(downloader));
// 取消订阅事件
if (downloader != null)
{
downloader.OnDownloadProgress -= OnDownloadProgress;
downloader.OnExtractProgress -= OnExtractProgress;
downloader.OnDownloadComplete -= OnDownloadComplete;
downloader.OnExtractComplete -= OnExtractComplete;
downloader.OnDownloadError -= OnDownloadError;
}
UpdateStatus("初始化完成!");
UpdateProgress(1f);
}
private IEnumerator CheckAndDownloadResources(ResourceDownloader downloader)
{
bool needsUpdate = false;
bool taskCompleted = false;
// 异步检查更新
downloader.CheckForUpdates().ContinueWith(task =>
{
needsUpdate = task.Result;
taskCompleted = true;
});
// 等待任务完成
while (!taskCompleted)
{
yield return null;
}
if (!needsUpdate)
{
UpdateStatus("资源已是最新版本");
UpdateProgress(0.9f);
yield return new WaitForSeconds(0.5f);
yield break;
}
UpdateStatus("发现新版本,开始下载...");
UpdateProgress(0.4f);
// 异步下载资源
bool downloadSuccess = false;
taskCompleted = false;
downloader.DownloadAndUpdateResources().ContinueWith(task =>
{
downloadSuccess = task.Result;
taskCompleted = true;
});
// 等待下载完成
while (!taskCompleted)
{
yield return null;
}
if (downloadSuccess)
{
UpdateStatus("资源更新完成!");
}
else
{
UpdateStatus("下载失败,使用现有资源");
}
UpdateProgress(0.95f);
}
private void OnDownloadProgress(float progress)
{
float baseProgress = 0.4f;
float range = 0.3f;
UpdateProgress(baseProgress + progress * range);
UpdateStatus($"正在下载资源 {(progress * 100):F0}%");
}
private void OnDownloadComplete(string filePath)
{
UpdateStatus("下载完成,正在解压...");
UpdateProgress(0.7f);
}
private void OnDownloadError(string error)
{
UpdateStatus("下载出错,使用现有资源");
Debug.LogWarning($"[SplashScreenManager] 下载错误: {error}");
}
private void OnExtractProgress(float progress)
{
float baseProgress = 0.7f;
float range = 0.2f;
UpdateProgress(baseProgress + progress * range);
UpdateStatus($"正在解压资源 {(progress * 100):F0}%");
}
private void OnExtractComplete()
{
UpdateStatus("解压完成!");
UpdateProgress(0.9f);
}
private IEnumerator WaitForMinimumTime()
{
float elapsedTime = Time.time - startTime;
float remainingTime = minimumDisplayTime - elapsedTime;
if (remainingTime > 0)
{
UpdateStatus("准备完成...");
yield return new WaitForSeconds(remainingTime);
}
}
private IEnumerator CompleteAndTransition()
{
UpdateStatus("准备进入游戏...");
// 淡出音乐
if (splashMusic != null)
{
AudioTool.Get().FadeOutMusic("splash");
}
// 淡出界面
if (mainCanvasGroup != null)
{
float elapsed = 0f;
while (elapsed < fadeOutDuration)
{
elapsed += Time.deltaTime;
mainCanvasGroup.alpha = Mathf.Lerp(1f, 0f, elapsed / fadeOutDuration);
yield return null;
}
}
// 显示黑屏并跳转
var blackPanel = TcgEngine.UI.BlackPanel.Get();
if (blackPanel != null)
{
blackPanel.Show();
yield return new WaitForSeconds(0.3f);
}
else
{
Debug.LogWarning("[SplashScreenManager] BlackPanel not found, skipping fade effect");
yield return new WaitForSeconds(0.1f);
}
// 跳转到下一个场景
SceneManager.LoadScene(nextSceneName);
}
private void UpdateStatus(string message)
{
if (statusText != null)
statusText.text = message;
Debug.Log($"[SplashScreen] {message}");
}
private void UpdateProgress(float progress)
{
progress = Mathf.Clamp01(progress);
if (progressBar != null)
progressBar.value = progress;
if (progressText != null)
progressText.text = $"{(progress * 100):F0}%";
}
///
/// 获取当前版本信息
///
private string GetCurrentVersion()
{
try
{
string versionFile = System.IO.Path.Combine(Application.persistentDataPath, "version.txt");
if (System.IO.File.Exists(versionFile))
{
string[] lines = System.IO.File.ReadAllLines(versionFile);
if (lines.Length > 0 && !string.IsNullOrEmpty(lines[0].Trim()))
{
return lines[0].Trim();
}
}
var downloader = ResourceDownloader.Get();
if (downloader != null)
{
string resourceVersion = downloader.GetCurrentVersion();
if (!string.IsNullOrEmpty(resourceVersion) && resourceVersion != "0.0.0")
{
return resourceVersion;
}
}
return Application.version;
}
catch (System.Exception e)
{
Debug.LogWarning($"[SplashScreenManager] 获取版本信息失败: {e.Message}");
return Application.version;
}
}
}
}