개요
Adressable을 프로젝트에 도입할 때
관리할 Adressable 에셋들을 인스펙터 리스트에 담아두거나 하고싶은데
Adressable은 여러 에셋을 한번에 드래그해서 리스트에 담을 수 없다.
그래서 코드로
List<Sprite>
타입에서 List<AssetReferenceSprite>
타입으로 복사하는 방법을 기록한다.
본문
코드는 다음과 같다.
[SerializeField] private List<AssetReferenceSprite> adressableSprites;
[SerializeField] private List<Sprite> tempSprites;
#if UNITY_EDITOR
[Button]
public void SpritesToAdressableList()
{
adressableSprites = new List<AssetReferenceSprite>();
foreach (var spr in tempSprites)
{
string path = UnityEditor.AssetDatabase.GetAssetPath(spr);
adressableSprites.Add(new AssetReferenceSprite(UnityEditor.AssetDatabase.GUIDFromAssetPath(path).ToString()));
}
}
#endif
참고로 [Button]
은 오딘 인스펙터 에셋을 써서 인스펙터에서 간단하게 함수 버튼을 만들어 주는 기능이다.
에셋의 주소를 가져와서 AssetReferenceSprite
타입으로 변환하면 된다.
주의할점은
UnityEditor API를 쓰기때문에 빌드에 포함하면 에러가 나므로 일회용으로 쓰거나
#Unity_Editor
로 전처리문을 구성해야 한다.
참고
Is there a way to set Asset Reference via code?
Basically I'd like to assign all my asset references on runtime but it seems that I'm only able to set the reference via manual selecting it in the...
forum.unity.com
'Unity > Tips' 카테고리의 다른 글
[Unity 최적화] Dynamic Resolution을 적용해보자! (0) | 2023.08.02 |
---|---|
[Unity] 유니티 에디터 OpenGL/Vulkan 그래픽스API로 열기 (0) | 2023.08.02 |
[Unity] Scene뷰에서 축 아이콘 없이 스크린샷 찍기 (0) | 2022.11.01 |
[Unity] URP/HDRP에서 OnPostRender() 사용하기 (0) | 2022.08.09 |
[Unity] 루트모션 부모에 적용하기 (0) | 2022.03.31 |