ClientServer/Client/Assets/Scripts/Util/OnDemandLoader.cs
TG9six 03a642d635 first push
first push
2025-09-06 17:17:39 +04:00

41 lines
1020 B
C#

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class OnDemandLoader
{
public static T Load<T>(string path) where T : Object
{
T returnValue = null;
try
{
if (!resourceCache.ContainsKey(path))
{
resourceCache[path] = Resources.Load<T>(path);
}
returnValue = (T)resourceCache[path];
}
catch
{
Debug.LogError($"Cast of{typeof(T)} cannot be done. Path: {path}");
}
return returnValue;
}
public static T[] LoadAll<T>(string path) where T : Object
{
if (!allResourceCache.ContainsKey(path))
allResourceCache[path] = Resources.LoadAll<T>(path);
return (T[])allResourceCache[path];
}
static Dictionary<string, Object> resourceCache = new Dictionary<string, Object>();
static Dictionary<string, Object[]> allResourceCache = new Dictionary<string, Object[]>();
}