so I am working on this game that has a randomly generated world. It also has different themes for the world. Only one theme is being used at the same time in the game. I put all theme files into the reosurces folder. This is the file structure I am using:
My problem with that is: All files are constantly loaded into memory. I want to only have the currently used theme in memory. If i for testing remove all themes but the one that is active from the resources folder, I will save 60mb on my iPhone. Which is a lot.
This is how I am loading the theme stuff:
// Load all prefabs of the desert theme
Resources.LoadAll("Themes/Desert/Prefabs");
// Load specific material
Resources.Load("Themes/Desert/Materials/sand");
I tried Resources.UnloadUnusedAssets() but there was no effect.
How do I load only the themes I am actually using into memory?
What might have caused those files to load?
I can see a difference between your first and second script. In the second script you Load /Themes/Desert??? Since there is no Desert folder (it should be DesertTheme) it might do buggy things. Try to follow the pathing correctly! I am almost sure this is not the solution but might be something that can help your issue
Did you try this on device? Can you confirm the objects are loaded in memory? What is your method to measure that? The editor can load keep resources loaded sometimes, if you were looking at it there.
If Resources.UnloadUnusedAssets have no effect it means you have a reference to it somewhere
Make sure that there isn’t any reference to the objects left lingering anywhere as described below. If you add them to a list etc, clear that list before calling UnloadUnusedAssets. Did you register an event handler on one of the objects? Clear that too. And so on.
An asset is deemed to be unused if it isn’t reached after walking the whole game object hierarchy, including script components. Static variables are also examined.
The script excecution stack, however, is not examined so an asset referenced only from within the script stack will be unloaded and, if necessary, loaded back in the next time one of its properties or methods is used. This requires extra care for assets which have been modified in memory.
If you want to destroy scene objects loaded using Resources.Load() prior to loading another level, call Object.Destroy() on them. To release assets, use Resources.UnloadUnusedAssets().