7
\$\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\$
  • 4
    \$\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 Dec 20 '16 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\$ – John Hamilton Dec 21 '16 at 7:27
  • \$\begingroup\$ @JohnHamilton In the editor \$\endgroup\$ – Evorlor Dec 21 '16 at 20:35
  • 1
    \$\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\$ – TomTsagk Mar 22 '18 at 17:07
0
\$\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\$
  • 2
    \$\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 Nov 18 '18 at 16:07
  • \$\begingroup\$ Edited the answer. Might help now \$\endgroup\$ – Hasnain NoOrani Nov 25 '18 at 18:19
  • 1
    \$\begingroup\$ FYI, that process bar you're seeing is the asynchronous light baking. \$\endgroup\$ – DMGregory Nov 25 '18 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\$ – Hasnain NoOrani Nov 26 '18 at 0:34
0
\$\begingroup\$

additive to others answers. we have flyweight pattern that is concerned with memory and optimization. when you have lots of objects, flyweight pattern, shares the same data in same memory and allocates memory for new or different features. unity3d uses this pattern to work with prefabs.

other approach is in programming, cloning is much faster with less allocation than making whole new object. in c# its MemberWiseClone and its instantiate in Unity

\$\endgroup\$
  • 1
    \$\begingroup\$ Do you have a citation for the claim that prefabs represent a use of the flyweight pattern, and are not simply duplicated into the scene? \$\endgroup\$ – DMGregory Aug 30 at 11:10
  • \$\begingroup\$ @DMGregory i saw some mentions in a tutorial but its obvious. adding external resources to your projects adds up size of your project but we have not so much add up when using many prefabs or scenes, they are not just an instances. just sub objects that share the main prefab. you think unity loads the whole texture and ... of an object for each of them? i believe some type of flyweight certainly is used in unity prefabing \$\endgroup\$ – virtouso Aug 30 at 11:33
  • 2
    \$\begingroup\$ Unity doesn't load a separate copy of a texture for any of its objects, prefabs or no. Textures absolutely use the flyweight pattern. But prefabs include a lot of stuff that isn't textures: GameObjects and Components that have to be copied to make the new instance. It's not obvious why these full copies of objects would be faster to load than ordinary scene objects when testing in the editor. So we need to dig a bit deeper into the guts to find out what's different. \$\endgroup\$ – DMGregory Aug 30 at 11:38
-4
\$\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\$
  • 4
    \$\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 Jun 15 '17 at 9:36
  • \$\begingroup\$ Sorry for the inconvenience i can try to improve my answer. \$\endgroup\$ – ramesh Jun 15 '17 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.