Files
5.24BuYu/Assets/MyGame/Scripts/fishResources/FishResourcesData.cs
T
2026-05-01 06:26:50 +08:00

246 lines
8.8 KiB
C#

using Newtonsoft.Json;
using Spine.Unity;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using static UnityEngine.AddressableAssets.Addressables;
[Serializable]
public class FishResourcesData
{
public int fishNumber;
public string prefabFishResourcesPath;
public string dieEffectAssets;
/// <summary>
/// 出场音频
/// </summary>
public List<int> showAudio;
/// <summary>
/// 移动音频
/// </summary>
public List<int> moveAudio;
/// <summary>
/// 死亡音效 1,2,3
/// </summary>
public List<int> dieAudio;
/// <summary>
/// 爆奖音效
/// </summary>
public Dictionary<string, int> dieGlodAduio;
const string prefabFish_AALabel="FishPrefab";
const string dieSpine_AALabel = "DieSpine";
const string Sprite_AALabel = "TuJian";
private static Dictionary<string,int> FishResourcesPrefabsDic ;//prefabpath--id
private static Dictionary<string,int> FishResourcesSpineDieDic ;//dieSpinepath--id
private static Dictionary<string,int> FishResourcesSpriteDic;//Spritepath--id
static FishResourcesData()
{
Init();
}
async static void Init()
{
string prefabFishPath = Resources.Load<TextAsset>("tbfishresources").text;
var v = JsonConvert.DeserializeObject<List<FishResourcesData>>(prefabFishPath);
FishResourcesPrefabsDic = new Dictionary<string, int>();
fishResourcesDatasDic = new Dictionary<int, FishResourcesData>();
FishResourcesSpineDieDic = new Dictionary<string, int>();
FishResourcesSpriteDic = new Dictionary<string, int>();
foreach (var item in v)
{
if (item.prefabFishResourcesPath != null && item.prefabFishResourcesPath != "")
{
Debug.Log($"添加鱼资源数据: {item.fishNumber} - {item.prefabFishResourcesPath}");
FishResourcesPrefabsDic.Add(item.prefabFishResourcesPath, item.fishNumber);
FishResourcesSpriteDic.Add(item.prefabFishResourcesPath, item.fishNumber);
if (item.dieEffectAssets != null && item.dieEffectAssets != "")
{
FishResourcesSpineDieDic.Add(item.dieEffectAssets, item.fishNumber);
}
fishResourcesDatasDic.Add(item.fishNumber, item);
}
}
}
private static Dictionary<int, GameObject> fishPrefabDic;//id--prefabs
public static Dictionary<int, FishResourcesData> fishResourcesDatasDic;//id--FishResourcesData
public static Dictionary<int, Sprite> fishResourcesSpritesDic;//id--Sprite
private static Dictionary<int, SkeletonDataAsset> diePrefabDic;//id--SkeletonDataAsset
public static async Task LoadFishAssetsData()
{
// 清空对象池,避免残留
Pool.Instance.Clear();
fishPrefabDic = new Dictionary<int, GameObject>();
diePrefabDic = new Dictionary<int, SkeletonDataAsset>();
fishResourcesSpritesDic = new Dictionary<int, Sprite>();
// ---------------- Prefab ----------------
var keys = new List<string> { prefabFish_AALabel, GameManager.Instance.gameData.currGameType.ToString() };
var prefabHandle = Addressables.LoadAssetsAsync<GameObject>(keys, prefab =>
{
if (FishResourcesPrefabsDic.TryGetValue(prefab.name, out int id))
{
// 防御性检查,避免重复覆盖
if (fishPrefabDic.ContainsKey(id))
Debug.LogWarning($"Prefab重复加载,覆盖ID={id}, 名称={prefab.name}");
fishPrefabDic[id] = prefab;
// 防御性检查组件
var mc = prefab.GetComponent<MonsterContral>();
if (mc != null)
mc.fishTypeID = id;
// 加入对象池
Pool.Instance.dic[id] = new PoolData(prefab, CreateFishManager.Instance.poolRoot.gameObject, id);
}
}, MergeMode.Intersection);
await prefabHandle.Task;
Addressables.Release(prefabHandle);
Debug.Log($"加载完成,共 {fishPrefabDic.Count} 个鱼Prefab");
// ---------------- 死亡 Spine 动效 ----------------
keys = new List<string> { dieSpine_AALabel, GameManager.Instance.gameData.currGameType.ToString() };
var dieHandle = Addressables.LoadAssetsAsync<SkeletonDataAsset>(keys, data =>
{
if (FishResourcesSpineDieDic.TryGetValue(data.name, out int id))
{
if (diePrefabDic.ContainsKey(id))
Debug.LogWarning($"死亡动效重复加载,覆盖ID={id}, 名称={data.name}");
diePrefabDic[id] = data;
}
}, MergeMode.Intersection);
await dieHandle.Task;
Addressables.Release(dieHandle);
Debug.Log($"加载完成,共 {diePrefabDic.Count} 个鱼死亡动效");
// ---------------- 鱼 Sprite ----------------
keys = new List<string> { Sprite_AALabel, GameManager.Instance.gameData.currGameType.ToString() };
var spriteHandle = Addressables.LoadAssetsAsync<Sprite>(keys, data =>
{
if (FishResourcesSpriteDic.TryGetValue(data.name, out int id))
{
if (fishResourcesSpritesDic.ContainsKey(id))
Debug.LogWarning($"Sprite重复加载,覆盖ID={id}, 名称={data.name}");
fishResourcesSpritesDic[id] = data;
}
}, MergeMode.Intersection);
await spriteHandle.Task;
Addressables.Release(spriteHandle);
Debug.Log($"加载完成,共 {fishResourcesSpritesDic.Count} 个鱼Sprite");
}
public static async Task UnLoadFishAssetsData()
{
Pool.Instance.Clear();
// 卸载 Prefab
var keys = new List<string> { prefabFish_AALabel, GameManager.Instance.gameData.currGameType.ToString() };
var prefabHandle = Addressables.LoadAssetsAsync<GameObject>(keys, null, MergeMode.Intersection);
await prefabHandle.Task;
if (prefabHandle.Result != null)
{
foreach (var prefab in prefabHandle.Result)
{
if (FishResourcesPrefabsDic.TryGetValue(prefab.name, out int id))
{
if (fishPrefabDic.Remove(id))
{
Debug.Log($"卸载鱼Prefab: {prefab.name} (ID={id})");
}
}
}
}
Addressables.Release(prefabHandle);
// 卸载死亡 Spine 动效
keys = new List<string> { dieSpine_AALabel, GameManager.Instance.gameData.currGameType.ToString() };
var dieHandle = Addressables.LoadAssetsAsync<SkeletonDataAsset>(keys, null, MergeMode.Intersection);
await dieHandle.Task;
if (dieHandle.Result != null)
{
foreach (var data in dieHandle.Result)
{
if (FishResourcesSpineDieDic.TryGetValue(data.name, out int id))
{
if (diePrefabDic.Remove(id))
{
Debug.Log($"卸载鱼死亡动效: {data.name} (ID={id})");
}
}
}
}
Addressables.Release(dieHandle);
// 卸载鱼 Sprite
keys = new List<string> { Sprite_AALabel, GameManager.Instance.gameData.currGameType.ToString() };
var spriteHandle = Addressables.LoadAssetsAsync<Sprite>(keys, null, MergeMode.Intersection);
await spriteHandle.Task;
if (spriteHandle.Result != null)
{
foreach (var sprite in spriteHandle.Result)
{
if (FishResourcesSpriteDic.TryGetValue(sprite.name, out int id))
{
if (fishResourcesSpritesDic.Remove(id))
{
Debug.Log($"卸载鱼Sprite: {sprite.name} (ID={id})");
}
}
}
}
Addressables.Release(spriteHandle);
Debug.Log($"卸载完成,剩余Prefab数量: {fishPrefabDic.Count}, 死亡动效数量: {diePrefabDic.Count}, Sprite数量: {fishResourcesSpritesDic.Count}");
}
public static GameObject GetFishPrefab(int fishNumber)
{
if (fishPrefabDic != null && fishPrefabDic.ContainsKey(fishNumber))
{
return fishPrefabDic[fishNumber];
}
else
{
Debug.LogError($"鱼Prefab未找到: {fishNumber}");
return null;
}
}
public static SkeletonDataAsset GetFishDieSpine(int fishNumber)
{
if (diePrefabDic != null && diePrefabDic.ContainsKey(fishNumber))
{
return diePrefabDic[fishNumber];
}
else
{
Debug.LogError($"鱼死亡动效未找到: {fishNumber}");
return null;
}
}
}