Load sprite atlases manually at runtime
To avoid Unity loading sprite atlases when your project starts, load the sprite atlas yourself at runtime instead.
読み終わるまでの所要時間 4 分最終更新 1ヶ月前
To avoid Unity loading sprite atlases and attaching them to your sprites when your project starts, load the sprite atlas yourself at runtime instead. This is called late binding.
For example, load a lower-resolution variant sprite atlas depending on the hardware, or load a set of sprites into memory only when a later game level needs it.
Exclude a sprite atlas from the build
To prevent Unity automatically including your sprite atlas in your build, follow these steps:
- In the Project window, select the sprite atlas.
- In the window, disable Include in Build.Inspector
?
Load the sprite atlas at runtime
When you disable Include in Build, your sprites still reference the sprite atlas when your project starts, but the sprite atlas texture is missing. You must manually bind the sprite atlas when sprites request it.
Use one of the following methods:
- Load the sprite atlas from the Resources system and load it with the API.
Resources.Load - Load the sprite atlas from an AssetBundle and load it with the API.
AssetBundle
Load the sprite atlas from the Resources system
Follow these steps:
-
In the Project window, create a folder called Resources.
-
Move the sprite atlas asset into the Resources folder. Unity automatically includes assets from the Resources folder in the build, but doesn't load them.
-
In a C# script, attach a new method to thecallback. The
SpriteAtlasManager.atlasRequestedcallback triggers when a sprite requests an unloaded sprite atlas. For example:atlasRequestedvoid OnEnable(){ SpriteAtlasManager.atlasRequested += MySpriteAtlasLoader;} -
In the method, use theAPI to load the sprite atlas from the Resources folder and pass it back to Unity. For example:
Resources.Load// Unity passes in the name of the sprite atlas that the sprite is requesting, and a callback to callvoid MySpriteAtlasLoader(string spriteAtlasName, System.Action<SpriteAtlas> callback){ // Load the sprite atlas var spriteAtlas = UnityEngine.Resources.Load<SpriteAtlas>(spriteAtlasName); // Pass the sprite atlas back to Unity callback(spriteAtlas);}
For more information, refer to Use the Resources system to load assets at runtime.
For a complete example, refer to the Sprite Atlas Examples in the 2D Common package. For more information about package samples, refer to The Package Manager window.
Load the sprite atlas from an AssetBundle
Follow these steps:
-
Assign the sprite atlas to an AssetBundle. For more information, refer to Assign assets to an AssetBundle.
-
In a C# script, attach a new method to thecallback. The
SpriteAtlasManager.atlasRequestedcallback triggers when a sprite requests an unloaded sprite atlas. For example:atlasRequestedvoid OnEnable(){ SpriteAtlasManager.atlasRequested += MySpriteAtlasLoader;} -
In the method, use theAPI to load the sprite atlas from the AssetBundle and pass it back to Unity. For example:
AssetBundle// Unity passes in the name of the sprite atlas that the sprite is requesting, and a callback to callvoid MySpriteAtlasLoader(string spriteAtlasName, System.Action<SpriteAtlas> callback){ // Load the sprite atlas from the AssetBundle var bundle = AssetBundle.LoadFromFile("path/to/assetbundle"); var spriteAtlas = bundle.LoadAsset<SpriteAtlas>(spriteAtlasName); // Pass the sprite atlas back to Unity callback(spriteAtlas);}
For more information, refer to Loading assets from AssetBundles.
For a complete example, refer to the Sprite Atlas Examples in the 2D Common package. For more information about package samples, refer to The Package Manager window.