Search Unity

Make SceneAsset full time citizen asset in the Runtime

Discussion in 'General Discussion' started by katoun, Feb 26, 2019.


Would you like to reference SceneAssets like all other assets?

  1. Yes please.

  2. No. Leave it like it is.

  1. katoun


    Dec 26, 2012

    I was trying for some time to create a scene-state management system, where I can map a GameState class to a specific SceneAsset. I created a ScriptableObject where I have this mapping data. Then I made a root scene where I have the StateManager and the reference to the mapping data. From this test I found that the Unity's scene management is a bit inflexible:
    - SceneAsset is only available in the UnityEditor API but not in the UnityEngine, even thought it can be serialized as a UnityEngine.Object which is the base of all assets.
    - SceneManager.LoadScene[Async] has only 2 ways to identify a scene asset: by the index in the Build Settings scene list or by the scene name/path. No option to identify a scene by a unique identifier that is not dependent on the scene name, path in the project's folder or any ordering.
    - Any SceneAsset that is not included in the BuildSettings scene list, even thought is it referenced in the main scene like any other asset, does not get included into the build.
    Because these limitations, I had to make some workarounds. Added a script that hooks to the build event from where I had manually injectall the referenced SceneAssets to the Build Settings scenes list and map GameStates to the corresponding scene name/path. Also, on any scene asset change of it's name or path, I had to re-save the ScriptableObject data to map GameStates to the new scene asset's path.

    My suggestion is not to completely change the way current system works. Let the scene list from the build settings work as it is now, but also let SceneAsset be available in the runtime API and also contribute the the build process as all the other assets do. So, if there is a scene in the build settings that inside has a game object with a script that contains a reference to a SceneAsset, that SceneAsset gets added to the BuildSettings scene list at build time (transparent for the user), and can be used with SceneManager.LoadScene[Async] by it's SceneAsset reference. As I know, each asset has an unique identifier at the runtime(after the build process) that is based on the guid (witch is the Editor global identifier of a file) and it's local fileID (to identify multiple assets that can be contained in one file: fbx has skeleton, geometry, animations and materials).