Search Unity

  1. Unity 2019.2 is now released.
    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.

    83.3%
  2. No. Leave it like it is.

    16.7%
  1. katoun

    katoun

    Joined:
    Dec 26, 2012
    Posts:
    87
    Hello,

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

    Regards.
     
  2. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    344
  3. frosted

    frosted

    Joined:
    Jan 17, 2014
    Posts:
    3,648
    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

    Peter77

    Joined:
    Jun 12, 2013
    Posts:
    4,102
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    12,039
    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...