Unity
Assets 폴더 안의 에셋 동적으로 로딩하기
촙촙쓰
2020. 3. 10. 00:31
보통 resources 폴더 안에 있는 에셋(이미지, 음악)등을 로드할때에는 resources.load<>() 나, resources.loadall<>() 을 이용하면 된다. 하지만 Resources 폴더 외에 다른 폴더에있는 에셋을 로드해야 할 때가 있다.
하나의 에셋을 로드할땐 AssetDatabase.LoadAssetAtPath(assetPath, typeof(에셋타입)); 을 사용하면된다.
resources.loadall<>() ; 대신 사용할 함수를 찾고있었는데, 함수가 따로 마련되지는 않은것같다.
따라서 Assets 폴더의 하위폴더 속 모든 에셋을 로드하는 방법을 알아보려한다.
string[] guids = AssetDatabase.FindAssets("", new string[] { "Assets/CharacterAnimations/Animations/Controller/character_animation" });
Debug.Log(guids.Length);
for (int i = 0; i < guids.Length; i++)
{
string assetPath = AssetDatabase.GUIDToAssetPath(guids[i]);
RuntimeAnimatorController ctr = (RuntimeAnimatorController)AssetDatabase.LoadAssetAtPath(assetPath, typeof(RuntimeAnimatorController));
charAnimations.Add(ctr.name, ctr);
}
로드를 원하는 폴더 안에 에셋이 총 몇개인지 FindAsset을 통해 찾고, for문을 이용하여 하나씩 로드하는 방법이다.
나의경우 애니메이션 컨트롤러를 로드해야했기 때문에 저렇게 작성했지만, 원하는 타입으로 변경해서 응용하면된다
### 수정
위 방법은 에디터에서는 가능한데 빌드를 할때 오류가난다 ( 아마 에디터에서만 사용가능한것같다)
모바일에서 필요할때는 resource폴더에 넣고 사용하는게 좋겠다.