런타임에 주소 가져오기
기본적으로 어드레서블은 에셋에 할당한 주소를 IResourceLocation
인스턴스의 PrimaryKey
값으로 사용합니다.
에셋이 속한 어드레서블 그룹의 Include Addresses in Catalog 옵션을 비활성화하면 PrimaryKey
는 GUID, 레이블 또는 빈 문자열이 될 수 있습니다. AssetReference
또는 레이블과 함께 로드하는 에셋의 주소를 가져오려는 경우 위치에 따라 에셋 로드 기술 자료에 설명된 대로 에셋의 위치를 로드할 수 있습니다. 그런 다음 IResourceLocation
인스턴스를 사용하여 PrimaryKey
값에 액세스하고 에셋을 로드할 수 있습니다.
다음 예시에서는 MyRef1
이라는 AssetReference
오브젝트에 할당된 에셋의 주소를 가져옵니다.
var opHandle = Addressables.LoadResourceLocationsAsync(MyRef1);
yield return opHandle;
if (opHandle.Status == AsyncOperationStatus.Succeeded &&
opHandle.Result != null &&
opHandle.Result.Count > 0)
{
Debug.Log("address is: " + opHandle.Result[0].PrimaryKey);
}
레이블은 여러 에셋을 참조하는 경우가 많습니다. 다음 예시에서는 여러 프리팹 에셋을 로드하고 주요 키 값을 사용하여 해당 에셋을 사전에 추가하는 방법을 설명합니다.
Dictionary<string, GameObject> _preloadedObjects
= new Dictionary<string, GameObject>();
private IEnumerator PreloadHazards()
{
//find all the locations with label "SpaceHazards"
var loadResourceLocationsHandle
= Addressables.LoadResourceLocationsAsync("SpaceHazards", typeof(GameObject));
if (!loadResourceLocationsHandle.IsDone)
yield return loadResourceLocationsHandle;
//start each location loading
List<AsyncOperationHandle> opList = new List<AsyncOperationHandle>();
foreach (IResourceLocation location in loadResourceLocationsHandle.Result)
{
AsyncOperationHandle<GameObject> loadAssetHandle
= Addressables.LoadAssetAsync<GameObject>(location);
loadAssetHandle.Completed +=
obj => { _preloadedObjects.Add(location.PrimaryKey, obj.Result); };
opList.Add(loadAssetHandle);
}
//create a GroupOperation to wait on all the above loads at once.
var groupOp = Addressables.ResourceManager.CreateGenericGroupOperation(opList);
if (!groupOp.IsDone)
yield return groupOp;
Addressables.Release(loadResourceLocationsHandle);
//take a gander at our results.
foreach (var item in _preloadedObjects)
{
Debug.Log(item.Key + " - " + item.Value.name);
}
}