Search Unity

Could Unity use a Root Scene?

Discussion in 'General Discussion' started by Arowx, Nov 20, 2018.

  1. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    In most programming languages there is a root function or/class e.g. main().

    One of the issues with Unity is that it is the root of your game, which is great as Unity does a lot for you but without a scriptable root we end up having to do weird hacky things (DontDestoryOnLoad(), PlayerPrefs) to keep the core game mechanics and data , players state and even UI/controllers between scenes.

    It's one of the first 'hacky' problems you hit developing with Unity.

    So could Unity use a Root Scene?

    Note ECS has a default world. Is that like a root scene, can the base world be kept between scenes?
     
    Last edited: Nov 20, 2018
  2. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,160
    No. Do it yourself.
     
  3. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    I Do every time I write a multi-scene game and it's clunky and inelegant.

    Could an optional Root Scene make multi-scene game development simpler and more elegant?
     
  4. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,160
    No.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You can already do a root scene. Just make it the first scene you start, and only use additive scene loading for other scenes. I think DontDestroyOnLoad is generally better though, because you're only keeping around the objects you actually need to keep around. It is only 1 line of code, so I've never considered it a weird hacky thing.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I actually prefer this approach. I find DontDestroyOnLoad to be about as subtle as a sledgehammer. I want this thing to hang around through a couple of scene loads, so I am going to make it immortal. No one shall ever destroy it! Muh, hah, hah, hah. Seriously, its such an evil villain scenario.

    With additive loading one can apply more subtlety. DestoyOnGameExit, DestroyOnPlayerChange, DestroyOnGameSessionEnd, DestroyOnLevelEnd, DestroyOnLeavingArea. Or so on based on the needs of your specific game design.
     
  7. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,536
    Indeed, make a 'preload' scene, do all your init and singleton stuff, then additive load every other scene. Be sure to assign the new scene as the main scene when you do.

    There's also this.
     
  8. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    @Arowx

    The scene is the root scene.

    You know what multi scene is all about in Unity, I assume? :p Unity and everyone is trying to make things less dependant on a root scene. Because the concept of a root scene is the same as having main.cpp - it's invasive to have to go through it. You can make it like this if you want.

    The rest of us are asking Unity to go the opposite way, make things less dependant on scenes so we can stream stuff and so on. Current issue is the lighting setup - it's locked to scene. Probes are locked to a master scene etc... very limiting.

    So no, I don't want an even more root than root scene.

    I currently have a master scene where things are pooled and the lighting is baked with, and a bunch of other scenes which are loaded in and out using multi scene to manage editor perf, authoring and so forth.
     
    Ryiah likes this.
  9. ClaudiaTheDev

    ClaudiaTheDev

    Joined:
    Jan 28, 2018
    Posts:
    331
    I also did it with one ''preload' scene containing the singelton things. The other scenes i am loading additive.
    Im am far away from a unity pro (started this year) but i am a good programmer. Actually i found it hard to implement/work out my solution and read many tutorials.
    For my needs a root scene would have been very helpful!
    I think unity has a lot of intuitive features but in my opinion scene loading ist sadly not one of them... perhaps i only think so becauce i just suck at unity ;-)
     
  10. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    Slightly of topic, but have you had any success using occlusion culling with this approach? I'm currently trying to get this to work.
     
  11. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I haven't actually noticed any problems with that but I bake occlusion with everything visible so that's probably why...
     
  12. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    So you activate all your scenes together on the inspector and bake it?
     
  13. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    My toolchain bakes it, from the menu. It loads up all scenes which have geometry and calculates a lot of things from probes to navmeshes to lighting and occlusion.

    I haven't got around to this part being a bottleneck yet so I've not investigated too deeply (sort of new ish to multi scene myself).
     
  14. nhold

    nhold

    Joined:
    May 2, 2017
    Posts:
    50
    I don't know about scene but your initial point makes sense. I could see it being interesting being able to override or extend the program entrypoint (main) in some manner.
     
  15. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    People talk additive scene loading or dontdestory. we use a third option, RuntimeInitializeOnLoadMethod and scriptable objects.

    Code (CSharp):
    1.       [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
    2.       static void CreateManagers()
    3.       {
    4.             Steam = Object.Instantiate(Resources.Load<SteamManager>("SteamManager"));
    5.             Settings = Object.Instantiate(Resources.Load<SettingsManager>("SettingsManager"));
    6.       }
     
    hippocoder and Ryiah like this.
  16. Kiupe

    Kiupe

    Joined:
    Feb 1, 2013
    Posts:
    528
    Stupid question, using the "RuntimeInitializeLoadType.BeforeSceneLoad" parameter, what can be accessed from the method ? I mean, the scene is not loaded yet, so can the class access something in the (not loaded) scene hierarchy ?
     
  17. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    You can add stuff to the scene only. Which I dont only load stuff that should live during the entire game session. (ie its perfectly fine doing new GameObject("my objects"); it will be added to the loading scene)
     
  18. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Thank You.

    As you can see nearly everyone has a different approach or take on this idea, that's because it's the solution we find to make our own version of the Unity 'root' problem.

    An optional root scene or script or class or world, gives a hook where the developer can choose to place global game data, code and scene elements.

    Imagine if there were a root component you could just drop on an object and it would be guaranteed to be the first thing to exist and run in your game and to live through as many scene loads as you choose...
     
  19. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,160
    Everyone has posted different solutions that fit their own specific needs.
     
    Lurking-Ninja likes this.
  20. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    We already have that...
     
  21. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yeah, that exists now. It's just not forced on people.
     
    Antypodish likes this.
  22. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    My point is it could be made simpler and easier...

    Look at the Unity UI it lets you quickly build up a level and with a few lines of code and/or standard assets you can have camera movement and player interaction.

    Now think about how you had to work out how to keep things going between scenes, something as simple as a post game score screen becomes a head scratcher as the score was in your game object and UI which vanishes when you move between scenes.

    Should every Unity developer have to search for and find the DontDestroyOnLoad Method or could a simple in UI root component or tick box make it easier?
     
  23. I'm pretty sure it will be part of the upcoming visual scripting madness, so you can click your application together, including dontdestroyonload if you choose to do so.
     
  24. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,160
    It is not complex enough for people who work at that level (the only people who would see any benefit from the concept of a root scene) that it makes sense to make it a full, official paradigm.
     
    Ryiah likes this.
  25. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I have to avoid the Unity UI because it offers no real benefit and has low performance. I'm still struggling at the rationale behind a lot of it if I'm honest. Shifted to just using textmesh pro with my own suitable textured objects. Works better and is simpler for me.

    You are advocating one way of working but that might not be suited to a lot of scenarios, since it is one way of working.
     
  26. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,203
    I don't think he's referring to the Unity UI. I believe he's referring to the Unity editor interface.
     
  27. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Ah so he is. but I don't understand any of what is asked for. A scene is exactly that. Using multi scene you can if you choose, use it like building blocks. Or the nested prefabs (which are technically scenes as far as I know).
     
  28. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    You treat it as if it's a problem with Unity, but it's not. It's a design decision that every piece of software has to make based on its own requirements.

    If you're using Unity then it's going to have an impact on the specifics of those requirements. Unity not addressing your specific project design preferences is not a "problem" with Unity.
     
  29. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    At some point you have to accept the engine is designed for professionals, not for beginners. Its nice to have a beginner friendly engine. But that's not really the point.
     
  30. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    But is already this simple. I'm not seeing how it could be made easier without forcing every other project to be harder.
     
    Antypodish, Kiwasi and zombiegorilla like this.