备份,准备做双端
This commit is contained in:
@@ -65,6 +65,31 @@ public static class EquipmentSkillGroupLibrary
|
||||
return asset != null ? asset.skillGroup : null;
|
||||
}
|
||||
|
||||
// --- Runtime cache -------------------------------------------------------
|
||||
// Equipment skill-group assets are static config loaded from Resources and never
|
||||
// mutated at runtime, so both the per-category asset list and the id->asset resolution
|
||||
// are memoized during play mode. This turns a per-note/per-trigger Resources.LoadAll +
|
||||
// collection-allocation storm into O(1) dictionary lookups. The editor (!isPlaying)
|
||||
// path stays uncached so asset edits are always reflected; the cache is cleared on
|
||||
// play-mode start via RuntimeInitializeOnLoadMethod so a fresh session never sees stale
|
||||
// entries from a prior play session (domain-reload-disabled projects).
|
||||
private static readonly Dictionary<EquipmentSkillGroupSO.EquipmentSkillCategory, List<EquipmentSkillGroupSO>> s_assetsByCategory =
|
||||
new Dictionary<EquipmentSkillGroupSO.EquipmentSkillCategory, List<EquipmentSkillGroupSO>>();
|
||||
// Keyed by (category, skillGroupId); caches both hits and misses (null value = confirmed miss).
|
||||
private static readonly Dictionary<long, EquipmentSkillGroupSO> s_findAssetCache = new Dictionary<long, EquipmentSkillGroupSO>();
|
||||
|
||||
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
|
||||
private static void ResetRuntimeCache()
|
||||
{
|
||||
s_assetsByCategory.Clear();
|
||||
s_findAssetCache.Clear();
|
||||
}
|
||||
|
||||
private static long MakeFindKey(int skillGroupId, EquipmentSkillGroupSO.EquipmentSkillCategory category)
|
||||
{
|
||||
return ((long)category << 32) | (uint)skillGroupId;
|
||||
}
|
||||
|
||||
public static EquipmentSkillGroupSO FindAsset(int skillGroupId, EquipmentSkillGroupSO.EquipmentSkillCategory category)
|
||||
{
|
||||
if (skillGroupId <= 0)
|
||||
@@ -72,7 +97,19 @@ public static class EquipmentSkillGroupLibrary
|
||||
return null;
|
||||
}
|
||||
|
||||
bool useCache = Application.isPlaying;
|
||||
long findKey = 0;
|
||||
if (useCache)
|
||||
{
|
||||
findKey = MakeFindKey(skillGroupId, category);
|
||||
if (s_findAssetCache.TryGetValue(findKey, out EquipmentSkillGroupSO cached))
|
||||
{
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
|
||||
var assets = LoadAssets(category);
|
||||
EquipmentSkillGroupSO resolved = null;
|
||||
for (int i = 0; i < assets.Count; i++)
|
||||
{
|
||||
EquipmentSkillGroupSO asset = assets[i];
|
||||
@@ -83,15 +120,28 @@ public static class EquipmentSkillGroupLibrary
|
||||
|
||||
if (asset.skillGroup.skillGroupID == skillGroupId)
|
||||
{
|
||||
return asset;
|
||||
resolved = asset;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
if (useCache)
|
||||
{
|
||||
// Cache hits AND misses (null) so repeated unmatched ids don't re-scan.
|
||||
s_findAssetCache[findKey] = resolved;
|
||||
}
|
||||
|
||||
return resolved;
|
||||
}
|
||||
|
||||
public static List<EquipmentSkillGroupSO> LoadAssets(EquipmentSkillGroupSO.EquipmentSkillCategory category)
|
||||
{
|
||||
// Play mode: build the per-category list once (from Resources) and reuse it.
|
||||
if (Application.isPlaying && s_assetsByCategory.TryGetValue(category, out var cachedList))
|
||||
{
|
||||
return cachedList;
|
||||
}
|
||||
|
||||
var results = new List<EquipmentSkillGroupSO>();
|
||||
|
||||
#if UNITY_EDITOR
|
||||
@@ -146,6 +196,11 @@ public static class EquipmentSkillGroupLibrary
|
||||
}
|
||||
}
|
||||
|
||||
if (Application.isPlaying)
|
||||
{
|
||||
s_assetsByCategory[category] = results;
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user