Files
5.24BuYu/Assets/MyGame/Scripts/fishResources/TurretResources.cs
T
2026-05-22 19:30:02 +08:00

116 lines
4.4 KiB
C#

using Newtonsoft.Json;
using Spine.Unity;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
using static UnityEngine.AddressableAssets.Addressables;
[Serializable]
public class TurretResourcesData
{
public int turretNumber;
public int turretType; // 1普通炮台 2战神 3翅膀
public string prefabTurretResourcesPath;
public int animationType; // 1spine 2animator 3animation
public int attackAudio;
const string prefabTurret_AALabel = "TurretPrefab";
const string turretSpine_AALabel = "TurretSpine";
const string turretSprite_AALabel = "TurretSprite";
private static Dictionary<string, int> TurretPrefabsDic; // prefabPath -> id
private static Dictionary<string, int> TurretSpineDic; // spinePath -> id
private static Dictionary<string, int> TurretSpriteDic; // spritePath -> id
public static Dictionary<int, TurretResourcesData> turretResourcesDatasDic; // id -> data
private static Dictionary<int, GameObject> turretPrefabDic; // id -> prefab
private static Dictionary<int, Sprite> turretSpriteDic; // id -> sprite
static TurretResourcesData()
{
Init();
}
async static void Init()
{
string turretPath = Resources.Load<TextAsset>("tbTurretResources").text;
var list = JsonConvert.DeserializeObject<List<TurretResourcesData>>(turretPath);
TurretPrefabsDic = new Dictionary<string, int>();
TurretSpineDic = new Dictionary<string, int>();
TurretSpriteDic = new Dictionary<string, int>();
turretResourcesDatasDic = new Dictionary<int, TurretResourcesData>();
foreach (var item in list)
{
if (!string.IsNullOrEmpty(item.prefabTurretResourcesPath))
{
TurretPrefabsDic[item.prefabTurretResourcesPath] = item.turretNumber;
TurretSpriteDic[item.prefabTurretResourcesPath] = item.turretNumber;
turretResourcesDatasDic[item.turretNumber] = item;
}
}
}
public static async Task LoadTurretAssetsData()
{
turretPrefabDic = new Dictionary<int, GameObject>();
turretSpriteDic = new Dictionary<int, Sprite>();
var labels = new List<string>
{
prefabTurret_AALabel,
turretSpine_AALabel,
turretSprite_AALabel,
GameManager.Instance.gameData.currGameType.ToString()
};
Debug.Log("开始预下载炮台资源依赖...");
var depHandle = Addressables.DownloadDependenciesAsync(labels, MergeMode.Intersection);
await depHandle.Task;
Addressables.Release(depHandle);
Debug.Log("炮台依赖资源预下载完成!");
// ---------------- 1. 加载炮台 Prefab ----------------
var prefabKeys = new List<string> { prefabTurret_AALabel, GameManager.Instance.gameData.currGameType.ToString() };
var prefabHandle = Addressables.LoadAssetsAsync<GameObject>(prefabKeys, prefab =>
{
if (TurretPrefabsDic.TryGetValue(prefab.name, out int id))
{
turretPrefabDic[id] = prefab;
}
}, MergeMode.Intersection);
await prefabHandle.Task;
Addressables.Release(prefabHandle);
Debug.Log($"炮台 Prefab 加载完成,共 {turretPrefabDic.Count} 个");
// ---------------- 3. 加载炮台 Sprite ----------------
var spriteKeys = new List<string> { turretSprite_AALabel, GameManager.Instance.gameData.currGameType.ToString() };
var spriteHandle = Addressables.LoadAssetsAsync<Sprite>(spriteKeys, data =>
{
if (TurretSpriteDic.TryGetValue(data.name, out int id))
{
turretSpriteDic[id] = data;
}
}, MergeMode.Intersection);
await spriteHandle.Task;
Addressables.Release(spriteHandle);
Debug.Log($"炮台 Sprite 加载完成,共 {turretSpriteDic.Count} 个");
Debug.Log("所有炮台资源加载完毕!");
}
public static GameObject GetTurretPrefab(int turretNumber)
{
return turretPrefabDic.TryGetValue(turretNumber, out var prefab) ? prefab : null;
}
public static Sprite GetTurretSprite(int turretNumber)
{
return turretSpriteDic.TryGetValue(turretNumber, out var sprite) ? sprite : null;
}
}