Search Unity

EditorOnly object still included in the build

Discussion in 'Editor & General Support' started by Xriuk, Apr 8, 2021.

  1. Xriuk

    Xriuk

    Joined:
    Aug 10, 2018
    Posts:
    21
    I'm tring to load additional scenes in the build of my game, in the Editor everything worked "good" so far, so I wanted to test the build.

    After adding all the scenes to the build list when I launch the game it does not load the additive scenes but from the logs it looks like it's continuously reloading the main scene, and I can't figure out why...
    I tried loading the scene by name, by build index, but nothing seems to help.
    I also tried the async version, coroutines, nothing works.

    So I have a scene called Player (build index 0), with my character, camera + other stuff. I created a script attached to che character which should load the second scene (build index 1) when the game starts:
    Code (CSharp):
    1. public class LoadPlayerOnAwake : MonoBehaviour{
    2.         private void Start() {
    3.             Debug.Log("Loading");
    4.             SceneManager.LoadScene(1, LoadSceneMode.Additive);
    5.         }
    6.     }
    In the editor it works, I get my second scene loaded and in the console appears "Loading".
    When I build the game and run it nothing happens. And if I take a look in Player.log, here's what it prints:
    Code (CSharp):
    1. Mono path[0] = [...]
    2. Mono config path = [...]
    3. Initialize engine version: 2020.2.0f1 (3721df5a8b28)
    4. [Subsystems] Discovering subsystems at path [...]
    5. GfxDevice: creating device client; threaded=1
    6. Direct3D:
    7.     Version:  Direct3D 11.0 [level 11.1]
    8.     Renderer: NVIDIA GeForce RTX 2070 (ID=0x1f10)
    9.     Vendor:
    10.     VRAM:     8031 MB
    11.     Driver:   27.21.14.6140
    12. Begin MonoManager ReloadAssembly
    13. - Completed reload, in  1.782 seconds
    14. D3D11 device created for Microsoft Media Foundation video decoding.
    15. <RI> Initializing input.
    16.  
    17. New input system (experimental) initialized
    18. <RI> Initialized touch support.
    19.  
    20. UnloadTime: 0.781800 ms
    21. Loading
    22.  
    23. Loading
    24.  
    25. Loading
    26.  
    27. Loading
    Plus various exceptions following. But apparently my main scene Player gets reloaded many times.

    I use Addressables too, but I think scenes don't have to be included in the addressables build, right?

    Any help?

    Unity 2020.2.0 with HDRP
     
  2. Xriuk

    Xriuk

    Joined:
    Aug 10, 2018
    Posts:
    21
    Ok, so apparently something really weird happens: in the second scene I have an object marked as "EditorOnly", which loades some scenes on Awake (even in Edit Mode) if they aren't loaded already, including the Player scene (but only if it is not loaded, which it is as it's the main scene of the build).
    But apparently the object is still being included in the build, but silently.
    I tried Debug.Log inside Awake of this script and in the build nothing prints but still the Player scene gets reloaded somehow... if I disable this script before building then everything is fine.
    Any idea?
     
  3. Xriuk

    Xriuk

    Joined:
    Aug 10, 2018
    Posts:
    21
    I think I solved this problem, but for the sake of my own sanity I would like to figure our what's going on, I have this class which I use to automatically load a list of sub-scenes for a certain scene. In my case I'm using it to load the Player scene (with my camera + other stuff) everytime I open any scene.
    Code (CSharp):
    1. #if UNITY_EDITOR
    2. using UnityEditor;
    3. using UnityEditor.SceneManagement;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6.  
    7. namespace StoneShelter {
    8.     [ExecuteInEditMode]
    9.     public class ChildScenesLoader : MonoBehaviour {
    10.         // Configurable variables
    11.         [Tooltip("If true will load the scenes as soon as this scene is loaded and will unload when being unloaded")]
    12.         [SerializeField]
    13.         protected bool m_autoLoadAndUnload = false;
    14.         [Tooltip("Names of the scenes to load")]
    15.         [SerializeField]
    16.         protected SceneAsset[] m_childScenes = null;
    17.  
    18.         // Protected members
    19.         protected bool m_skipFirst = false; // If true, when unloading will skip the first valid scene
    20.  
    21.  
    22.         public void LoadScenes() {
    23.             if(EditorApplication.isPlayingOrWillChangePlaymode || m_childScenes == null)
    24.                 return;
    25.  
    26.             foreach(SceneAsset scene in m_childScenes) {
    27.                 if(scene == null || EditorSceneManager.GetSceneByName(scene.name).IsValid())
    28.                     continue;
    29.                 Scene sc = EditorSceneManager.OpenScene(AssetDatabase.GetAssetPath(scene), OpenSceneMode.Additive);
    30.                 if(!sc.IsValid())
    31.                     Debug.LogError("Scene cannot be loaded: '" + scene.name + "'!", gameObject);
    32.             }
    33.         }
    34.  
    35.         public void UnloadScenes() {
    36.             if(EditorApplication.isPlayingOrWillChangePlaymode || m_childScenes == null)
    37.                 return;
    38.  
    39.             foreach(SceneAsset scene in m_childScenes) {
    40.                 if(scene == null)
    41.                     continue;
    42.                 Scene sc = EditorSceneManager.GetSceneByName(scene.name);
    43.                 if(!sc.IsValid())
    44.                     continue;
    45.                 if(m_skipFirst) {
    46.                     m_skipFirst = false;
    47.                     continue;
    48.                 }
    49.                 EditorSceneManager.CloseScene(sc, true);
    50.             }
    51.         }
    52.  
    53.         protected void Awake() {
    54.             if(m_autoLoadAndUnload && !EditorApplication.isPlayingOrWillChangePlaymode)
    55.                 LoadScenes();
    56.         }
    57.  
    58.         protected void OnDestroy() {
    59.             if(!m_autoLoadAndUnload || !EditorApplication.isPlayingOrWillChangePlaymode)
    60.                 return;
    61.  
    62.             int count = 0;
    63.             foreach(SceneAsset scene in m_childScenes) {
    64.                 if(scene == null)
    65.                     continue;
    66.                 Scene sc = EditorSceneManager.GetSceneByName(scene.name);
    67.                 if(!sc.IsValid())
    68.                     continue;
    69.                 count++;
    70.             }
    71.  
    72.             if(EditorSceneManager.loadedSceneCount == count)
    73.                 m_skipFirst = true;
    74.  
    75.             UnloadScenes();
    76.         }
    77.     }
    78. }
    79. #endif
    This script is placed on a static GameObject tagged with "EditorOnly", so teorically it should be removed in the build.
    My problem comes when I check
    m_autoLoadAndUnload
    and then build. It looks like the code is still being executed as it reloads the Player scene continuously while playing thus provoking a loop.