NiklasEi / bevy_asset_loader

Bevy plugin helping with asset loading and organization
Apache License 2.0
459 stars 52 forks source link

Unload Assets #95

Closed meanbeanmachine closed 1 year ago

meanbeanmachine commented 1 year ago

Do you plan to implement asset unloading? From my tests, this doesn't appear to take place:

  1. Load first state with its own AssetCollection that has 5mb of assets. Memory usage goes up ~5mb
  2. Change state that uses a different AssetCollection that has <1mb of assets and doesn't use any of the assets from the previous state's collection
  3. Note that memory usage doesn't go back down, and the assets from the first collection remain.

This would not be good for applications with large amounts of assets.

Additionally, in my Component despawn function that runs on state exit, I tried AssetServer::free_unused_assets() but that doesn't work either. My knowledge is limited, but I'm guessing it has something to do with the Assets being loaded as Strong handles? Either way, it would be nice if AssetCollections would unload assets if the new state doesn't use them.

I suppose the problem implementing this feature would be that some people would prefer assets to stay loaded, and others not. Maybe an unload option could be set for a collection, individual assets, or both; i.e. tag a collection to be automatically unloaded, except where otherwise specified on the individual asset... and of course vice versa.

NiklasEi commented 1 year ago

The expectation for unloading is that you take care of this yourself. Because, as you already mentioned, different people/games will want different collections to stay and others to be unloaded.

It should be enough to add on_exit systems to a state and use commands.remove_resource::<MyAssetCollection>() to unload that collection. Bevy will then take care of unloading the assets, as long as you do not save handles to the assets anywhere outside of the asset collection.

meanbeanmachine commented 1 year ago

Okie dokie, thanks.