Addressables 클래스를 활용한 리소스 동기/비동기 로드 및 캐싱 처리 샘플 코드.
// 비동기 처리
public void LoadAsync<T>(string address, Action<T> callback = null) where T : Object
{
string key = address;
if(_loadedAssets.TryGetValue(key, out Object res))
{
callback?.Invoke(op.Result);
return;
}
var asyncOperation = Addressables.LoadAssetAsync<T>(key);
if (!asyncOperation.IsValid())
{
Debug.LogError($"asset load failed : {address}");
return;
}
asyncOperation.Completed += (op) =>
{
_loadedAssets.Add(key, op.Result);
callback?.Invoke(op.Result);
};
}
// 동기 처리
public T LoadSync<T>(string address) where T : Object
{
string key = address;
if(_loadedAssets.TryGetValue(key, out Object res))
return (T)res;
var asyncOperation = Addressables.LoadAssetAsync<T>(key);
if (!asyncOperation.IsValid())
{
Debug.LogError($"asset load failed : {address}");
return null;
}
asyncOperation.WaitForCompletion();
_loadedAssets.Add(key, asyncOperation.Result);
return asyncOperation.Result;
}
Addressables 공식 문서
https://docs.unity3d.com/Packages/com.unity.addressables@1.21/manual/load-assets-asynchronous.html
Asynchronous loading | Addressables | 1.21.19
Asynchronous loading The Addressables system API is asynchronous and returns an AsyncOperationHandle to use to manage operation progress and completion. Addressables is designed to be content location agnostic. The content might need to be downloaded first
docs.unity3d.com
게임 개발에 필수적인 내용을 담는 명서들을 소개합니다.
<유니티 교과서 개정6판>(유니티 최신 버전) 구입 링크
https://link.coupang.com/a/be3P0t
유니티 교과서 개정6판
COUPANG
www.coupang.com
<리얼-타임 렌더링(REAL-TIME RENDERING) 4/e> 구입 링크
https://link.coupang.com/a/8VWas
리얼-타임 렌더링 4/e
COUPANG
www.coupang.com
<이득우의 게임 수학:39가지 예제로 배운다! 메타버스를 구성하는 게임 수학의 모든 것> 구입 링크
https://link.coupang.com/a/9BqLd
이득우의 게임 수학:39가지 예제로 배운다! 메타버스를 구성하는 게임 수학의 모든 것
COUPANG
www.coupang.com
유니티 에셋 스토어 링크
https://assetstore.unity.com?aid=1011lvz7h
에셋스토어
여러분의 작업에 필요한 베스트 에셋을 찾아보세요. 유니티 에셋스토어가 2D, 3D 모델, SDK, 템플릿, 툴 등 여러분의 콘텐츠 제작에 날개를 달아줄 다양한 에셋을 제공합니다.
assetstore.unity.com
(링크를 통해 도서/에셋 구입시 일정액의 수수료를 지급받습니다.)
'유니티 엔진 (Unity Engine)' 카테고리의 다른 글
[Unity] Inspector 에 Addressable AssetReference 배열 노출시키기 (0) | 2024.01.15 |
---|---|
[Unity] Shader Keyword 셰이더 키워드로 동작 조건부 작성 (0) | 2023.12.20 |
[Unity] Editor SceneView 에서 카메라 조작하기 (fov, position, rotation) (0) | 2023.12.07 |
[Unity] C# Finalizer 와 Garbage Collector 동작에 대한 정리 (0) | 2023.12.01 |
[Unity] Flags Attribute (0) | 2023.11.30 |
[Unity] ScriptableObject의 CustomEditor가 있을 때 저장이 안 되는 문제 (0) | 2023.11.22 |
[Unity] 드로우 콜 최적화 (Optimize DrawCall) 요약 정리 (0) | 2023.11.16 |