102 lines
2.6 KiB
C#
102 lines
2.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
|
|
public class AddrResourceMgr : MonoBehaviourSingletonTemplate<AddrResourceMgr>
|
|
{
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
DontDestroy();
|
|
}
|
|
|
|
public void LoadObject<T>(string path, Action<AsyncOperationHandle<T>> onCompleted)
|
|
{
|
|
// Addressables에서 비동기 로드
|
|
AsyncOperationHandle<T> handle = Addressables.LoadAssetAsync<T>($"Assets/Res_Addr/{path}");
|
|
|
|
// 로딩 완료 시 호출될 콜백
|
|
handle.Completed += onCompleted;
|
|
}
|
|
//public void Set_AddressableReleaseSelf(GameObject go)
|
|
//{
|
|
// if (DSUtil.CheckNull(go.GetComponent<AddressableReleaseSelf>()))
|
|
// go.AddComponent<AddressableReleaseSelf>();
|
|
//}
|
|
|
|
public IEnumerator LoadObjectSequential<T>(string path, Action<T> onLoaded)
|
|
{
|
|
AsyncOperationHandle<T> handle;
|
|
|
|
try
|
|
{
|
|
// 로드 요청
|
|
handle = Addressables.LoadAssetAsync<T>($"Assets/Res_Addr/{path}");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
#if UNITY_EDITOR
|
|
MyEditorDialog.Show_Dialog($"[Addressable] LoadAssetAsync 예외 발생: {path}\n{e}");
|
|
#endif
|
|
yield break;
|
|
}
|
|
|
|
// 완료될 때까지 대기
|
|
yield return handle;
|
|
|
|
// 상태 확인
|
|
if (handle.Status == AsyncOperationStatus.Succeeded)
|
|
{
|
|
onLoaded?.Invoke(handle.Result);
|
|
}
|
|
else
|
|
{
|
|
#if UNITY_EDITOR
|
|
MyEditorDialog.Show_Dialog($"[Addressable] Failed to load: {path}");
|
|
#endif
|
|
// 실패 로그
|
|
Debug.LogError($"[Addressable] Failed to load: {path}");
|
|
|
|
// Exception 필드가 존재할 수도 있음
|
|
if (handle.OperationException != null)
|
|
{
|
|
Debug.LogError($"[Addressable] Exception: {handle.OperationException}");
|
|
}
|
|
}
|
|
|
|
// 필요 시 해제
|
|
// Addressables.Release(handle);
|
|
}
|
|
|
|
public void Relese(AsyncOperationHandle handle)
|
|
{
|
|
if (handle.IsValid())
|
|
{
|
|
Addressables.Release(handle);
|
|
handle = default; // 핸들 초기화
|
|
}
|
|
}
|
|
}
|
|
|
|
public class MyEditorDialog
|
|
{
|
|
public static void Show_Dialog(string msg, Action onConfirm = null)
|
|
{
|
|
#if UNITY_EDITOR
|
|
bool result = EditorUtility.DisplayDialog(
|
|
"알림",
|
|
msg,
|
|
"확인",
|
|
"취소"
|
|
);
|
|
|
|
if (result)
|
|
{
|
|
onConfirm?.Invoke();
|
|
}
|
|
#endif
|
|
}
|
|
} |