atlasRequested는 빌드에 포함되지 않은 스프라이트가 요청될 때 이를 알리는 콜백입니다. 늦은 바인딩으로 스프라이트 아틀라스를 수동으로 로드하고 스프라이트를 Unity 에디터로 전송해야 합니다.
다음 예시는 콜백을 설정하여 늦은 바인딩으로 에셋 번들의 스프라이트 아틀라스를 로드하는 방법을 보여줍니다. 이 스크립트는 스프라이트 아틀라스를 빌드 또는 프로젝트에 이미 존재하는 스프라이트에 로드하고 바인딩하는 데 사용됩니다. 런타임 시점에 GetSprites로 스프라이트 아틀라스의 콘텐츠를 가져오는 방식으로 프로젝트에 존재하지 않는 스프라이트를 스프라이트 아틀라스에서 만들 수 있습니다.
void OnEnable()
{
SpriteAtlasManager.atlasRequested += RequestAtlas;
}
void OnDisable()
{
SpriteAtlasManager.atlasRequested -= RequestAtlas;
}
void RequestAtlas(string tag, System.Action callback)
{
if (spriteAtlas == null)
{
StartCoroutine(LoadFromStreammingAsset(callback));
}
else
{
callback(spriteAtlas);
}
}
IEnumerator LoadFromStreammingAsset(System.Action callback)
{
string path = Application.streamingAssetsPath + "/" + bundleName;
print(path);
AssetBundleCreateRequest bundleLoadRequest = AssetBundle.LoadFromFileAsync(Application.streamingAssetsPath + "/" + bundleName);
yield return bundleLoadRequest;
bundle = bundleLoadRequest.assetBundle;
if (bundle == null)
{
Debug.Log("Failed to load AssetBundle!");
yield break;
}
SpriteAtlas spriteAtlas = bundle.LoadAsset(bundleName);
callback(spriteAtlas);
}