1
This commit is contained in:
@@ -1429,12 +1429,33 @@ public class SitPosData : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
|
||||
|
||||
currentTurretNumber = turretNumber;
|
||||
|
||||
// 从 TurretResourcesData 拿预制体
|
||||
// ⭐ 散包按需加载:缓存没有时异步取(PT{编号} 标签),完成后实例化
|
||||
StartCoroutine(LoadTurretRoutine(turretNumber, ++_turretLoadSeq));
|
||||
}
|
||||
|
||||
/// <summary>炮台加载序号(防加载期间再次切换的竞态)</summary>
|
||||
private int _turretLoadSeq = 0;
|
||||
|
||||
/// <summary>炮台异步按需加载协程:AcquireTurretPrefabAsync 完成后实例化(原同步逻辑后半段)</summary>
|
||||
private IEnumerator LoadTurretRoutine(int turretNumber, int seq)
|
||||
{
|
||||
GameObject prefab = TurretResourcesData.GetTurretPrefab(turretNumber);
|
||||
if (prefab == null)
|
||||
{
|
||||
Debug.LogWarning($"[SitPosData] 炮台预制体未加载: #{turretNumber}");
|
||||
return;
|
||||
var task = TurretResourcesData.AcquireTurretPrefabAsync(turretNumber);
|
||||
while (!task.IsCompleted) yield return null;
|
||||
if (seq != _turretLoadSeq) // 期间又切换了炮台 → 丢弃并归还引用
|
||||
{
|
||||
TurretResourcesData.ReleaseTurretPrefab(turretNumber);
|
||||
yield break;
|
||||
}
|
||||
prefab = task.Result;
|
||||
}
|
||||
if (prefab == null)
|
||||
{
|
||||
TurretResourcesData.ReleaseTurretPrefab(turretNumber);
|
||||
Debug.LogWarning($"[SitPosData] 炮台预制体未加载: #{turretNumber}(检查 PT{turretNumber} 标签配置)");
|
||||
yield break;
|
||||
}
|
||||
|
||||
// 克隆到父节点
|
||||
@@ -1451,11 +1472,14 @@ public class SitPosData : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
|
||||
{
|
||||
Debug.LogError($"[SitPosData] 预制体上未找到 TurretData 组件: #{turretNumber}");
|
||||
Destroy(instance);
|
||||
return;
|
||||
yield break;
|
||||
}
|
||||
|
||||
currentTurret.turretNumber = turretNumber;
|
||||
|
||||
// ⭐ 战神装备中判断:异步加载完成后实时读(原方法开头局部变量 zhanShenActive 不能跨协程)
|
||||
bool zhanShenActive = currentZhanShenData != null;
|
||||
|
||||
// 缓存旋转节点:优先用 TurretData.tra_RotationPivot,否则回退父节点
|
||||
tra_CurrTurret = GetTurretRotationPivot(currentTurret);
|
||||
|
||||
@@ -1591,12 +1615,30 @@ public class SitPosData : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
|
||||
// 先清理已有翅膀
|
||||
ClearWing();
|
||||
|
||||
// 从 TurretResourcesData 拿预制体
|
||||
// ⭐ 散包按需加载(异步):缓存没有时按 PT{编号} 标签取,完成后实例化
|
||||
StartCoroutine(LoadWingRoutine(turretNumber, ++_turretLoadSeq));
|
||||
}
|
||||
|
||||
/// <summary>翅膀异步按需加载协程(模式同炮台:seq 防竞态)</summary>
|
||||
private IEnumerator LoadWingRoutine(int turretNumber, int seq)
|
||||
{
|
||||
GameObject prefab = TurretResourcesData.GetTurretPrefab(turretNumber);
|
||||
if (prefab == null)
|
||||
{
|
||||
Debug.LogWarning($"[SitPosData] 翅膀预制体未加载: #{turretNumber}");
|
||||
return;
|
||||
var task = TurretResourcesData.AcquireTurretPrefabAsync(turretNumber);
|
||||
while (!task.IsCompleted) yield return null;
|
||||
if (seq != _turretLoadSeq)
|
||||
{
|
||||
TurretResourcesData.ReleaseTurretPrefab(turretNumber);
|
||||
yield break;
|
||||
}
|
||||
prefab = task.Result;
|
||||
}
|
||||
if (prefab == null)
|
||||
{
|
||||
TurretResourcesData.ReleaseTurretPrefab(turretNumber);
|
||||
Debug.LogWarning($"[SitPosData] 翅膀预制体未加载: #{turretNumber}(检查 PT{turretNumber} 标签配置)");
|
||||
yield break;
|
||||
}
|
||||
|
||||
// 克隆到 wingParent,保持预制体原始位置/旋转/缩放
|
||||
@@ -1637,20 +1679,52 @@ public class SitPosData : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
|
||||
/// (渲染由座位预设的 Camera + RawImage 完成,显示节点挂在 turretRotate 下会跟随瞄准旋转)。
|
||||
/// ⭐ 炮台/战神显示二选一:加载战神后隐藏 turretParent(仅游戏内,切换面板 UI 不受影响)。
|
||||
/// </summary>
|
||||
/// <summary>战神加载序号(防加载期间再次切换的竞态:旧协程完成后丢弃结果)</summary>
|
||||
private int _zsLoadSeq = 0;
|
||||
|
||||
public void LoadZhanShen(int zhanShenNumber)
|
||||
{
|
||||
// XJMH 模式没有战神概念,直接跳过(和翅膀一致)
|
||||
if (turretControlMode == TurretControlMode.XJMH) return;
|
||||
|
||||
// 先清理已有战神(保留预设的 Camera,它是用来渲染战神到 RawImage 的)
|
||||
ClearZhanShenInstance();
|
||||
// ⭐ 切换战神:释放上一个的按需资源引用(别的座位还在用同一个战神则只减计数不卸载)
|
||||
int prevNumber = currentZhanShenNumber;
|
||||
ClearZhanShenInstance(); // 先清理已有战神实例(保留预设的 Camera)
|
||||
if (prevNumber > 0 && prevNumber != zhanShenNumber)
|
||||
TurretResourcesData.ReleaseZhanShen(prevNumber, ZhanShenUsage.GamePlay);
|
||||
|
||||
// 从 TurretResourcesData 拿游戏内使用的战神预制体
|
||||
GameObject prefab = TurretResourcesData.GetZhanShenPrefab(zhanShenNumber, ZhanShenUsage.GamePlay);
|
||||
// ⭐ 异步按需加载(不再阻塞主线程):散包模式下战神不预载,这里现取
|
||||
StartCoroutine(LoadZhanShenRoutine(zhanShenNumber, ++_zsLoadSeq));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 战神异步加载协程:等 AcquireZhanShenAsync 完成后实例化。
|
||||
/// 加载期间座位被销毁 / 又切换了别的战神 → 丢弃本次结果并归还引用。
|
||||
/// </summary>
|
||||
private IEnumerator LoadZhanShenRoutine(int zhanShenNumber, int seq)
|
||||
{
|
||||
var task = TurretResourcesData.AcquireZhanShenAsync(zhanShenNumber, ZhanShenUsage.GamePlay);
|
||||
while (!task.IsCompleted)
|
||||
yield return null;
|
||||
|
||||
// 竞态丢弃:期间又切换了战神(seq 变了)→ 归还本次引用
|
||||
if (seq != _zsLoadSeq)
|
||||
{
|
||||
TurretResourcesData.ReleaseZhanShen(zhanShenNumber, ZhanShenUsage.GamePlay);
|
||||
yield break;
|
||||
}
|
||||
|
||||
GameObject prefab = task.Result;
|
||||
if (prefab == null)
|
||||
{
|
||||
Debug.LogWarning($"[SitPosData] 战神使用预制体未加载: #{zhanShenNumber}(检查 ZSYX 标签配置)");
|
||||
return;
|
||||
// 标签未配置(散包未拆):回退全量字典(启动 PT+ZSYX 加载的)
|
||||
prefab = TurretResourcesData.GetZhanShenPrefab(zhanShenNumber, ZhanShenUsage.GamePlay);
|
||||
}
|
||||
if (prefab == null)
|
||||
{
|
||||
TurretResourcesData.ReleaseZhanShen(zhanShenNumber, ZhanShenUsage.GamePlay);
|
||||
Debug.LogWarning($"[SitPosData] 战神使用预制体未加载: #{zhanShenNumber}(检查 ZS{zhanShenNumber}+ZSYX 标签配置)");
|
||||
yield break;
|
||||
}
|
||||
|
||||
// 克隆到 zhanShenParent,保持预制体原始位置/旋转/缩放
|
||||
@@ -1702,7 +1776,11 @@ public class SitPosData : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
|
||||
/// <summary>清除战神并恢复普通炮台显示(炮台/战神显示二选一的另一半)</summary>
|
||||
public void ClearZhanShen()
|
||||
{
|
||||
// ⭐ 卸下战神:释放按需资源引用(归零才真正卸包)
|
||||
int prevNumber = currentZhanShenNumber;
|
||||
ClearZhanShenInstance();
|
||||
if (prevNumber > 0)
|
||||
TurretResourcesData.ReleaseZhanShen(prevNumber, ZhanShenUsage.GamePlay);
|
||||
currentZhanShenNumber = 0;
|
||||
|
||||
// 恢复普通炮台显示(特殊鱼炮台使用中不显示:等 RestoreTurretVisual 统一恢复)
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AddressableAssets;
|
||||
using UnityEngine.ResourceManagement.AsyncOperations;
|
||||
using UnityEngine.U2D;
|
||||
using static UnityEngine.AddressableAssets.Addressables;
|
||||
|
||||
/// <summary>
|
||||
@@ -50,6 +51,156 @@ public class TurretResourcesData
|
||||
// ---- 保留的 AA Handle(不释放,避免 Build 下资源被卸载)----
|
||||
private static readonly List<AsyncOperationHandle> _loadedHandles = new List<AsyncOperationHandle>();
|
||||
|
||||
// ==================== 战神按需加载(散包:用哪个加载哪个) ====================
|
||||
// 每个战神单独打标签 ZS{编号}(如 ZS14901),展示 prefab = ZS{id}+Default,游戏内 = ZS{id}+ZSYX。
|
||||
// 战神 prefab 不再带 PTCBZS 标签 → 启动全量加载(PT+Default/PT+ZSYX)不再拉出战神,
|
||||
// turretchibangzhanshenres 包只剩炮台/翅膀/sprite,战神包按需单独加载。
|
||||
|
||||
/// <summary>战神 prefab 缓存:(编号, 用途) → prefab</summary>
|
||||
private static readonly Dictionary<(int id, ZhanShenUsage usage), GameObject> _zsPrefabCache
|
||||
= new Dictionary<(int, ZhanShenUsage), GameObject>();
|
||||
/// <summary>战神加载句柄:(编号, 用途) → handle(卸载用;LoadAssetsAsync 返回 IList 句柄)</summary>
|
||||
private static readonly Dictionary<(int id, ZhanShenUsage usage), AsyncOperationHandle<IList<GameObject>>> _zsHandles
|
||||
= new Dictionary<(int, ZhanShenUsage), AsyncOperationHandle<IList<GameObject>>>();
|
||||
/// <summary>战神引用计数:(编号, 用途) → 使用中的座位/预览数(归零才真正卸载,防止多座位共用误卸)</summary>
|
||||
private static readonly Dictionary<(int id, ZhanShenUsage usage), int> _zsRefCount
|
||||
= new Dictionary<(int, ZhanShenUsage), int>();
|
||||
|
||||
/// <summary>
|
||||
/// ⭐ 按需获取单个战神 prefab(异步 + 引用计数)。
|
||||
/// 用途:进房/换战神(GamePlay)、大厅预览(Display)。
|
||||
/// 每次调用引用 +1;切换/关闭时配对调用 ReleaseZhanShen(-1),
|
||||
/// 引用归零才真正卸载战神包 —— 多个座位用同一个战神不会互相误卸。
|
||||
/// 标签:ZS{编号} + Default(展示)/ZSYX(游戏内) 交集;标签未配置时返回 null
|
||||
/// (调用方可回退 GetZhanShenPrefab 查全量字典,平滑迁移)。
|
||||
/// </summary>
|
||||
public static async Task<GameObject> AcquireZhanShenAsync(int turretNumber, ZhanShenUsage usage)
|
||||
{
|
||||
var key = (turretNumber, usage);
|
||||
_zsRefCount[key] = _zsRefCount.TryGetValue(key, out int c) ? c + 1 : 1;
|
||||
if (_zsPrefabCache.TryGetValue(key, out var cached)) return cached;
|
||||
|
||||
var keys = new List<string> { "ZS" + turretNumber, usage == ZhanShenUsage.GamePlay ? zhanShenGameSubLabel : defaultSubLabel };
|
||||
var handle = Addressables.LoadAssetsAsync<GameObject>(keys, null, MergeMode.Intersection);
|
||||
await handle.Task;
|
||||
if (handle.Status == AsyncOperationStatus.Succeeded && handle.Result != null && handle.Result.Count > 0)
|
||||
{
|
||||
// 展示/游戏内同名靠标签区分,交集结果取第一个
|
||||
_zsPrefabCache[key] = handle.Result[0];
|
||||
_zsHandles[key] = handle;
|
||||
return handle.Result[0];
|
||||
}
|
||||
// 加载失败(标签未配置等):归还引用计数,不缓存句柄
|
||||
if (handle.IsValid()) Addressables.Release(handle);
|
||||
_zsRefCount[key] = c; // 还原为进入时的值
|
||||
if (_zsRefCount[key] <= 0) _zsRefCount.Remove(key);
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ⭐ 按需获取炮台/翅膀 prefab(异步 + 引用计数,标签 PT{编号},如 PT11001)。
|
||||
/// 散包模式下启动不再全量加载 prefab,座位装炮台/大厅预览时按需取。
|
||||
/// 与 ReleaseTurretPrefab 配对(切换/关闭时 -1,归零卸包)。
|
||||
/// </summary>
|
||||
public static async Task<GameObject> AcquireTurretPrefabAsync(int turretNumber)
|
||||
{
|
||||
_ptRefCount[turretNumber] = _ptRefCount.TryGetValue(turretNumber, out int c) ? c + 1 : 1;
|
||||
if (_ptPrefabCache.TryGetValue(turretNumber, out var cached)) return cached;
|
||||
|
||||
var handle = Addressables.LoadAssetsAsync<GameObject>(new List<string> { "PT" + turretNumber }, null, MergeMode.Intersection);
|
||||
await handle.Task;
|
||||
if (handle.Status == AsyncOperationStatus.Succeeded && handle.Result != null && handle.Result.Count > 0)
|
||||
{
|
||||
_ptPrefabCache[turretNumber] = handle.Result[0];
|
||||
_ptHandles[turretNumber] = handle;
|
||||
return handle.Result[0];
|
||||
}
|
||||
if (handle.IsValid()) Addressables.Release(handle);
|
||||
_ptRefCount[turretNumber] = c;
|
||||
if (_ptRefCount[turretNumber] <= 0) _ptRefCount.Remove(turretNumber);
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>释放炮台/翅膀 prefab 引用(与 AcquireTurretPrefabAsync 配对,归零卸包)</summary>
|
||||
public static void ReleaseTurretPrefab(int turretNumber)
|
||||
{
|
||||
if (!_ptRefCount.TryGetValue(turretNumber, out int c) || c <= 0) return;
|
||||
if (c > 1) { _ptRefCount[turretNumber] = c - 1; return; }
|
||||
_ptRefCount.Remove(turretNumber);
|
||||
if (_ptHandles.TryGetValue(turretNumber, out var h) && h.IsValid())
|
||||
Addressables.Release(h);
|
||||
_ptHandles.Remove(turretNumber);
|
||||
_ptPrefabCache.Remove(turretNumber);
|
||||
}
|
||||
|
||||
/// <summary>按需缓存:炮台/翅膀 prefab(PT{编号} 标签)</summary>
|
||||
private static readonly Dictionary<int, GameObject> _ptPrefabCache = new Dictionary<int, GameObject>();
|
||||
private static readonly Dictionary<int, AsyncOperationHandle<IList<GameObject>>> _ptHandles = new Dictionary<int, AsyncOperationHandle<IList<GameObject>>>();
|
||||
private static readonly Dictionary<int, int> _ptRefCount = new Dictionary<int, int>();
|
||||
|
||||
/// <summary>
|
||||
/// 强制卸载按需加载的炮台/翅膀 prefab(离房时调用,忽略引用计数)。
|
||||
/// </summary>
|
||||
public static void UnloadAllTurretPrefabs()
|
||||
{
|
||||
var keys = new List<int>(_ptHandles.Keys);
|
||||
foreach (var k in keys)
|
||||
{
|
||||
if (_ptHandles[k].IsValid())
|
||||
Addressables.Release(_ptHandles[k]);
|
||||
}
|
||||
_ptHandles.Clear();
|
||||
_ptPrefabCache.Clear();
|
||||
_ptRefCount.Clear();
|
||||
if (keys.Count > 0)
|
||||
Debug.Log($"[TurretResourcesData] 卸载炮台/翅膀按需 prefab {keys.Count} 项");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放战神引用(切换战神/关闭预览时调用,与 AcquireZhanShenAsync 配对)。
|
||||
/// 引用归零才真正卸载包;标签未配置走全量回退时此调用无害(没有计数项直接跳过)。
|
||||
/// </summary>
|
||||
public static void ReleaseZhanShen(int turretNumber, ZhanShenUsage usage)
|
||||
{
|
||||
var key = (turretNumber, usage);
|
||||
if (!_zsRefCount.TryGetValue(key, out int c) || c <= 0) return;
|
||||
if (c > 1)
|
||||
{
|
||||
_zsRefCount[key] = c - 1;
|
||||
return;
|
||||
}
|
||||
// 归零:真正卸载
|
||||
_zsRefCount.Remove(key);
|
||||
if (_zsHandles.TryGetValue(key, out var h) && h.IsValid())
|
||||
Addressables.Release(h);
|
||||
_zsHandles.Remove(key);
|
||||
_zsPrefabCache.Remove(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 强制卸载按需加载的战神资源(离房时清游戏内的;面板关闭可清展示的)。
|
||||
/// usage 传 null 清全部。忽略引用计数(离房场景所有引用者都已销毁)。
|
||||
/// </summary>
|
||||
public static void UnloadAllZhanShen(ZhanShenUsage? usage = null)
|
||||
{
|
||||
var toRemove = new List<(int, ZhanShenUsage)>();
|
||||
foreach (var kv in _zsHandles)
|
||||
{
|
||||
if (usage == null || kv.Key.usage == usage.Value)
|
||||
toRemove.Add(kv.Key);
|
||||
}
|
||||
foreach (var k in toRemove)
|
||||
{
|
||||
if (_zsHandles[k].IsValid())
|
||||
Addressables.Release(_zsHandles[k]);
|
||||
_zsHandles.Remove(k);
|
||||
_zsPrefabCache.Remove(k);
|
||||
_zsRefCount.Remove(k);
|
||||
}
|
||||
if (toRemove.Count > 0)
|
||||
Debug.Log($"[TurretResourcesData] 卸载战神按需资源 {toRemove.Count} 项(usage={usage?.ToString() ?? "全部"})");
|
||||
}
|
||||
|
||||
static TurretResourcesData()
|
||||
{
|
||||
Init();
|
||||
@@ -147,54 +298,58 @@ public class TurretResourcesData
|
||||
Addressables.Release(zsDepHandle);
|
||||
Debug.Log("[TurretResourcesData] 战神游戏内依赖资源预下载完成!");
|
||||
|
||||
// ---- 加载炮台 Prefab(展示用)----
|
||||
var prefabHandle = Addressables.LoadAssetsAsync<GameObject>(labels, prefab =>
|
||||
{
|
||||
if (turretPrefabPathDic.TryGetValue(prefab.name, out int id))
|
||||
{
|
||||
turretPrefabDic[id] = prefab;
|
||||
Debug.Log($"[TurretResourcesData] 加载 Prefab: {prefab.name} (ID={id})");
|
||||
}
|
||||
}, MergeMode.Intersection);
|
||||
// ⭐ 散包模式:炮台/翅膀/战神的 prefab 不再启动全量加载(每个资源一个小包,
|
||||
// 全量会把上百个散包全部拉起 —— 刚登录 194 个包的根源)。
|
||||
// prefab 全部按需:炮台/翅膀走 AcquireTurretPrefabAsync(PT{编号} 标签),
|
||||
// 战神走 AcquireZhanShenAsync(ZS{编号}+Default/ZSYX 标签)。
|
||||
// 启动只加载列表小图 Sprite(若 Sprite 与 prefab 同包仍会拉起散包,
|
||||
// 应把列表小图合入图集 —— 见 SpriteAtlasBuilderWindow 工具)。
|
||||
|
||||
await prefabHandle.Task;
|
||||
// 不释放!保留 Handle 以保持资源存活
|
||||
_loadedHandles.Add(prefabHandle);
|
||||
Debug.Log($"[TurretResourcesData] 炮台 Prefab 加载完成,共 {turretPrefabDic.Count} 个");
|
||||
|
||||
// ---- 加载战神游戏内 Prefab(ZSYX 子标签;与展示同名,靠标签区分)----
|
||||
var zsPrefabHandle = Addressables.LoadAssetsAsync<GameObject>(zsGameLabels, prefab =>
|
||||
{
|
||||
if (turretPrefabPathDic.TryGetValue(prefab.name, out int id)
|
||||
&& turretDataDic.TryGetValue(id, out var data)
|
||||
&& data.turretType == 2) // 只收战神(turretType==2)的游戏内使用预制体
|
||||
{
|
||||
turretGamePrefabDic[id] = prefab;
|
||||
Debug.Log($"[TurretResourcesData] 加载战神游戏内 Prefab: {prefab.name} (ID={id})");
|
||||
}
|
||||
}, MergeMode.Intersection);
|
||||
|
||||
await zsPrefabHandle.Task;
|
||||
// 不释放!保留 Handle 以保持资源存活
|
||||
_loadedHandles.Add(zsPrefabHandle);
|
||||
Debug.Log($"[TurretResourcesData] 战神游戏内 Prefab 加载完成,共 {turretGamePrefabDic.Count} 个");
|
||||
|
||||
// ---- 加载炮台 Sprite ----
|
||||
// ---- 加载炮台 Sprite(列表图标,全量保留)----
|
||||
var spriteHandle = Addressables.LoadAssetsAsync<Sprite>(labels, data =>
|
||||
{
|
||||
if (turretSpritePathDic.TryGetValue(data.name, out int id))
|
||||
{
|
||||
turretSpriteDic[id] = data;
|
||||
Debug.Log($"[TurretResourcesData] 加载 Sprite: {data.name} (ID={id})");
|
||||
}
|
||||
}, MergeMode.Intersection);
|
||||
|
||||
await spriteHandle.Task;
|
||||
// 不释放!保留 Handle 以保持资源存活
|
||||
_loadedHandles.Add(spriteHandle);
|
||||
Debug.Log($"[TurretResourcesData] 炮台 Sprite 加载完成,共 {turretSpriteDic.Count} 个");
|
||||
|
||||
Debug.Log("[TurretResourcesData] 所有炮台资源加载完毕!");
|
||||
// ⭐ 图集兜底:列表小图已合入 SpriteAtlas 且 sprite 从组里移除(标签查不到)时,
|
||||
// 按图集标签 "TurretAtlas" 加载图集,用 GetSprite(名) 填表。
|
||||
// (图集方式下 sprite 仍在图集里保留原名 = prefabTurretResourcesPath)
|
||||
if (turretSpriteDic.Count == 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
var atlasHandle = Addressables.LoadAssetsAsync<SpriteAtlas>("TurretAtlas", null);
|
||||
await atlasHandle.Task;
|
||||
if (atlasHandle.Status == AsyncOperationStatus.Succeeded
|
||||
&& atlasHandle.Result != null && atlasHandle.Result.Count > 0)
|
||||
{
|
||||
var atlas = atlasHandle.Result[0];
|
||||
foreach (var kv in turretSpritePathDic)
|
||||
{
|
||||
var sp = atlas.GetSprite(kv.Key);
|
||||
if (sp != null)
|
||||
turretSpriteDic[kv.Value] = sp;
|
||||
}
|
||||
_loadedHandles.Add(atlasHandle);
|
||||
Debug.Log($"[TurretResourcesData] 图集方式加载 Sprite 完成,共 {turretSpriteDic.Count} 个(来自 {atlas.name})");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Debug.LogWarning($"[TurretResourcesData] 图集兜底加载失败(未找到 TurretAtlas 标签的图集?): {e.Message}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log($"[TurretResourcesData] 炮台 Sprite 加载完成,共 {turretSpriteDic.Count} 个(prefab 已改按需加载)");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -229,22 +384,28 @@ public class TurretResourcesData
|
||||
return turretDataDic != null && turretDataDic.TryGetValue(turretNumber, out var data) ? data : null;
|
||||
}
|
||||
|
||||
/// <summary>根据 ID 获取炮台 Prefab。</summary>
|
||||
/// <summary>根据 ID 获取炮台 Prefab。⭐ 优先查按需缓存(PT{编号} 标签),回退全量字典(已不再填充,恒空)。</summary>
|
||||
public static GameObject GetTurretPrefab(int turretNumber)
|
||||
{
|
||||
return turretPrefabDic != null && turretPrefabDic.TryGetValue(turretNumber, out var prefab) ? prefab : null;
|
||||
if (_ptPrefabCache.TryGetValue(turretNumber, out var prefab)) return prefab;
|
||||
return turretPrefabDic != null && turretPrefabDic.TryGetValue(turretNumber, out var fullLoad) ? fullLoad : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据战神编号获取战神预制体(两种情况二选一)。
|
||||
/// - Display:展示用(大厅/商城预览,与 GetTurretPrefab 返回同一个展示模型);
|
||||
/// - GamePlay:游戏内用(座位上替代普通炮台的使用模型)。
|
||||
/// 未加载、非战神或未配置使用预制体时返回 null。
|
||||
/// ⭐ 优先查按需加载缓存(散包);未加载时回退全量加载的字典(标签未拆时的平滑迁移)。
|
||||
/// </summary>
|
||||
public static GameObject GetZhanShenPrefab(int turretNumber, ZhanShenUsage usage = ZhanShenUsage.Display)
|
||||
{
|
||||
// 按需缓存优先
|
||||
if (_zsPrefabCache.TryGetValue((turretNumber, usage), out var prefab))
|
||||
return prefab;
|
||||
|
||||
// 回退:全量加载的字典(战神 prefab 未去 PTCBZS 标签时仍会被启动全量加载进来)
|
||||
var dic = usage == ZhanShenUsage.GamePlay ? turretGamePrefabDic : turretPrefabDic;
|
||||
return dic != null && dic.TryGetValue(turretNumber, out var prefab) ? prefab : null;
|
||||
return dic != null && dic.TryGetValue(turretNumber, out var fullLoad) ? fullLoad : null;
|
||||
}
|
||||
|
||||
/// <summary>根据 ID 获取炮台 Sprite。</summary>
|
||||
|
||||
Reference in New Issue
Block a user