yyl
2025-12-03 b1b9a387a63e1ce77cf387b90ec170c7a05bb53b
Main/System/Battle/BattleResources/BattlePreloadManager.cs
@@ -1,11 +1,10 @@
using UnityEngine;
using System;
using System.Collections.Generic;
using Spine.Unity;
public class BattlePreloadManager
{
    private BattleSpineResLoader spineLoader = new BattleSpineResLoader();
    private BattleAudioResLoader audioLoader = new BattleAudioResLoader();
    private BattleCacheManager cacheManager = new BattleCacheManager();
    private BattleUnloadManager unloadManager = new BattleUnloadManager();
    
@@ -31,10 +30,18 @@
        var redTeamInfo = AnalyzeTeamList(redTeamList, true);
        var blueTeamInfo = AnalyzeTeamList(blueTeamList, false);
        
        // ===== 新增:注册战场红队资源需求 =====
        cacheManager.RegisterBattlefieldRedTeam(battleGuid, redTeamInfo.SpineResources, redTeamInfo.AudioResources);
        // ===== 合并红蓝队资源,统一注册 =====
        var allSpineResources = new List<BattleResCache.ResourceIdentifier>();
        var allAudioResources = new List<BattleResCache.ResourceIdentifier>();
        
        StartPreload(redTeamInfo, blueTeamInfo, battleGuid, progressCallback, () =>
        allSpineResources.AddRange(redTeamInfo.SpineResources);
        allSpineResources.AddRange(blueTeamInfo.SpineResources);
        allAudioResources.AddRange(redTeamInfo.AudioResources);
        allAudioResources.AddRange(blueTeamInfo.AudioResources);
        cacheManager.RegisterBattlefieldResources(battleGuid, allSpineResources, allAudioResources);
        StartPreload(allSpineResources, allAudioResources, battleGuid, progressCallback, () =>
        {
            isLoading = false;
            completeCallback?.Invoke();
@@ -95,11 +102,12 @@
        return false;
    }
    
    private void StartPreload(TeamResTracker.TeamResourceInfo redInfo, TeamResTracker.TeamResourceInfo blueInfo,
    private void StartPreload(List<BattleResCache.ResourceIdentifier> spineResources,
        List<BattleResCache.ResourceIdentifier> audioResources,
        string battleGuid,
        Action<float> progressCallback, Action completeCallback)
    {
        int totalResources = redInfo.GetTotalCount() + blueInfo.GetTotalCount();
        int totalResources = spineResources.Count + audioResources.Count;
        
        if (totalResources == 0)
        {
@@ -109,60 +117,90 @@
        }
        
        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}");
        Debug.Log($"  Spine={spineResources.Count}, Audio={audioResources.Count}");
        
        int completedPhases = 0;
        int totalPhases = 4;
        int loadedCount = 0;
        
        Action onPhaseComplete = () =>
        Action onSingleComplete = () =>
        {
            completedPhases++;
            float progress = (float)completedPhases / totalPhases;
            loadedCount++;
            float progress = (float)loadedCount / totalResources;
            progressCallback?.Invoke(progress);
            
            if (completedPhases >= totalPhases)
            if (loadedCount >= totalResources)
            {
                Debug.Log($"BattlePreloadManager: Completed! {cacheManager.GetCacheStats(battleGuid)}");
                completeCallback?.Invoke();
            }
        };
        
        // 并行加载4个阶段(传入cacheManager和是否为红队标识)
        spineLoader.LoadSpineResourcesAsync(
            redInfo.SpineResources,
            cacheManager.GetSpineCache(true, battleGuid),
            null,
            onPhaseComplete,
            cacheManager,  // ← 传入管理器
            true           // ← 是红队
        );
        // 异步加载所有Spine资源
        foreach (var identifier in spineResources)
        {
            LoadSpineAsync(identifier, battleGuid, onSingleComplete);
        }
        
        audioLoader.LoadAudioResourcesAsync(
            redInfo.AudioResources,
            cacheManager.GetAudioCache(true, battleGuid),
            null,
            onPhaseComplete,
            cacheManager,  // ← 传入管理器
            true           // ← 是红队
        );
        // 异步加载所有Audio资源
        foreach (var identifier in audioResources)
        {
            LoadAudioAsync(identifier, battleGuid, onSingleComplete);
        }
    }
        
        spineLoader.LoadSpineResourcesAsync(
            blueInfo.SpineResources,
            cacheManager.GetSpineCache(false, battleGuid),
            null,
            onPhaseComplete,
            null,   // ← 蓝队不需要引用追踪
            false   // ← 不是红队
        );
    private void LoadSpineAsync(BattleResCache.ResourceIdentifier identifier, string battleGuid, Action onComplete)
    {
        string key = identifier.GetKey();
        
        audioLoader.LoadAudioResourcesAsync(
            blueInfo.AudioResources,
            cacheManager.GetAudioCache(false, battleGuid),
            null,
            onPhaseComplete,
            null,   // ← 蓝队不需要引用追踪
            false   // ← 不是红队
        ResManager.Instance.LoadAssetAsync<SkeletonDataAsset>(
            identifier.Directory,
            identifier.AssetName,
            (success, asset) =>
            {
                if (success && asset != null)
                {
                    var skeletonData = asset as SkeletonDataAsset;
                    if (skeletonData != null)
                    {
                        var cachedRes = new BattleResCache.CachedResource(identifier, skeletonData, false);
                        cacheManager.UpdateResourceReference(key, cachedRes, battleGuid, identifier.OwnerId);
                        Debug.Log($"BattlePreloadManager: Loaded spine: {key}");
                    }
                }
                else
                {
                    Debug.LogError($"BattlePreloadManager: Failed to load spine: {key}");
                }
                onComplete?.Invoke();
            }
        );
    }
    private void LoadAudioAsync(BattleResCache.ResourceIdentifier identifier, string battleGuid, Action onComplete)
    {
        string key = identifier.GetKey();
        ResManager.Instance.LoadAssetAsync<AudioClip>(
            identifier.Directory,
            identifier.AssetName,
            (success, asset) =>
            {
                if (success && asset != null)
                {
                    var audioClip = asset as AudioClip;
                    if (audioClip != null)
                    {
                        var cachedRes = new BattleResCache.CachedResource(identifier, audioClip, false);
                        cacheManager.UpdateResourceReference(key, cachedRes, battleGuid, identifier.OwnerId);
                        Debug.Log($"BattlePreloadManager: Loaded audio: {key}");
                    }
                }
                else
                {
                    Debug.LogError($"BattlePreloadManager: Failed to load audio: {key}");
                }
                onComplete?.Invoke();
            },
            false  // needExt = false
        );
    }
}