Files
bansonic_beta_main/Assets/scripts/_runtimecache/RuntimeResourcesCache.cs
T

121 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
public static class RuntimeResourcesCache
{
private static readonly Dictionary<string, UnityEngine.Object[]> TypedLoadAllCache =
new Dictionary<string, UnityEngine.Object[]>(StringComparer.Ordinal);
private static readonly Dictionary<string, UnityEngine.Object> LoadCache =
new Dictionary<string, UnityEngine.Object>(StringComparer.Ordinal);
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void ResetStaticState()
{
TypedLoadAllCache.Clear();
LoadCache.Clear();
}
public static T[] LoadAll<T>(string path) where T : UnityEngine.Object
{
string normalizedPath = NormalizePath(path);
string cacheKey = typeof(T).FullName + "|" + normalizedPath;
if (TypedLoadAllCache.TryGetValue(cacheKey, out UnityEngine.Object[] cached))
{
return cached as T[] ?? Array.Empty<T>();
}
T[] loaded = Resources.LoadAll<T>(normalizedPath) ?? Array.Empty<T>();
TypedLoadAllCache[cacheKey] = loaded;
return loaded;
}
public static T Load<T>(string path) where T : UnityEngine.Object
{
string normalizedPath = NormalizePath(path);
string cacheKey = typeof(T).FullName + "|" + normalizedPath;
if (LoadCache.TryGetValue(cacheKey, out UnityEngine.Object cached))
{
return cached as T;
}
T loaded = Resources.Load<T>(normalizedPath);
LoadCache[cacheKey] = loaded;
return loaded;
}
public static Player_SO LoadDefaultPlayerSo()
{
Player_SO player = Load<Player_SO>("playerdata/playerData_uniqueData");
if (player != null)
{
return player;
}
Player_SO[] players = LoadAll<Player_SO>(string.Empty);
return players.Length > 0 ? players[0] : null;
}
public static SongData[] LoadAllSongs()
{
return LoadAll<SongData>(string.Empty);
}
public static SongData[] LoadSongIndex()
{
return LoadAll<SongData>("song_songIndex");
}
public static SongData[] LoadSongsFromPath(string path)
{
return LoadAll<SongData>(path);
}
public static AllyHero_SO[] LoadAllAllyHeroes()
{
return LoadAll<AllyHero_SO>(string.Empty);
}
public static storeItemSO[] LoadAllStoreItems()
{
return LoadAll<storeItemSO>("so/storeSO");
}
public static dlcData[] LoadAllDlcs()
{
return LoadAll<dlcData>(string.Empty);
}
public static expBottlesSO[] LoadAllExpBottles()
{
return LoadAll<expBottlesSO>(string.Empty);
}
public static growthMaterialSO[] LoadAllGrowthMaterials()
{
return LoadAll<growthMaterialSO>(string.Empty);
}
public static equipmentConsumableSO[] LoadAllEquipmentConsumables()
{
return LoadAll<equipmentConsumableSO>(string.Empty);
}
public static mail_so[] LoadAllMailDefinitions()
{
return LoadAll<mail_so>("so/mail_so");
}
public static void InvalidateAll()
{
TypedLoadAllCache.Clear();
LoadCache.Clear();
}
private static string NormalizePath(string path)
{
return string.IsNullOrWhiteSpace(path) ? string.Empty : path.Trim().Replace("\\", "/");
}
}