Files
5.24BuYu/Assets/MyGame/Scripts/fishResources/FishResourcesData.cs
T
2026-03-18 17:26:14 +08:00

68 lines
2.1 KiB
C#

using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
[Serializable]
public class FishResourcesData
{
public int fishNumber;
public string prefabFishResourcesPath;
const string prefabFish_AAGroup="FishPrefab";
private static Dictionary<string,string> FishResourcesDatas ;
static FishResourcesData()
{
string prefabFishPath = Resources.Load<TextAsset>("tbfishresources").text;
var v = JsonConvert.DeserializeObject<List<FishResourcesData>>(prefabFishPath);
FishResourcesDatas=new Dictionary<string, string>();
foreach (var item in v)
{
if (item.prefabFishResourcesPath!=null&& item.prefabFishResourcesPath != "")
{
Debug.Log($"添加鱼资源数据: {item.fishNumber} - {item.prefabFishResourcesPath}");
FishResourcesDatas.Add(item.prefabFishResourcesPath, item.fishNumber.ToString());
}
}
LoadFishPrefabs();
}
private static Dictionary<string, GameObject> fishPrefabDic;
public static async void LoadFishPrefabs()
{
if (fishPrefabDic != null)
return;
fishPrefabDic = new Dictionary<string, GameObject>();
await Addressables.LoadAssetsAsync<GameObject>(prefabFish_AAGroup, prefab =>
{
if (FishResourcesDatas.ContainsKey(prefab.name))
{
fishPrefabDic.Add(FishResourcesDatas[prefab.name], prefab);
prefab.GetComponent<MonsterContral>().fishTypeID = FishResourcesDatas[prefab.name].ToString();
}
}).Task;
Debug.Log($"加载完成,共 {fishPrefabDic.Count} 个鱼Prefab");
}
public static GameObject GetFishPrefab(string fishNumber)
{
if (fishPrefabDic != null && fishPrefabDic.ContainsKey(fishNumber))
{
return fishPrefabDic[fishNumber];
}
else
{
Debug.LogError($"鱼Prefab未找到: {fishNumber}");
return null;
}
}
}