67de857f8c
checkpoint: StreamingAsset Loader.
113 lines
3.3 KiB
C#
113 lines
3.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using JetBrains.Annotations;
|
|
using UnityEngine;
|
|
using Object = UnityEngine.Object;
|
|
|
|
namespace OCES
|
|
{
|
|
public class ResourceLoader : MonoBehaviour
|
|
{
|
|
readonly Dictionary<string, Object> m_cachedObjects = new();
|
|
readonly Dictionary<string, List<Action<Object>>> m_pendingCallbacks = new();
|
|
IAssetProvider m_assetProvider;
|
|
|
|
public void SetProvider(IAssetProvider assetProvider)
|
|
{
|
|
this.m_assetProvider = assetProvider;
|
|
}
|
|
|
|
[CanBeNull]
|
|
internal T LoadSync<T>(string path) where T : Object
|
|
{
|
|
if (this.m_cachedObjects.TryGetValue(path, out Object cachedObject))
|
|
{
|
|
return cachedObject as T;
|
|
}
|
|
|
|
T newObject;
|
|
if (this.m_assetProvider is not null)
|
|
{
|
|
newObject = this.m_assetProvider.Load<T>(path);
|
|
}
|
|
else
|
|
{
|
|
newObject = Resources.Load<T>(path);
|
|
Debug.LogWarning($"[ResourceLoader] No IAssetProvider set, falling back to Resources.Load for '{path}'");
|
|
}
|
|
|
|
this.m_cachedObjects.Add(path, newObject);
|
|
return newObject;
|
|
}
|
|
|
|
internal void LoadAsync<T>(string path, Action<T> onComplete) where T : Object
|
|
{
|
|
if (this.m_cachedObjects.TryGetValue(path, out Object cachedObject))
|
|
{
|
|
onComplete?.Invoke(cachedObject as T);
|
|
return;
|
|
}
|
|
|
|
if (this.m_pendingCallbacks.TryGetValue(path, out List<Action<Object>> callbacks))
|
|
{
|
|
callbacks.Add(obj => onComplete?.Invoke(obj as T));
|
|
return;
|
|
}
|
|
|
|
this.m_pendingCallbacks[path] = new List<Action<Object>> { obj => onComplete?.Invoke(obj as T) };
|
|
|
|
if (this.m_assetProvider is not null)
|
|
{
|
|
this.m_assetProvider.LoadAsync<T>(path, this, asset =>
|
|
{
|
|
OnAsyncAssetLoaded(path, asset as Object);
|
|
});
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[ResourceLoader] No IAssetProvider set, falling back to Resources.Load for '{path}'");
|
|
StartCoroutine(FallbackLoadAsyncCoroutine<T>(path));
|
|
}
|
|
}
|
|
|
|
IEnumerator FallbackLoadAsyncCoroutine<T>(string path) where T : Object
|
|
{
|
|
ResourceRequest request = Resources.LoadAsync<T>(path);
|
|
yield return request;
|
|
OnAsyncAssetLoaded(path, request.asset);
|
|
}
|
|
|
|
void OnAsyncAssetLoaded(string path, Object asset)
|
|
{
|
|
if (asset)
|
|
{
|
|
this.m_cachedObjects[path] = asset;
|
|
}
|
|
|
|
if (this.m_pendingCallbacks.Remove(path, out List<Action<Object>> callbacks))
|
|
{
|
|
foreach (Action<Object> callback in callbacks)
|
|
{
|
|
callback?.Invoke(asset);
|
|
}
|
|
}
|
|
}
|
|
|
|
internal void PreloadAsync<T>(string[] paths)
|
|
{
|
|
|
|
}
|
|
|
|
internal void Release<T>(string path)
|
|
{
|
|
|
|
}
|
|
|
|
internal void ReleaseUnused()
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|