- Home /
Performance issues when loading first scene
Hi! I'm creating a strategy game using NGUI. The game involves mostly a complex UI and the usage of lots of data text files and images (no 3D graphics and nearly no 2D animation involved). I'm creating my game using a single scene and I add/remove overlay screens accordingly. The game loads and executes immediately when using the Editor but, for some reason, it takes ages to load when running as a standalone Windows executable (sometimes it takes more than one minute to load).
My initial scene has a few game objects that holds references to all the prefabs that are used throughout the game (mostly container objects that hold references to text assets and images), but none of the prefabs get instantiated unless they are absolutely needed.
I'm trying to look for ways to speed up my game, as I'm sure I'm doing something silly here. Do you have any ideas on where should I look? I'm using Unity Pro.
Thanks in advance!
Edit: Does the fact that an object holds a reference to several prefabs have an impact on its loading time despite even when those prefabs are not instantiated at a later stage? Is it possible that somehow Unity is preloading the prefab reference and that is the cause behind the slowdown when running the game as a standalone executable while it works perfectly when it runs inside the editor?
Edit 2: I've changed my project so that it starts with a scene containing a single game object that has an attached script which triggers the loading of the first game scene inside the Start function (using Application.LoadLevelAsync). I've run the profiler again and I get the information for the first frame and then it freezes until the first game scene finishes loading. I've also put some code in the awake and start methods of the objects that belong to the first game scene so that they print the time it takes for them to execute and I can confirm they do it quite quickly. Would it be possible that this freezing occurs because of all the references I have in the game objects from the first game scene (see the first Edit)?
Answer by TheCheese · Dec 18, 2012 at 08:01 PM
Your problem is pretty vague, but the best place to start optimizing is with the profiler. You can see what scripts and assets are chewing up your time.
http://docs.unity3d.com/Documentation/Manual/Profiler.html
EDIT: In particular, look for a function called "Loading.UpdatePreloading" when your second level loads. Drilling down into that function will eventually gave you list of objects in the right-hand pane and how long each of them takes to load. This may help you figure out if all those prefabs are being loaded with the scene.
If this is the issue, you may want to look into using the Resource folder: http://docs.unity3d.com/Documentation/Manual/LoadingResourcesatRuntime.html
Yes, my apologies if I didn't provide enough information in my original post, but I wasn't sure which bits would be relevant for my problem and that's why I decided to keep it concise and provide more information based on the answers. I tried using the profiler before posting here and found out that it virtually freezes until the scene loading process is over. One thing that got my attention is that in the memory page, it says that the "Game Objects in Scene" are 25 (which is about right), but that the "Total Object Count" is 8868. What is this "Total Object Count"? What's the difference between them and the GameObjects? I'll update my original post with one more question.
Total Object Count are the total number of objects loaded in memory. While a GameObject is an object visible in the hierarchy, a single gameObject is made up of many other objects (each component on a gameObject is an "object"). It's possible you're running out of system memory - but I would look at the CPU part of the profiler to see if any scripts are taking a long time or if any particular assets are taking a long time to load. Having references to objects in a script may well account for so many objects being loaded in memory with so few actual "gameObjects" loaded in the scene.
Thank you for your reply. I've updated my original post with a second edit detailing the changes I've made and the results. As you can see, I cannot blame an individual function for this slowdown.