Unity / C#
Intermediate
Unity 2021.3+ / Unity 6
Addressable 에셋 번들 비동기 로딩 및 메모리 누수 없는 릴리즈(Release) 패턴
Resources 폴더의 메모리 낭비를 제거하고 원격/로컬 에셋을 비동기로 로드하며 참조 카운팅(Reference Counting) 기반으로 안전하게 언로드하는 실무 패턴.
#Unity #Addressables #AssetBundle #Memory #Optimization
핵심 개념
Resources.Load는 빌드 시 앱의 시작 시간과 메모리를 폭증시킵니다. Addressables 시스템은 에셋 주소(Key) 기반으로 메모리에 비동기 적재(LoadAssetAsync)하며, 더 이상 필요 없을 때 Addressables.Release로 핸들을 닫아 완벽한 GC 수거를 보장합니다.1. Addressables 안전 로드/언로드 래퍼
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
public class AddressableLoader : MonoBehaviour
{
private AsyncOperationHandle<GameObject> _handle;
public async Task<GameObject> LoadPrefabAsync(string address)
{
_handle = Addressables.LoadAssetAsync<GameObject>(address);
await _handle.Task;
if (_handle.Status == AsyncOperationStatus.Succeeded)
{
return Instantiate(_handle.Result);
}
Debug.LogError($"[Addressables] 로드 실패: {address}");
return null;
}
private void OnDestroy()
{
// 핸들이 유효하면 메모리에서 언로드
if (_handle.IsValid())
{
Addressables.Release(_handle);
}
}
}