When you make levels in the game, what is more reasonable - do each level on your own scene and load with LoadScene, or spoof the destruction of all objects on the screen and generate a level on the same scene? What takes up more space? Do old scenes in memory hang after LoadScene?

  • I think the exact answer is only known by the profiler, it seems that after a scene change, the objects are deleted, but all at once, which is faster than manually by 1. - pavel

1 answer 1

In memory links to scenes exactly hang. Otherwise it would be impossible to switch between them.

At the expense of memory: by default, when the scene is loaded - everything on the previous scene is destroyed. Otherwise, the memory would flow away like a river.

However, some resources may remain loaded. You can use Resources.UnloadUnusedAssets to clean up unused resources. And something in the documentation

You can use a profiler to view what is happening with your memory. For example, there is a tool like xCode . True, it is for ios and it seems Android. Try to look to read information about him, how to work with him in Unity , I think it will be very useful. Just in case, a short video on how xCode works with Unity .

what is smarter - to do each level on your own stage and load with LoadScene or to wipe out the destruction of all objects on the screen and generate a level on the same stage

It is more reasonable to act according to circumstances. For common sense ruled! And in any way not one pattern of actions for everything and everyone.

For example , if you are writing a simple plain Arcanoid , then all the logic can be defined in the script (for example, the configuration and arrangement of blocks at each level can be easily described in the config file, rather than using 100,500 scenes and arranging these blocks manually. Simply based on the config will be generated other grid). And then it is enough to generate objects at the beginning of the configuration level, and at the end to regenerate a new grid with other parameters. It is logical to have one level and dynamic generation.

Another example : some RPG for which you need to generate a map, place in the right places enemies / objects / portals / secret places / chests / other.

You have to agree, all this is described in the configuration and writing a lot of additional logic for generating them in the right coordinates with the right rotation angle will be much more expensive than arranging all this on the scenes and switching between them. Imagine how much extra work: you have to manually build the entire scene, then all the data about each necessary object (of which there may be thousands on the scene) are written into the necessary configs, set up the logic of interaction between the objects of the scene, then delete everything from the scene. And so for each scene. And if you need to move a pebble or a chest of gold or the enemy to another place - what to do? Again, re-create everything, shift, look at the coordinates, overwrite? Write a bunch of code, classes, interfaces, interaction and control over all of this. Horror horror.

So do everything on the same stage here will be more expensive.

Bottom line: for a memory analysis, use the profiler convenient, and to be or not to be, that is the question to destroy everything and rebuild everything anew, or put everything on different scenes - depends on the concept, the complexity of the project and how much you want to bother with certain things