이전 페이지 : https://dean83.tistory.com/204
이번에는 inspector 에서 직접연결을 통해 addressable 리소스를 이용하는 법을 정리한다.
방식은 기존에 inspector 에서 변수에 직접 드래그 하는것과 동일하나, 내부적으로는 GUID를 통해 관리한다.
AssetReference 형식의 변수를 선언하며, addressable 리소스만 연결이 가능하기 때문에
직접 이름을 넣는 방식의 휴먼에러를 막을 수 있다.
1. 스크립트에 AssetReference 변수 추가
- public 혹은 SerializeField 속성의 private 변수 생성
- AssetReference 변수의 경우, 모든 Addressable 리소스를 할당 받을 수 있다.
따라서 특정 형의 리소스를 불러온다는 명시를 해주어야 한다.
- 잘못 불러올경우 오류가 발생 할 수 있다. (예 : Scene 을 Texture로 로딩 등)
[SerializeField]
private AssetReference 변수명;
2. AsyncOperationHandle 에 리소스 배정
if (AssetReference변수명.RuntimeKeyIsValid() == false)
return;
AsyncOperationHandle<GameObject>형 변수 = AssetReference변수명.LoadAsync<GameObject>();
AsyncOperationHandle<GameObject>형 변수.Completed += .....
//AsyncOperationHandle<GameObject>형 변수.Result에 게임오브젝트가 있다
3. AssetReferenceGameObject 형 변수 사용
- 앞서 말했듯, AssetReference는 모든 addressable을 배정할 수 있다보니, 로딩시 잘못된 항목을 불러오면 오류가 발생할 수 있다.
- AssetReferenceGameObject는 GameObject 전용 변수 이다.
[SerializeField]
private AssetReferenceGameObject 변수명;
....
if (AssetReferenceGameObject변수명.RuntimeKeyIsValid() == false)
return;
AsyncOperationHandle<GameObject>변수 = AssetReferenceGameObject변수명.LoadAssetAsync();
AsyncOperationHandle<GameObject>형 변수.Completed += .....
//AsyncOperationHandle<GameObject>형 변수.Result에 게임오브젝트가 있다
...'Unity' 카테고리의 다른 글
| 리소스 관리 - Addressable (4) - Scene (0) | 2024.02.02 |
|---|---|
| 리소스 관리 - Addressable (3) - Sprite (0) | 2024.02.02 |
| 리소스 관리 - Addressable (1) - 초기세팅 및 코드베이스 (0) | 2024.02.01 |
| 빌드 용량 줄이기 - 2 외부 툴 이용 (0) | 2024.01.31 |
| 빌드 용량 줄이기 - 1 (ft.빌드 리포트 확인) (0) | 2024.01.30 |