using UnityEngine;
|
using System;
|
using System.Collections.Generic;
|
|
public class BattlePreloadManager
|
{
|
private BattleSpineResLoader spineLoader = new BattleSpineResLoader();
|
private BattleAudioResLoader audioLoader = new BattleAudioResLoader();
|
private BattleCacheManager cacheManager = new BattleCacheManager();
|
private BattleUnloadManager unloadManager = new BattleUnloadManager();
|
|
private bool isLoading = false;
|
|
public BattleCacheManager CacheManager => cacheManager;
|
public BattleUnloadManager UnloadManager => unloadManager;
|
|
/// <summary>
|
/// 预加载战斗资源
|
/// </summary>
|
public void PreloadBattleResources(string battleGuid, List<TeamBase> redTeamList, List<TeamBase> blueTeamList,
|
Action<float> progressCallback, Action completeCallback)
|
{
|
if (isLoading)
|
{
|
Debug.LogWarning("BattlePreloadManager: Already loading, ignoring request");
|
return;
|
}
|
|
isLoading = true;
|
|
var redTeamInfo = AnalyzeTeamList(redTeamList, true);
|
var blueTeamInfo = AnalyzeTeamList(blueTeamList, false);
|
|
StartPreload(redTeamInfo, blueTeamInfo, battleGuid, progressCallback, () =>
|
{
|
isLoading = false;
|
completeCallback?.Invoke();
|
});
|
}
|
|
private TeamResTracker.TeamResourceInfo AnalyzeTeamList(List<TeamBase> teamList, bool isPersistent)
|
{
|
var combinedInfo = new TeamResTracker.TeamResourceInfo();
|
|
if (teamList == null || teamList.Count == 0)
|
{
|
return combinedInfo;
|
}
|
|
foreach (var team in teamList)
|
{
|
if (team == null)
|
continue;
|
|
var teamInfo = TeamResTracker.AnalyzeTeam(team, isPersistent);
|
MergeResourceInfo(combinedInfo, teamInfo);
|
}
|
|
return combinedInfo;
|
}
|
|
private void MergeResourceInfo(TeamResTracker.TeamResourceInfo target, TeamResTracker.TeamResourceInfo source)
|
{
|
// 合并Spine资源(去重)
|
foreach (var res in source.SpineResources)
|
{
|
if (!ContainsResource(target.SpineResources, res))
|
{
|
target.SpineResources.Add(res);
|
}
|
}
|
|
// 合并音频资源(去重)
|
foreach (var res in source.AudioResources)
|
{
|
if (!ContainsResource(target.AudioResources, res))
|
{
|
target.AudioResources.Add(res);
|
}
|
}
|
}
|
|
private bool ContainsResource(List<BattleResCache.ResourceIdentifier> list, BattleResCache.ResourceIdentifier resource)
|
{
|
foreach (var item in list)
|
{
|
if (item.GetKey() == resource.GetKey())
|
{
|
return true;
|
}
|
}
|
return false;
|
}
|
|
private void StartPreload(TeamResTracker.TeamResourceInfo redInfo, TeamResTracker.TeamResourceInfo blueInfo,
|
string battleGuid,
|
Action<float> progressCallback, Action completeCallback)
|
{
|
int totalResources = redInfo.GetTotalCount() + blueInfo.GetTotalCount();
|
|
if (totalResources == 0)
|
{
|
Debug.Log("BattlePreloadManager: No resources to preload");
|
completeCallback?.Invoke();
|
return;
|
}
|
|
Debug.Log($"BattlePreloadManager: Preloading {totalResources} resources for battlefield {battleGuid}");
|
Debug.Log($" Red: Spine={redInfo.SpineResources.Count}, Audio={redInfo.AudioResources.Count}");
|
Debug.Log($" Blue: Spine={blueInfo.SpineResources.Count}, Audio={blueInfo.AudioResources.Count}");
|
|
int completedPhases = 0;
|
int totalPhases = 4;
|
|
Action onPhaseComplete = () =>
|
{
|
completedPhases++;
|
float progress = (float)completedPhases / totalPhases;
|
progressCallback?.Invoke(progress);
|
|
if (completedPhases >= totalPhases)
|
{
|
Debug.Log($"BattlePreloadManager: Completed! {cacheManager.GetCacheStats(battleGuid)}");
|
completeCallback?.Invoke();
|
}
|
};
|
|
// 并行加载4个阶段(传入cacheManager和是否为红队标识)
|
spineLoader.LoadSpineResourcesAsync(
|
redInfo.SpineResources,
|
cacheManager.GetSpineCache(true, battleGuid),
|
null,
|
onPhaseComplete,
|
cacheManager, // ← 传入管理器
|
true // ← 是红队
|
);
|
|
audioLoader.LoadAudioResourcesAsync(
|
redInfo.AudioResources,
|
cacheManager.GetAudioCache(true, battleGuid),
|
null,
|
onPhaseComplete,
|
cacheManager, // ← 传入管理器
|
true // ← 是红队
|
);
|
|
spineLoader.LoadSpineResourcesAsync(
|
blueInfo.SpineResources,
|
cacheManager.GetSpineCache(false, battleGuid),
|
null,
|
onPhaseComplete,
|
null, // ← 蓝队不需要引用追踪
|
false // ← 不是红队
|
);
|
|
audioLoader.LoadAudioResourcesAsync(
|
blueInfo.AudioResources,
|
cacheManager.GetAudioCache(false, battleGuid),
|
null,
|
onPhaseComplete,
|
null, // ← 蓝队不需要引用追踪
|
false // ← 不是红队
|
);
|
}
|
|
/// <summary>
|
/// 处理红队变更:清空旧的,重新加载新的
|
/// </summary>
|
public void HandleRedTeamChange(List<TeamBase> newRedTeamList, Action completeCallback)
|
{
|
if (newRedTeamList == null)
|
{
|
completeCallback?.Invoke();
|
return;
|
}
|
|
Debug.Log("BattlePreloadManager: Handling red team change");
|
|
// 1. 卸载旧的红队资源
|
unloadManager.UnloadRedTeamResources(cacheManager);
|
|
// 2. 分析新红队资源
|
var newRedInfo = AnalyzeTeamList(newRedTeamList, true);
|
|
// 3. 预加载新红队资源(红队是全局的,传空字符串)
|
StartPreload(newRedInfo, new TeamResTracker.TeamResourceInfo(), "", null, completeCallback);
|
}
|
}
|