ClientServer/Client/Assets/Scripts/Util/OnDemandLoader.cs

41 lines
1020 B
C#
Raw Normal View History

2025-09-06 17:17:39 +04:00
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[]>();
}