Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to start any level/scene in Unity editor (with game logic loaded first)?

Discussion in 'Scripting' started by kimcgm, Oct 7, 2014.

  1. kimcgm

    kimcgm

    Joined:
    Jul 30, 2013
    Posts:
    11
    Hi there,

    I have been trying to find a way to start any level in Unity editor with some prerequisite GameObject from another scene.

    The idea I have is to store all my common objects (singleton, in-game ui, audio manager, etc) in a scene that doesn't get destroy between scenes loading. I can control the load order if user starts the game from the beginning.

    I wanted to have the ability to start any level in the editor with the common objects loaded before hand.
    The purpose is to save development time later.

    I have hooked into EditorApplication.playmodeStateChanged, so I can trigger my code to Application.LoadLevelAdditive().

    For Example:
    Code (CSharp):
    1.  
    2. [InitializeOnLoad]
    3. public class EditorHelper
    4. {
    5.     static EditorHelper()
    6.     {
    7.         // Add Play Mode Change Hook
    8.         EditorApplication.playmodeStateChanged += HookPlayModeChangeOnce;
    9.     }
    10.  
    11.     static void HookPlayModeChangeOnce()
    12.     {
    13.         if (EditorApplication.currentScene != "Assets/Scenes/myEngine.unity")
    14.         {
    15.             Application.LoadLevelAdditive("myEngine");
    16.         }
    17.  
    18.         // Remove itself.
    19.         EditorApplication.playmodeStateChanged -= HookPlayModeChangeOnce;
    20.     }
    21. }

    When I hit "play" in the editor, the level's scene gets loaded first before myEngine scene.
    The problem is the level script has some dependency on myEngine which hasn't been instantiated yet.

    Is there a way to know or to force myEngine scene to be loaded first?
    I have tried the ProjectSettings>Script Execution Order, but doesn't seem to work.

    What do you guys do? Any suggestion?

    P/S: It's been many years since I last wrote a line of code so I'm quite rusty in this field. And I'm very new to Unity.
    kimc.