11
\$\begingroup\$

I have a scene with a massive terrain. It took a long time to load (approximately 60 seconds) when I pressed play. I am in the editor.

I prefabbed it.

Now it only took about 5 seconds to load when I pressed play. Why?

My best guess is that there is some preloading done when it is prefabbed, but it only takes a few seconds to prefab it. So that couldn't be the reason. Any ideas? I am at a loss.

\$\endgroup\$
5
  • 5
    \$\begingroup\$ My best guess is that, just before running, Unity caches the current state of the scene in the editor, so that when you stop the game it can put everything back the way it was (even if you had changes that hadn't yet been saved to the scene file on disc). By offloading the big terrain to the prefab, it doesn't need to go through this caching step, which might speed things up. I haven't tested this hypothesis though. \$\endgroup\$
    – DMGregory
    Commented Dec 20, 2016 at 19:45
  • 2
    \$\begingroup\$ Are you talking about the editor or a complied project? In case of an exe file, it shouldn't really matter all that much (haven't tested) but the editor shouldn't have a cache available. Prefabs should have that though and that might speed things up in the editor. (All of this is speculation, haven't actually tested.) \$\endgroup\$ Commented Dec 21, 2016 at 7:27
  • \$\begingroup\$ @JohnHamilton In the editor \$\endgroup\$
    – Evorlor
    Commented Dec 21, 2016 at 20:35
  • 2
    \$\begingroup\$ If I'm not wrong, prefabs are static in unity, so there's a high chance they are being loaded in the beginning, so when you load your terrain, it's already in memory. \$\endgroup\$ Commented Mar 22, 2018 at 17:07
  • \$\begingroup\$ I'd second Tom here - it definitely seems like the editor just had it in memory for some reason. You could try building it and measure the time then. Then maybe do a restart of PC and run build and then run project in editor. If there is some discrepancy we could make more accurate predictions as to when the optimization occurs. \$\endgroup\$ Commented May 20 at 9:20

3 Answers 3

0
\$\begingroup\$

So the original question "why do prefabs speedup loading" is an very interessting question.

You may check out the memory part of the profiler when working with prefabs.

Every asset you once prefabed will use memory for reference. Even when you unload and destroy the prefab - there is still some information kept in the memory (as an example the texture part of the prefab).

When you load the prefab unity will access this information by reference and speedup loading.

When your project getting very large though it could eat up your memory even when you destroy thous prefabs. This also applies to prefabs whch are currently not loaded into scene.

You should check out Unitys Addressable Asset for qualified information.

Greetings :)

\$\endgroup\$
-2
\$\begingroup\$

What are Prefabs

The Unity Documentation for Prefabs simply describes it as

A template from which you can create new object instances in the scene. Any edits made to a prefab asset are immediately reflected in all instances produced from it but you can also override components and settings for each instance individually.

How does it effects performance

My experience tells, it does not. Since google does not point to any rigid analysis that whether or not prefabs improve performance nor I have done any experimentation regarding this, this Unity answers.com question explains it like this:

There's nothing magic about prefabs; they are simply a convenience. There's no performance difference between putting two cubes in a scene, or making a prefab out of the cube and then putting two cube prefabs in the scene.

That's all. If there is any performance improvement reports or analysis or anything regarding this, I might want to know about it as well. Thank you.

EDIT: I tried what you said, made a terrain of 50k by 50k with heightmap resolution of 512 (couldn't get any higher).

  1. Pressed play button first ~ 5seconds
  2. Pressed play button second time ~ 4-5 Seconds(roughly)
  3. Prefabbed it and pressed Play ~ 4-5 Seconds
  4. Waited for a while, may be 5minutes and pressed play 2-3 seconds

Time difference was not significant but here's what I noticed. Whenever you change Terrain settings, on the bottom right corner, there is some processing happening. If you let that complete, the startup time would decrease.

Here's what it looks like:

enter image description here

I didn't know your terrain settings and how complex it was but from this I can deduce that bottom-right processing might be the thing that manages the load time. It might be loading in the memory and could be doing some one-time calculations/ processing.

\$\endgroup\$
5
  • 3
    \$\begingroup\$ This is good information, but it doesn't explain the phenomenon that OP describes. Any idea what causes their scene to load substantially faster when pressing play in the Editor after they'd saved their terrain as a prefab? \$\endgroup\$
    – DMGregory
    Commented Nov 18, 2018 at 16:07
  • \$\begingroup\$ Edited the answer. Might help now \$\endgroup\$ Commented Nov 25, 2018 at 18:19
  • 3
    \$\begingroup\$ FYI, that process bar you're seeing is the asynchronous light baking. \$\endgroup\$
    – DMGregory
    Commented Nov 25, 2018 at 19:05
  • \$\begingroup\$ Then I guess I'm not sure about it because I tried out the same thing, but there was literally no change in time for me. \$\endgroup\$ Commented Nov 26, 2018 at 0:34
  • \$\begingroup\$ Ignore that 'clustering 5/11 jobs' comment. That is unity baking Light Maps and has NOTHING to do with the prefab performance increase you are discussing. I can't comment on Prefab performance, but would like to know if they do help. I hope they do, it seems like it would make sense. Any information you can share on this would be appreciated. \$\endgroup\$
    – AllanMac
    Commented Jan 27, 2020 at 18:43
-5
\$\begingroup\$

Prefab is just for reusability and for a repeatable task it doesn't have any connection with preprocessing. When rendering process happens with any game objects and it has any retrace to the project panel unity make its meta file and store some information on it and for next time Unity uses that information for next time.

\$\endgroup\$
2
  • 6
    \$\begingroup\$ I have attempted to edit your question for readability.. but you completely lose me, after the first sentence. "When (the?) rendering process happens with any gameobject(s).." is about as far as I get before I can not make any sense of this answer. I believe you mean well, but an illegible answer is simply unhelpful. \$\endgroup\$
    – Gnemlock
    Commented Jun 15, 2017 at 9:36
  • 1
    \$\begingroup\$ Sorry for the inconvenience i can try to improve my answer. \$\endgroup\$
    – ramesh
    Commented Jun 15, 2017 at 20:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .