Files
5.24BuYu/Assets/MyGame/Scripts/fishResources/FishResourcesData.cs
T

278 lines
10 KiB
C#
Raw Normal View History

2026-07-24 01:37:11 +08:00
using Newtonsoft.Json;
2026-03-27 15:18:35 +08:00
using Spine.Unity;
2026-02-03 11:35:19 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
2026-05-01 06:26:50 +08:00
using System.Threading.Tasks;
2026-02-03 11:35:19 +08:00
using UnityEngine;
using UnityEngine.AddressableAssets;
2026-03-27 15:18:35 +08:00
using static UnityEngine.AddressableAssets.Addressables;
2026-02-03 11:35:19 +08:00
[Serializable]
public class FishResourcesData
{
public int fishNumber;
public string prefabFishResourcesPath;
2026-03-27 15:18:35 +08:00
public string dieEffectAssets;
2026-04-28 01:53:41 +08:00
/// <summary>
/// 出场音频
/// </summary>
public List<int> showAudio;
/// <summary>
/// 移动音频
/// </summary>
public List<int> moveAudio;
/// <summary>
/// 死亡音效 1,2,3
/// </summary>
public List<int> dieAudio;
/// <summary>
2026-04-29 01:18:39 +08:00
/// 爆奖音效
2026-04-28 01:53:41 +08:00
/// </summary>
public Dictionary<string, int> dieGlodAduio;
2026-03-27 15:18:35 +08:00
const string prefabFish_AALabel="FishPrefab";
const string dieSpine_AALabel = "DieSpine";
2026-04-29 01:18:39 +08:00
const string Sprite_AALabel = "TuJian";
2026-02-03 11:35:19 +08:00
2026-03-27 15:18:35 +08:00
private static Dictionary<string,int> FishResourcesPrefabsDic ;//prefabpath--id
private static Dictionary<string,int> FishResourcesSpineDieDic ;//dieSpinepath--id
2026-04-29 01:18:39 +08:00
private static Dictionary<string,int> FishResourcesSpriteDic;//Spritepath--id
2026-05-01 06:26:50 +08:00
static FishResourcesData()
2026-02-03 11:35:19 +08:00
{
2026-05-01 06:26:50 +08:00
Init();
}
async static void Init()
{
2026-02-03 11:35:19 +08:00
string prefabFishPath = Resources.Load<TextAsset>("tbfishresources").text;
var v = JsonConvert.DeserializeObject<List<FishResourcesData>>(prefabFishPath);
2026-05-01 06:26:50 +08:00
FishResourcesPrefabsDic = new Dictionary<string, int>();
2026-03-27 15:18:35 +08:00
fishResourcesDatasDic = new Dictionary<int, FishResourcesData>();
FishResourcesSpineDieDic = new Dictionary<string, int>();
2026-04-29 01:18:39 +08:00
FishResourcesSpriteDic = new Dictionary<string, int>();
2026-02-03 11:35:19 +08:00
foreach (var item in v)
{
2026-05-01 06:26:50 +08:00
if (item.prefabFishResourcesPath != null && item.prefabFishResourcesPath != "")
2026-02-03 11:35:19 +08:00
{
Debug.Log($"添加鱼资源数据: {item.fishNumber} - {item.prefabFishResourcesPath}");
2026-03-27 15:18:35 +08:00
FishResourcesPrefabsDic.Add(item.prefabFishResourcesPath, item.fishNumber);
2026-04-29 01:18:39 +08:00
FishResourcesSpriteDic.Add(item.prefabFishResourcesPath, item.fishNumber);
2026-05-01 06:26:50 +08:00
if (item.dieEffectAssets != null && item.dieEffectAssets != "")
2026-03-27 15:18:35 +08:00
{
FishResourcesSpineDieDic.Add(item.dieEffectAssets, item.fishNumber);
}
fishResourcesDatasDic.Add(item.fishNumber, item);
2026-04-29 01:18:39 +08:00
2026-02-03 11:35:19 +08:00
}
}
}
2026-03-27 15:18:35 +08:00
private static Dictionary<int, GameObject> fishPrefabDic;//id--prefabs
public static Dictionary<int, FishResourcesData> fishResourcesDatasDic;//id--FishResourcesData
2026-04-29 01:18:39 +08:00
public static Dictionary<int, Sprite> fishResourcesSpritesDic;//id--Sprite
2026-03-27 15:18:35 +08:00
private static Dictionary<int, SkeletonDataAsset> diePrefabDic;//id--SkeletonDataAsset
2026-05-01 06:26:50 +08:00
public static async Task LoadFishAssetsData()
2026-02-03 11:35:19 +08:00
{
2026-05-18 18:39:11 +08:00
// 清空对象池
2026-04-29 23:53:55 +08:00
Pool.Instance.Clear();
2026-02-03 11:35:19 +08:00
2026-03-27 15:18:35 +08:00
fishPrefabDic = new Dictionary<int, GameObject>();
2026-05-01 06:26:50 +08:00
diePrefabDic = new Dictionary<int, SkeletonDataAsset>();
2026-04-29 01:18:39 +08:00
fishResourcesSpritesDic = new Dictionary<int, Sprite>();
2026-05-18 18:39:11 +08:00
// ---------------- 1. 预下载所有依赖资源(关键) ----------------
var labels = new List<string>
{
prefabFish_AALabel,
dieSpine_AALabel,
Sprite_AALabel,
GameManager.Instance.gameData.currGameType.ToString()
};
Debug.Log("开始预下载所有鱼资源依赖...");
var depHandle = Addressables.DownloadDependenciesAsync(labels, MergeMode.Intersection);
await depHandle.Task;
Addressables.Release(depHandle);
Debug.Log("依赖资源预下载完成!");
// ---------------- 2. 加载 Prefab ----------------
var prefabKeys = new List<string> { prefabFish_AALabel, GameManager.Instance.gameData.currGameType.ToString() };
var prefabHandle = Addressables.LoadAssetsAsync<GameObject>(prefabKeys, prefab =>
2026-02-03 11:35:19 +08:00
{
2026-05-01 06:26:50 +08:00
if (FishResourcesPrefabsDic.TryGetValue(prefab.name, out int id))
2026-02-03 11:35:19 +08:00
{
2026-05-01 06:26:50 +08:00
fishPrefabDic[id] = prefab;
var mc = prefab.GetComponent<MonsterContral>();
if (mc != null)
mc.fishTypeID = id;
2026-07-27 16:59:22 +08:00
// ⭐ 统一用 Pool.Instance 的 poolObj 作为父节点(不再用 CreateFishManager.poolRoot),
// 这样 Pool.Clear() 销毁 poolObj 时能连带销毁所有 poolList_XXX 子节点,避免残留
Pool.Instance.dic[id] = new PoolData(prefab, Pool.Instance.EnsurePoolRoot(), id);
2026-02-03 11:35:19 +08:00
}
2026-05-01 06:26:50 +08:00
}, MergeMode.Intersection);
await prefabHandle.Task;
Addressables.Release(prefabHandle);
2026-05-18 18:39:11 +08:00
Debug.Log($"Prefab 加载完成,共 {fishPrefabDic.Count} 个");
2026-05-01 06:26:50 +08:00
2026-05-18 18:39:11 +08:00
// ---------------- 3. 加载死亡 Spine 动效 ----------------
2026-07-24 01:37:11 +08:00
// 注意:某些游戏类型下 Addressables 中 "DieSpine"+"游戏类型" 两个 label 可能没有交集资源,
// 会抛 InvalidKeyException。这里用 try-catch 容错,避免单个资源类型加载失败阻塞整体流程。
try
2026-03-27 15:18:35 +08:00
{
2026-07-24 01:37:11 +08:00
var dieKeys = new List<string> { dieSpine_AALabel, GameManager.Instance.gameData.currGameType.ToString() };
var dieHandle = Addressables.LoadAssetsAsync<SkeletonDataAsset>(dieKeys, data =>
2026-03-27 15:18:35 +08:00
{
2026-07-24 01:37:11 +08:00
if (FishResourcesSpineDieDic.TryGetValue(data.name, out int id))
{
diePrefabDic[id] = data;
}
}, MergeMode.Intersection);
2026-05-01 06:26:50 +08:00
2026-07-24 01:37:11 +08:00
await dieHandle.Task;
Addressables.Release(dieHandle);
Debug.Log($"死亡 Spine 加载完成,共 {diePrefabDic.Count} 个");
}
catch (Exception e)
{
Debug.LogWarning($"[FishResourcesData] 死亡 Spine 加载失败(游戏类型={GameManager.Instance.gameData.currGameType}),跳过此项: {e.Message}");
}
2026-04-29 01:18:39 +08:00
2026-05-18 18:39:11 +08:00
// ---------------- 4. 加载鱼 Sprite ----------------
2026-07-24 01:37:11 +08:00
try
2026-04-29 01:18:39 +08:00
{
2026-07-24 01:37:11 +08:00
var spriteKeys = new List<string> { Sprite_AALabel, GameManager.Instance.gameData.currGameType.ToString() };
var spriteHandle = Addressables.LoadAssetsAsync<Sprite>(spriteKeys, data =>
2026-04-29 01:18:39 +08:00
{
2026-07-24 01:37:11 +08:00
if (FishResourcesSpriteDic.TryGetValue(data.name, out int id))
{
fishResourcesSpritesDic[id] = data;
}
}, MergeMode.Intersection);
2026-05-01 06:26:50 +08:00
2026-07-24 01:37:11 +08:00
await spriteHandle.Task;
Addressables.Release(spriteHandle);
Debug.Log($"Sprite 加载完成,共 {fishResourcesSpritesDic.Count} 个");
}
catch (Exception e)
{
Debug.LogWarning($"[FishResourcesData] Sprite 加载失败(游戏类型={GameManager.Instance.gameData.currGameType}),跳过此项: {e.Message}");
}
2026-05-18 18:39:11 +08:00
Debug.Log("所有鱼资源加载完毕!");
2026-02-03 11:35:19 +08:00
}
2026-05-01 06:26:50 +08:00
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
2026-08-09 17:57:46 +08:00
// ⭐ try-catch 防护:Addressables 可能找不到 TuJian+游戏类型 交集的 Sprite,
// 抛 InvalidKeyException 导致整个 UnLoadFishAssetsData 中断,后续清理无法执行。
// Sprite 加载时已有 try-catch,卸载时也应对称处理
try
2026-05-01 06:26:50 +08:00
{
2026-08-09 17:57:46 +08:00
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)
2026-05-01 06:26:50 +08:00
{
2026-08-09 17:57:46 +08:00
foreach (var sprite in spriteHandle.Result)
2026-05-01 06:26:50 +08:00
{
2026-08-09 17:57:46 +08:00
if (FishResourcesSpriteDic.TryGetValue(sprite.name, out int id))
2026-05-01 06:26:50 +08:00
{
2026-08-09 17:57:46 +08:00
if (fishResourcesSpritesDic.Remove(id))
{
Debug.Log($"卸载鱼Sprite: {sprite.name} (ID={id})");
}
2026-05-01 06:26:50 +08:00
}
}
}
2026-08-09 17:57:46 +08:00
Addressables.Release(spriteHandle);
}
catch (Exception e)
{
Debug.LogWarning($"[FishResourcesData] Sprite 卸载失败(游戏类型={GameManager.Instance.gameData.currGameType}),跳过此项: {e.Message}");
2026-05-01 06:26:50 +08:00
}
Debug.Log($"卸载完成,剩余Prefab数量: {fishPrefabDic.Count}, 死亡动效数量: {diePrefabDic.Count}, Sprite数量: {fishResourcesSpritesDic.Count}");
}
2026-03-27 15:18:35 +08:00
public static GameObject GetFishPrefab(int fishNumber)
2026-02-03 11:35:19 +08:00
{
if (fishPrefabDic != null && fishPrefabDic.ContainsKey(fishNumber))
{
return fishPrefabDic[fishNumber];
}
else
{
Debug.LogError($"鱼Prefab未找到: {fishNumber}");
return null;
}
}
2026-03-27 15:18:35 +08:00
public static SkeletonDataAsset GetFishDieSpine(int fishNumber)
{
if (diePrefabDic != null && diePrefabDic.ContainsKey(fishNumber))
{
return diePrefabDic[fishNumber];
}
else
{
Debug.LogError($"鱼死亡动效未找到: {fishNumber}");
return null;
}
}
2026-02-03 11:35:19 +08:00
}