nightward/Assets/Scripts/Addressable/AddrHandleBase.cs

88 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.UI;
public class AddrHandleBase : MonoBehaviour
{
protected Dictionary<string, AsyncOperationHandle> m_Handle = new Dictionary<string, AsyncOperationHandle>();
protected void Load_Obj<T>(string path, Action<AsyncOperationHandle<T>> loadfinish = null)
{
ReleaseHandle(path);
AddrResourceMgr.Ins.LoadObject<T>(path, handle =>
{
m_Handle[path] = handle;
loadfinish?.Invoke(handle);
});
}
protected void Load_Image(Image image, string path, Action loadfinish = null)
{
image.enabled = false;
ReleaseHandle(path);
AddrResourceMgr.Ins.LoadObject<Sprite>(path, handle =>
{
m_Handle[path] = handle;
image.enabled = true;
image.sprite = handle.Result;
loadfinish?.Invoke();
});
}
protected void Load_SpriteRenderer(SpriteRenderer image, string path, Action loadfinish = null)
{
image.enabled = false;
ReleaseHandle(path);
AddrResourceMgr.Ins.LoadObject<Sprite>(path, handle =>
{
m_Handle[path] = handle;
image.enabled = true;
image.sprite = handle.Result;
loadfinish?.Invoke();
});
}
protected void Load_SpriteRenderer(SpriteRenderer[] images, string path, Action loadfinish = null)
{
for (int i = 0; i < images.Length; i++)
images[i].enabled = false;
ReleaseHandle(path);
AddrResourceMgr.Ins.LoadObject<Sprite>(path, handle =>
{
m_Handle[path] = handle;
for (int i = 0; i < images.Length; i++)
{
images[i].enabled = true;
images[i].sprite = handle.Result;
}
loadfinish?.Invoke();
});
}
/// <summary>
/// 특정 path의 핸들만 해제
/// </summary>
private void ReleaseHandle(string path)
{
if (m_Handle.TryGetValue(path, out var handle))
{
AddrResourceMgr.Ins.Relese(handle);
m_Handle.Remove(path);
}
}
protected virtual void OnDestroy()
{
foreach (var handle in m_Handle.Values)
AddrResourceMgr.Ins.Relese(handle);
m_Handle.Clear();
}
}