- Home /
Long scene load time caused by many textures in project(mobile)
OK, I've been putting this off for a while, but I'd like to figure it out..
I have a 2D game which uses sprites w/ Alpha for all the characters in the scene, which may be quite a few at once and of numerous different types. My pictures folder currently contains about 1,500 images, and I'm about halfway done...
This all runs perfectly fine on a variety of android devices (barring only the CRAPPIEST knockoff I have, which can't handle the transparency as usual).
That is, everything seems to run fine, once a scene is loaded, however, EVERY TIME I load a new scene (really it's just back and forth between two scenes), it takes like 20 or 30 seconds.
This is due to the large number of textures. Every time I add another stack to the project, the load time increases. To be clear, I would expect, say, an increased game load time at LAUNCH, when all the textures are first cached. I MIGHT EVEN anticipate a long load in general for my MAIN scene, which actually may involve the sprites in question.. But even loading my menu scene takes super long now, and that does not involve any sprites.. And, like I said, once my main scene (or any scene) is loaded, there is no performance issue, even though ANY of my prefabs may be instantiated at any time.. (meaning all the textures are successfully cached with no apparent lag) So why does changing scenes always take so long?
A bit more on the setup: Each "character" is a game object with a script which contains several Texture[] arrays, which are already set with their appropriate textures.. These are all prefabs, and are instantiated/destroyed randomly as needed in my main scene, without issue.
Note that, since all my levels are in fact one single scene, I could simply combine it with my menu scene to make the whole game essentially one scene.. That would (essentially) solve this issue, but I'd like to understand the real deal.. Why is it that more PICTURES in my ASSETS folder equals longer load for EVERY SCENE, EVERY TIME? And, is there a way around this?
(my suspicion is that the resources are maybe being automatically re-loaded (onto the prefabs?), in which case I'm thinking maybe I need to put everything into the Resources folder to avoid this.. Or maybe just dynamically create my character templates in-scene so there are no prefabs.. But I may be way off.. I am about to start a test build to test these (A: is it the prefab connection, or the textures themselves.. and B: will Resources provide a solution, or the no-prefab approach, etc), but meanwhile any advice is greatly appreciated!)
Answer by Bunny83 · Dec 26, 2012 at 11:42 AM
I'm a bit confused. You said your game happens only in one scene, so why do you switch the scenes all the time? I think the problem is that you don't reference any of the prefabs / assets in your menu scene. Unity loads on demand so when you load your game scene it will load all prefabs + images into memory. When you switch back to then menu scene the prefabs are no longer needed since there's no reference to them anymore. I guess that unity will unload the assets when you enter the menu scene. That's what takes so long when you switch back to the menu.
It's hard for unity to determine what assets you might want to use again and which ones can be removed. Since the scene and all references to the assets have need destroyed I guess unity will remove the assets.
If that's the problem you can try to add a manager script that hold references to all your prefabs and use DontDestroyOnLoad on it. Keep in mind that loading a scene again will duplicate objects which have been marked with DDOL. So it's best to create the manager object by code and check if it exists already (a classical singleton)
Unfortunately I am already pooling all my prefabs into the scene on my title screen.. And using DDOL to keep them intact. (I have a GameObject in the title screen with an instance of each prefab as a child). This does not affect the load time either way, though it's a great theory nonetheless!
(my plan is to use the pooled objects rather than instantiate, but it's not yet fully set up, in case you are wondering why I have it that way..) :)
I have also (since this post) changed all my prefabs to contain only one or two textures, rather than 100 or more.. (in a test copy), thereby breaking the prefab connection and drastically reducing the number of textures referenced by the PREFABS (but leaving the in-scene "POOL" object. No change either, so DDOL doesn't solve anything regardless..
Next, I delete the Pool object completely, but only when the third scene has loaded...
more to come..
(oh, I didn't know switching scenes was SUCH an ordeal, or I suppose I would have done things differently! :] Still, it'll all be worth it once I get to the bottom of this ;])
But keep in $$anonymous$$d if you place objects with DDOL in a scene to load this scene only once. In the racing game I'm developing AT$$anonymous$$ I also switched to object pools, but it didn't change much. Enabling and disabling game objects is almost as slow as instantiate and destroy. It's a bit faster so I keep it this way. Pooling brought a lot new problems when it comes to reinitialize the oobjects but it depends on your setup.
$$anonymous$$y game only use one scene. It's much easier to use layers and different cameras to "switch" scenes. Also my menus are all made with the Unity GUI system so I don't depend on different scenes.
I have and in game editor which I made a prefab. When entering the edit mode I just instantiate the editor prefab and when pressing quit I destroy it. I've done the same for the game environment (a game prefab). However most parts of the environment are instantiated at runtime from prefabs. The prefabs references are all placed on a manager script in my only scene. I never use LoadLevel.
Thanks for the tips! my Title screen IS only loaded once.. I would say the "new problems" are the same type of reasons I haven't yet completed the switch.. Although in this case the core framework is SO simple, it won't be too tough... I doubt if the change will even register, but they say it's better... (as a rule)
Really there is NO REASON why I can't switch to a single scene at this point, but of course I would still like to understand what I'm missing.. Once I do, I expect I will ultimately switch to one scene no matter what, since that involves NO LOAD regardless.
turns out there was one or two other issues that had slipped by and were also contributing to this issue, most notably an old legacy object from an outdated build which seems to contain some bad code, hidden discreetly in my scene. Just deleting that helped considerably, so though some of the load time was attributable to the textures, I seem to have been accidentally exaggerating the extent. Probably was responsible for the sort of "unloading time" issue @Bunny83 mentioned above. Either way, switching to a single scene is DEFINITELY the way to go, my only load screen now is on initial launch, and not bad.. Thanks too for re-assuring me that it's O$$anonymous$$ to use only ONE SCENE, I was leaning that way but uneasy!
Your answer
Follow this Question
Related Questions
Texture Quality - Performance for Mobile Devices 2 Answers
My mobile game crashes once a certain number of textures have been loaded. What can I do about that? 0 Answers
Unity5 Android mip-mapping problem 1 Answer
Blue looks like teal on mobile? 0 Answers
Cells for a mobile app stacked vertically, not moving correctly 0 Answers