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);
}