Prefabs.MergePrefabs on entering PlayMode takes almost 4 minutes

Cross post from the unity forums where the issue is being ignored.

My project takes almost 4 minutes to go into PlayMode. Same goes for going out of PlayMode.
What’s weird about this is that this just started on my personal dev branch while it affected my colleague’s and the development branch much sooner. So this seems to be tied to something happening with the actual scene.

Deep Profiling the process of entering PlayMode, I was able to get rid of a few logic loops that might’ve hindered my scripts. However, I DO need some tips on dealing with the elephant in the room.

The Prefab.MergePrefab call at start.

The weird thing is that the time allocated for the call in question is not the sum of its childrens’ times. There is a LOT of functionality in this one call that is completely hidden from the user behind its name.

Does anybody know what is happening here and how to reduce it?

Bump.

After some deliberation and trial-and-error, we have isolated the problem to definitely have something to do with the handling of prefabs in our scene. (This might seem like a no-brainer, but really having a definite focus for troubleshooting is a big step towards a solution)

There is an object in our hierarchy called “Forests”. This is the hierarchy of its children:

Forests
| Phase0
  | forest-patch-0
    | treetype0 (prefab)
    | treetype0 (prefab)
    | ...
    | treetypeX (prefab)
  | forest-patch-1
    | ...
  | ...
  | forest-patch-n
| Phase 1
| ...
| Phase n

Because there is still not a good solution to merging large scenes, we decided to prefab the “Forests” object in its entirety in order to decouple it from the scene proper. This seems to be a grey area for Unity in regards to how it then tries to handle the “nested prefabs”. In some way, the prefab instances of the individual trees get broken but in some ways, Unity still seems to recognize them on entering/exiting PlayMode and “do stuff” to them, encased in a call to Prefab.MergePrefabs, whatever that is.

Today, I broke the “Forests” prefab in the hierarchy and replaced all tree objects with their dedicated prefabs. Now play mode takes only about 10 seconds to start and exiting takes about the same time. This is a humongous improvement to the 340 seconds I started with.

Now. This still doesn’t explain why my branch was still working fine even with “Forests” still being a prefab in my scene but I’m not gonna be complaining about results here.
I’ll keep an eye on this and update accordingly.