using System.Collections.Generic;
using UnityEngine;
///
/// 队伍资源追踪器
/// 分析队伍需要哪些资源(Spine动画、音频)
///
public class TeamResTracker
{
///
/// 队伍资源信息
///
public class TeamResourceInfo
{
public List SpineResources = new List();
public List AudioResources = new List();
public void Clear()
{
SpineResources.Clear();
AudioResources.Clear();
}
///
/// 获取所有资源数量
///
public int GetTotalCount()
{
return SpineResources.Count + AudioResources.Count;
}
}
///
/// 分析单个队伍的资源需求
///
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;
}
///
/// 添加技能相关资源
///
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);
}
}
///
/// 添加单个技能的资源
///
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 effectIds = new List();
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);
}
}
///
/// 检查资源列表是否包含指定资源
///
private static bool ContainsResource(List list, BattleResCache.ResourceIdentifier resource)
{
foreach (var item in list)
{
if (item.GetKey() == resource.GetKey())
{
return true;
}
}
return false;
}
}