Search code examples
libgdxassetsskin

Libgdx Skin dispose behavior


I've a question about the libgdx Skin behavior. the game I'm developing has a global AssetsManager (from the libgdx suite) that every class can access. I load different TextureAtlas inside this assets manage I know that assetManager.dispose() disposes all the resources loaded inside the assets manager.

Now, I would like also to have a Skin (for the GUI) loaded inside the assets manager. The skin is gonna use several TextureAtlas...

Here's the question: since I'm gonna use skin.addRegion() and since the online API reference about the skin class says "The atlas will not be automatically disposed when the skin is disposed" is it a good idea to load all the TextureAtlasof the skin in the global assets manager?

I'm fearing about the dispose action. Because when I call the assetManager.dispose() both the TextureAtlas and the Skin will be called on the dispose method...but what if the TextureAtlas are disposed before the skin? Could actually happen any problem about it?

The skin behavior is not so well-defined, I mean...what does the dispose method do?

Thank in advance,

Luca


Solution

  • skin.dispose() calls dispose on any specific resources that are Disposable. But the TextureAtlas itself is not one of the "resources" so it must be manually disposed of separately from the skin.

    The only example of a disposable skin resource I can think of is a BitmapFont that does not use the TextureAtlas that you're using with the Skin.

    Note that you should never call dispose on something that you loaded with the AssetManager. Instead, you should call manager.unload() on that resource so the AssetManager can properly manage dependencies.

    The nice thing about manager.unload() is that it keeps track of how many other resources are dependent on the object and only disposes it when it's clear of dependencies. So if you also load your Skin with the AssetManager, you only ever need to worry about calling manager.unload("mySkin") and it will correctly determine whether the associated TextureAtlas should also be disposed.

    But be sure to only call unload() on a resource once per time you called load() on the same resource. AssetManager's internal dependency counting does rely on all your load() and unload() calls mirroring each other one-to-one.