Search Unity

Loading Scene and Main Scene

Discussion in 'Scripting' started by tomerpeledNG, Sep 13, 2017.

  1. tomerpeledNG

    tomerpeledNG

    Joined:
    Jul 14, 2017
    Posts:
    81
    Hi,

    I'm building a mobile game, and my game should contain Loading Screen with progress, Lobby screen and 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:
    This 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. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    In all scenarios, I'd also add a "Startup" scene, where you create your "DontDestroyOnLoad" singleton-like objects there. That way, it only gets loaded once during the entire app lifetime without accidentally creating a new instance of those objects that you're forced to destroy and throw away.

    As for the network manager, I actually don't use Unity's implementation, so I'm free to keep it existing across scenes (since it's my class, I control its lifetime). Even so, I still put it in the main/game scene where it can silently connect you in the background while you're exploring (for a bit) in single player mode. When I quit to the main scene, it disconnects me from the network, and when I re-enter the game (could be a different level), I'm starting with a fresh clean new network manager.

    As for the rest... well, at some point you may eventually have to share something (like player name, character config options, what save game to load, etc.), depending on your game design. I'd focus more on what you actually need to share rather than trying to design the entire app around sharing or not sharing. Anything you don't need to share, don't share it. Like my network manager for example -- because I didn't need to share it, I don't share it.
     
    tomerpeledNG likes this.
  3. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Oh, and if you're going to start splitting the app into multiple scenes (like I did), you'll find this tool extremely useful for keeping your sanity sane: Scene Auto Loader
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    This is what we do (note - not on mobile though). The scene initially has very little in it and contains code hooks to load the first menu (UI canvas). From there everything is UI driven until you actually load up a map and play a match (which is also just a big coroutine to load a bunch of objects hidden behind a loading screen canvas)
     
    tomerpeledNG likes this.
  5. game-rules

    game-rules

    Joined:
    Jan 11, 2014
    Posts:
    45
    I would go also for option 3. It is simpler to setup and with the usage of coroutine, initialization of different objects and states can be easily handled. The advantage also, is that once initialized, players would not have additional delays.

    It is however important to setup a progress bar or some refreshing text with percentage to show the player that the game is not blocked with initial picture.

    --
    Game Rules
    Unified Visual Scripting Asset with rules engine
    https://www.assetstore.unity3d.com/en/#!/content/84383
    https://game-rules.net
     
    tomerpeledNG likes this.
  6. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
  7. tomerpeledNG

    tomerpeledNG

    Joined:
    Jul 14, 2017
    Posts:
    81
    So you load the rest of the scene using AssteBundles/Resources ?
     
  8. tomerpeledNG

    tomerpeledNG

    Joined:
    Jul 14, 2017
    Posts:
    81
    Actually I tried this approach, but go into some difficulties, mainly due to the reason that using allowSceneActivation set to false, doesn't help with the loading of the next scene, because the next scene awake/start functions doesn't get called. Only after freeing the scene loading with allowSceneActivation set to true, the awake/start functions of the next scene get called...
     
  9. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    That's sort of how it has to work; how could you call Start on something if it's not even loaded into the game yet? I think you'll run into this problem no matter what method you use... there's no way to call Start on something until it's actually in the scene.
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Resources, yes.