1
votes

I'm building a mobile game, and my game should contain

  • Loading Screen with progress

  • Lobby screen

  • Game screen (with loading screen before the game loads)

No special levels in the Game...

Notice that I need to be able to share GameObjects between the scenes, for example my Network handler, which holds a connection to my servers.

How will you construct the scenes for this? What is the best practices for scenes creating?

I can thought of the following options:

  1. Two Scenes: Loading Scene + Main Scene and share GameObjects:

    The loading scene will be very lightweight scene, which will only be exists for loading the Main Scene. It can show a progress bar according to the progress of the main scene loading and the network login process.

    Pros: The application will be loaded fast (showing the loading scene with the progress)

    Cons: I will have to share my Network handler between the scenes (Holding the connection after a login).

  2. Two Scenes: Loading Scene + Main Scene without sharing the GameObjects:

    I would like to be able to start loading the main scene async, but without switching to it automatically. I want that the main scene will start the login process for example, and only when finished all the initialization tasks it will notify the Loading scene that it is ready to be switched. Is this possible? Can I load the scene in background and actually do the switching on demand? Also I will need to be able to get the progress from the main scene in order to show it in the loading progress bar.

    Pros: No GameObjects sharing is required - clean and isolated code.

    Cons: I'm not sure that Unity has this ability....

  3. One scene:

    Pros: All the shared GameObjects are in one place

    Cons: Very slow loading time of the application (Unless there is an option to tell unity to ignore the loading of several GameObjects and then I could load them during the Loading screen showing time).

Thanks!

2

2 Answers

1
votes

I would recommend having three different scenes for the different screens. You might have several "menu screens" in the "menu scene".

For sharing gameobjects, you can set "DontDestroyOnLoad" on the obect you want to share. From the docs:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Awake() {
        DontDestroyOnLoad(transform.gameObject);
    }
}

https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

For option 2: I guess the scene won't switch to the main scene until it's finished loading. However, there might be a bit of a lag while it's loading, which might show if you're displaying a loading animation (a spinner, for instance).

It's also possible to use SceneManager.LoadSceneAsync, which will load the scene in the background. I haven't got personal experience with that one myself, but might be helpful to keep the loading screen updating while main screen is loading. https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html

1
votes

I would have 3 different scenes (one for each screen), and switching from scene to scene with the SceneManagement.LoadScene() method of UnityEngine.SceneManagement.

To share GameObjects between scenes, and more generally to share data, you have 3 options:

  • use a static class: good to share values
  • use DontDestroyOnLoad: be careful not to have duplicate objects when using that
  • store/load data in a file

If you want details on how to do and the differences between these 3 solutions, I advise you this tutorial : https://www.youtube.com/watch?v=WchH-JCwVI8 It is one hour long, but very useful and complete, everything is there.