- Home /
How do I unregister a sprite atlas with the SpriteAtlasManager?
Is there a way to 'un-register' an atlas with the SpriteAtlasManager?
In this crude example we load the sprite atlas when it is 'requested' by the engine. This occurs when a sprite object is present in a scene that just loaded, as an example...
The problem is, when we unload the atlas, the next time the scene loads, and we want to re-load the atlas, the event SpriteAtlasManager.atlasRequested, doesn't fire, because the engine has no way of knowing that we unloaded the atlas manually (to save memory), there isn't a function or method of telling the SpriteAtlasManager that the atlas no longer exists, at least that I can find.
And unity is not unloading the atlas on it's own. If you load the atlas this way in a scene with sprites, and then transition to an empty scene, the altases are all still in memory. Which is why I am unloading them manually. Even if I call Resources.UnloadUnusedAssets(); the atlases are still there, so I call Addressables.Release(nameOfAtlas); to ensure they get unloaded, but this doesn't inform the SpriteAtlasManager that it's gone, so the next time you go back to the original scene, it doesn't ask to load the atlas....
private void Start() { SpriteAtlasManager.atlasRequested += LoadAtlasRequestFromUnityEngine; } private void LoadAtlasRequestFromUnityEngine(string name, Action callback) { AsyncOperationHandle op = Addressables.LoadAssetAsync<SpriteAtlas>(name); op.Completed += (AsyncOperationHandle handle) => { if (handle.Status == AsyncOperationStatus.Failed) { //Handle fail } else if (handle.Status == AsyncOperationStatus.Succeeded) { successCallback((SpriteAtlas)handle.Result); // Do something with the atlas } }; }
Am I just missing some method somewhere? https://docs.unity3d.com/ScriptReference/U2D.SpriteAtlasManager.html doesn't have anything...