Search Unity

Feedback Allow SceneAsset to be used outside the Editor

Discussion in 'Editor & General Support' started by Peter77, Apr 20, 2019.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    SceneAsset is currently stored in the UnityEditor namespace, thus not available outside the editor.

    Having SceneAsset available in a Player would allow to set up scene loading in a more robust way. Looking at Unity projects today, I was able to observe four different approaches to load scenes:
    1. Use scene name
    2. Use scene build index
    Using the build index is the approach that breaks the easiest, followed by using the scene name. If you rename the scene, your game breaks.

    The third solution I've seen is to build a database that maps assetGUID's to scene and asset names at edit-time, which is then used at run-time to resolve names via their guids. Thus, rather than using the scene name or build index, the assetGUID of that scene is used to look up its name at run-time in the earlier created database, then use the name to load the scene.

    The last workaround I have seen is to create a "SceneId" ScriptableObject that just contains the scene name and use this one to wire up scenes. Game code then reads the entered scene name from that "SceneId" to load the scene. Again, this does not really solve the problem when renaming a scene. The advantage here would be that you only need to maintain the scene name in two different places.

    The third option is the one that allows to rename scenes and is in my opinion the most robust approach. Yet it baffles me that we need to build this database, because there is already this SceneAsset thing in the editor.

    If SceneAsset would be available in a Player and allows to get the scene name for example, that would already be super useful to laod scenes.

    Having SceneAsset available outside the editor, would allow us to create more robust Unity projects, that don't fall apart if a scene is being renamed.

    TL;DR: Please move SceneAsset to the UnityEngine namespace.
     
    Last edited: Apr 21, 2019
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
  3. alfish

    alfish

    Joined:
    Apr 5, 2017
    Posts:
    26
    See my post here for implementation of 3rd solution above (except it maps GUID->build-index).
    The code is quite simple (just 4 classes). Just add the code and mapping is included automatically at build time.
    If you want to use the name SceneAsset instead of SceneReference, you can just rename it (and maybe even put it under UnityEngine namespace), though I don't recommend it.