- Home /
Most efficient way to transition between scenes?
I'm a little confused on what's going on with Unity when I load a new scene, and I'm not really sure what the best practices are for keeping memory usage and load times down.
At the beginning of each of my scenes, I'm using Resources.Load to load a lot of different prefabs into memory. Many of these are repeated between the different scenes. Is it better to store these as prefabs in a "DontDestroyOnLoad" object and just reference them this way rather than use lots of Resources.Loads in each scene? Or does this not make much of a difference?
I also have to instantiate a lot of objects at runtime at the start of a level due to the game world being procedurally generated. This takes a bit of time to load. Would there be some way to keep this stuff in memory and place them into a new level more quickly, maybe using a pooling system?
Furthermore, is there anything else you'd recommend that I do when entering a new scene to ensure that it loads more quickly and keeps memory usage down?
Answer by ashleyjlive · Jan 12, 2016 at 10:38 AM
Forgive me if I'm wrong but I believe Unity cleans up the memory on objects which aren't marked as "DontDestroyOnLoad" using a garbage collector every time you load a scene (provided it isn't additive scene loading when then everything in the current scene is marked as don't destroy on load). Given this, you can get away with just loading them into memory each time you load a scene but it is far better just to pool them (this does not decrease/increase memory usage but instead improves load times). Therefore, it is better to load them only once and mark them as "DontDestroyOnLoad" if load times is what you are aiming for. If you are mainly focused on memory usage then do not pool these objects and instead just create them when necessary.
To keep memory usage down, ensure that you compress textures and use appropriate filtering on objects in your scene.
Feel free for anyone to correct me if I'm wrong.
This seems pretty accurate. I also posed the question on Reddit, and one of the commenters confirmed what you said here.
Excuse me, but which version of Unity was active when this question was asked (on January 12)? Context might help some users.
Your answer
Follow this Question
Related Questions
Unity Scene loading problem (slow loading until it freezes) 0 Answers
Is there any ideas to load Unity scene asynchronously? 1 Answer
One Location Game (with Scenes Loaded and Disabled) 0 Answers
Scene Loading takes over 3 minutes 1 Answer
Old scene still appears for short time when switching scene 0 Answers