migrate changes from audio system
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
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();
|
||||
|
||||
[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 = Resources.Load<T>(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) };
|
||||
ResourceRequest newRequest = Resources.LoadAsync<T>(path);
|
||||
StartCoroutine(HandleAsyncLoadCompletion(path, newRequest));
|
||||
}
|
||||
|
||||
IEnumerator HandleAsyncLoadCompletion(string path, ResourceRequest request)
|
||||
{
|
||||
yield return request;
|
||||
|
||||
if (request.asset)
|
||||
{
|
||||
this.m_cachedObjects[path] = request.asset;
|
||||
}
|
||||
|
||||
if (this.m_pendingCallbacks.Remove(path, out List<Action<Object>> callbacks))
|
||||
{
|
||||
foreach (Action<Object> callback in callbacks)
|
||||
{
|
||||
callback?.Invoke(request.asset);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void PreloadAsync<T>(string[] paths)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal void Release<T>(string path)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
internal void ReleaseUnused()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user