Unity – How to run a long task without freezing my game?

I have a component loaded to generate a level at run time, an operation that takes a long time. As a result, I experience a crash when this operation is executed (when the scene is loaded).

This problem can be summarized by the fact that SceneManager.LoadSceneAsync is actually do not async at all in its second phase, when it initializes all objects.

I have researched and thought about three approaches:

Cutting of the operation as explained on this video:

This is not trivial, I should rewrite pieces of code to avoid a few seconds of hiccups loading the scene. Moreover, the person explaining in the video said to have modified the source code of Unity to which I do not have access.

Running the operation in a background thread:

It's really interesting, it would require minimal changes. The only problem is that at some point I have to create Unity objects (GameObject, Mesh etc) and this is not possible outside the Unity thread.

I was thinking more or less about this code to be executed on the background thread:

Builder class
empty public Build ()
// 1. do something long

// 2. create a Unity object with the result of # 1
// something like that
// wait for RunInUnitySynchronizationContext (() => new GameObject (...))

// 3. do extra things on the result of # 2

The fact is that I do not know at all how this second step should be written, if possible.

Use a non-animated Loading screen and wait for it to finish:

This is the simplest and effectively eliminates the problem by simply "hiding" it.


What is an effective approach to solve this problem?