Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

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).

  2. Jean-Fabre


    Sep 6, 2007
    andreiagmu likes this.
  3. frosted


    Jan 17, 2014
    I actually agree w this. Unexpected since these types of posts are usually dumb.

    Having references to scene assets in unityengine instead of editor would make a bunch of stuff easier.
  4. Peter77


    QA Jesus

    Jun 12, 2013
  5. angrypenguin


    Dec 29, 2011
    Yeah, I also think it's pretty weird that we've got a proper object to refer to scenes, but at runtime have to resort to strings or numbers.

    This is the only thing I'm not sure about. I'm not saying I disagree, I'm really not sure either way.

    On one hand, yeah, scenes don't need to be treated differently to any other data. On the other hand, it's quite handy to have one place where we can pick what's included in or excluded from a build. Of course that itself depends on how we implement other things, and there are other solutions which could be more practical anyway...