hch
2025-12-03 c2c98d54516b45118bd4061317b3e291539119c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System.Collections.Generic;
using UnityEngine;
 
/// <summary>
/// 队伍资源追踪器
/// 分析队伍需要哪些资源(Spine动画、音频)
/// </summary>
public class TeamResTracker
{
    /// <summary>
    /// 队伍资源信息
    /// </summary>
    public class TeamResourceInfo
    {
        public List<BattleResCache.ResourceIdentifier> SpineResources = new List<BattleResCache.ResourceIdentifier>();
        public List<BattleResCache.ResourceIdentifier> AudioResources = new List<BattleResCache.ResourceIdentifier>();
        
        public void Clear()
        {
            SpineResources.Clear();
            AudioResources.Clear();
        }
        
        /// <summary>
        /// 获取所有资源数量
        /// </summary>
        public int GetTotalCount()
        {
            return SpineResources.Count + AudioResources.Count;
        }
    }
    
    /// <summary>
    /// 分析单个队伍的资源需求
    /// </summary>
    public static TeamResourceInfo AnalyzeTeam(TeamBase team, bool isPersistent)
    {
        TeamResourceInfo info = new TeamResourceInfo();
        
        if (team == null || team.serverHeroes == null)
        {
            return info;
        }
        
        foreach (var teamHero in team.serverHeroes)
        {
            if (teamHero == null)
                continue;
            
            // 生成角色唯一标识(用于引用追踪)
            string ownerId = $"{teamHero.heroId}_{teamHero.SkinID}";
            
            // ===== 移除:不再预加载角色Spine资源 =====
            // AddHeroSpineResource(teamHero, info, isPersistent, ownerId);
            
            // 只预加载技能相关资源(音效、特效Spine)
            AddSkillResources(teamHero, info, isPersistent, ownerId);
        }
        
        return info;
    }
    
    /// <summary>
    /// 添加技能相关资源
    /// </summary>
    private static void AddSkillResources(TeamHero teamHero, TeamResourceInfo info, bool isPersistent, string ownerId)
    {
        if (teamHero.heroConfig == null)
            return;
        
        // 普攻技能
        if (teamHero.heroConfig.AtkSkillID > 0)
        {
            AddSingleSkillResources(teamHero.heroConfig.AtkSkillID, info, isPersistent, ownerId);
        }
        
        // 怒气技能
        if (teamHero.heroConfig.AngerSkillID > 0)
        {
            AddSingleSkillResources(teamHero.heroConfig.AngerSkillID, info, isPersistent, ownerId);
        }
    }
    
    /// <summary>
    /// 添加单个技能的资源
    /// </summary>
    private static void AddSingleSkillResources(int skillId, TeamResourceInfo info, bool isPersistent, string ownerId)
    {
        SkillConfig skillConfig = SkillConfig.Get(skillId);
        if (skillConfig == null)
            return;
        
        AddSkillAudio(skillConfig, info, isPersistent, ownerId);
        AddSkillEffects(skillConfig, info, isPersistent, ownerId);
    }
    
    private static void AddSkillAudio(SkillConfig skillConfig, TeamResourceInfo info, bool isPersistent, string ownerId)
    {
        if (skillConfig.SkinllSFX1 > 0)
        {
            AddAudioResource(skillConfig.SkinllSFX1, info, isPersistent, ownerId);
        }
        
        if (skillConfig.SkinllSFX2 > 0)
        {
            AddAudioResource(skillConfig.SkinllSFX2, info, isPersistent, ownerId);
        }
    }
    
    private static void AddSkillEffects(SkillConfig skillConfig, TeamResourceInfo info, bool isPersistent, string ownerId)
    {
        List<int> effectIds = new List<int>();
        
        if (skillConfig.BulletEffectId > 0) effectIds.Add(skillConfig.BulletEffectId);
        if (skillConfig.ExplosionEffectId > 0) effectIds.Add(skillConfig.ExplosionEffectId);
        if (skillConfig.ExplosionEffect2 > 0) effectIds.Add(skillConfig.ExplosionEffect2);
        if (skillConfig.ExplosionEffect3 > 0) effectIds.Add(skillConfig.ExplosionEffect3);
        if (skillConfig.ExplosionEffect4 > 0) effectIds.Add(skillConfig.ExplosionEffect4);
        if (skillConfig.EffectId > 0) effectIds.Add(skillConfig.EffectId);
        if (skillConfig.EffectId2 > 0) effectIds.Add(skillConfig.EffectId2);
        if (skillConfig.MStartEffectId > 0) effectIds.Add(skillConfig.MStartEffectId);
        if (skillConfig.BuffEffect > 0) effectIds.Add(skillConfig.BuffEffect);
        if (skillConfig.TriggerEffect > 0) effectIds.Add(skillConfig.TriggerEffect);
        
        foreach (int effectId in effectIds)
        {
            EffectConfig effectConfig = EffectConfig.Get(effectId);
            if (effectConfig == null)
                continue;
                
            // 特效Spine资源(只预加载特效Spine,不预加载角色Spine)
            if (effectConfig.isSpine > 0 && !string.IsNullOrEmpty(effectConfig.fxName))
            {
                var identifier = new BattleResCache.ResourceIdentifier
                {
                    Directory = "UIEffect/" + effectConfig.packageName,
                    AssetName = effectConfig.fxName,
                    Type = BattleResCache.ResourceType.Spine,
                    IsPersistent = isPersistent,
                    OwnerId = ownerId  // ← 添加所有者ID
                };
                
                if (!ContainsResource(info.SpineResources, identifier))
                {
                    info.SpineResources.Add(identifier);
                }
            }
            
            // 特效音频
            if (effectConfig.audio > 0)
            {
                AddAudioResource(effectConfig.audio, info, isPersistent, ownerId);
            }
        }
    }
    
    private static void AddAudioResource(int audioId, TeamResourceInfo info, bool isPersistent, string ownerId)
    {
        AudioConfig audioConfig = AudioConfig.Get(audioId);
        if (audioConfig == null)
            return;
        
        var identifier = new BattleResCache.ResourceIdentifier
        {
            Directory = "Audio/" + audioConfig.Folder,  // ← 修复:添加 Audio/ 前缀
            AssetName = audioConfig.Audio,
            Type = BattleResCache.ResourceType.Audio,
            IsPersistent = isPersistent,
            OwnerId = ownerId
        };
        
        if (!ContainsResource(info.AudioResources, identifier))
        {
            info.AudioResources.Add(identifier);
        }
    }
    
    /// <summary>
    /// 检查资源列表是否包含指定资源
    /// </summary>
    private static bool ContainsResource(List<BattleResCache.ResourceIdentifier> list, BattleResCache.ResourceIdentifier resource)
    {
        foreach (var item in list)
        {
            if (item.GetKey() == resource.GetKey())
            {
                return true;
            }
        }
        return false;
    }
}