10
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ I think Unity is already loading your whole application when the splash screen is visible. I don't think what you want to do is possible, but I would love to be wrong this time. \$\endgroup\$ – Hellium Nov 9 '16 at 23:31
  • 2
    \$\begingroup\$ This feels like a bit of an XY problem: why do you want to load stuff during that screen? \$\endgroup\$ – doppelgreener May 21 '17 at 16:24
  • 1
    \$\begingroup\$ Anything you put in the first scene of your game is loaded while that splash screen is up, right? So, would it not suffice to include the content that you want to load in that first scene (either off-camera, disabled, or merely referenced if you don't want that content visible immediately)? Or are you trying to load content from another source, like a web server? \$\endgroup\$ – DMGregory May 21 '17 at 17:14
  • \$\begingroup\$ @DMGregory Great point. I updated the question. I am trying to run code. \$\endgroup\$ – Evorlor May 21 '17 at 17:16
  • \$\begingroup\$ We understand you're trying to run code (loading scripts are necessarily code) -- but what code and why? You've got a problem and you've decided the solution is to run that code during the Unity splash screen; why is that? \$\endgroup\$ – doppelgreener May 28 '17 at 13:31
9
+50
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 3 seconds before the end, you mean, one second before the start? \$\endgroup\$ – Jacob May 22 '17 at 7:43
  • \$\begingroup\$ In my experiments, my script gets its first Update() tick 2.9 seconds after the constructor fired. That's about all I can discern with the current instrumentation. \$\endgroup\$ – DMGregory May 22 '17 at 8:44
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Double load screens is what you are suggesting? \$\endgroup\$ – Evorlor May 21 '17 at 16:39
  • \$\begingroup\$ Essentially yes. It's the way to do it with Unity. Put a splash/loading screen for your game or some studio logo animation while it loads the next scene. \$\endgroup\$ – Stephane Hockenhull May 21 '17 at 23:37
  • 1
    \$\begingroup\$ @Evorlor this is what I do with my game and in works perfectly fine. Although this doesn't let you utilize the time that Unity intro takes, since that intro can be pretty short, you can just make a loading screen (however generic) and use that for yours. \$\endgroup\$ – John Hamilton May 23 '17 at 13:04
-1
\$\begingroup\$

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:

  1. Splash screen (no image here, just make it black)
  2. Splash scene (small footprint with your initial code and next scene loader)
  3. Intro scene (regular intro scene)
  4. .... (other scenes as usual)

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);
    }
}
\$\endgroup\$

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.