Unloading asset in code

Hello,

I am trying to unload asset to save memory after I load and then use it.
It seems like assets are cached forever even after asset.unload() - after asset.unload() I called

pc.app.assets.loadFromUrl(url, ‘model’, function() {
// never get here because the asset is cached (exists in the asset repository ) I guess
});

I know I can use pc.app.assets.getByUrl(url) to reuse one in the repository, but, I do not want to keep the asset in memory every time I load it - I want to load a model and then unload it, load new one and so forth because I have many models to be loaded.

Would you help? I did pc.app.assets.remove(asset); but it seems not doing anything.

Thank you,

Hey!

myAsset.unload(); will remove the asset from RAM/VRAM (memory will be reclaimed) but not from browser’s cache. The asset remains in the browsers cache and you are able to reload it using this.app.assets.load(myAsset);

I am not sure you have much control there from Javascript if you really want to remove it from the disk as well (browser cache).

Thank you, now I think I know what’s going on:
I have loaded a model through pc.app.assets.loadFromUrl(url, ‘model’, …). When I unload the model I did:
pc.app.assets.getByUrl(url).unload() and PlayCanvas seems like unload only the ‘model’ without unloading associated materials, and that prevents callback in .loadFromUrl() from being called when I reload model I have reloaded previously.

If this is true then what is the best practice to remove all materials and textures belonging to the model? Or any advice please?

That’s a good question, now that you ask I am not sure if unloading a model will unload all accompanying materials and textures. Or if you must find them manually one by one and unload them.

For unloading use the method found on each asset and not on the assets registry.

myAsset.unload();