Can I load data in the background while the Unity splash screen is up? If so, how?
I am trying to run code during the splash screen, not just load assets.
I have not been able to find any information on this in Google or the Unity Documentation.
Can I load data in the background while the Unity splash screen is up? If so, how?
I am trying to run code during the splash screen, not just load assets.
I have not been able to find any information on this in Google or the Unity Documentation.
I just did some tests, and I found that the constructor for objects in the first scene actually gets called while the splash screen is still displayed.
Here are some timings I measured with a static Stopwatch:
Constructed 0 s
Awake 2.118 s
OnEnable 2.12 s
Start 2.744 s
First Update 2.919 s
So, it looks like we can trigger a script to run nearly 3 seconds before the end of the splash screen (with default settings) by firing it off from the constructor. You can try triggering your loading code (possibly on its own thread) from there.
Just beware that this is probably being called from the loading thread, so since we're not on the main thread we'll be a bit more limited with what we're allowed to do here. If there's a specific thing you're trying to do that's not supported on the loading thread, consider posting a new question with details of your application and we can try to find a workaround for that case.
Update()
tick 2.9 seconds after the constructor fired. That's about all I can discern with the current instrumentation.
\$\endgroup\$
Your best bet is to make your own loading screen and set that as the initial scene. Then, from that simple scene, use the Scene Manager to load the next scene in the background, asynchronously.
I cannot find information to confirm this, but from my observation, it seems Unity is loading your initial scene while the splash screen is being shown.
On some platforms, the game starts to run before the splash screen goes away, if loading is fast enough.
All this can change at any time, on any platform, on any version update. There is no guarantee that this will be consistent, so you're better off using your own loading screen.
Short answer is no, you can not do that.
Both above answers are correct in terms of scene management. You can use Scene Manager to load the scene and since it is async you can run other code in parallel.
Look at SceneManager.LoadScene documentation for more details.
The closest you can get to running your code as fast as possible after game starts is running it from the first scene with tiny footprint. Let's call it Splash Scene. The only thing it would contain is your splash image and controller running needed code and loading another scene.
Since it has small footprint, it will load very fast and thus make your code running almost instantly. You can then do what you have to do, and load Other Scene afterwards.
Another thing you can try is removing "splash screen" remove any images you would want to display there. It will be just a bit faster since it won't be loaded initially.
So the flow would be something like this:
Smaller the scene is faster it will load. You can use something like splash controller attached to any object in loaded Splash screen.
using UnityEngine;
using UnityEngine.SceneManagement;
public class SplashController: MonoBehaviour
{
void Start()
{
// RunSomeSyncMethod();
SceneManager.LoadScene("OtherSceneName", LoadSceneMode.Additive);
}
}