How to reference unity Scene types?

I’m trying to make level editing and selection easier for my designers and I want to know if there’s a way to reference unity scenes in a script. It seems you can because there is a Scene object, but in my GameManager singleton when I set those as public, the variables are not being serialized in the editor for drag’n’drop assignments. Is there a way to make that so?

Just storing a path string (as the SceneAsset documentation suggests) is inadequate for production, because if the scene file is renamed you’ve lost your reference. We can use an Object reference to the SceneAsset but we no longer have access to AssetDatabase to look up the path when not in editor.

To reliably handle both file renames and runtime paths you need two serialized pieces of data (Object reference in editor, string path at runtime). You can create a SceneReference object that uses ISerializationCallbackReceiver to ensure that the stored path is always valid based on the specified SceneAsset Object.

As an example, here is my approach to this problem: [Unity3D] A Reliable, user-friendly way to reference SceneAssets by script. · GitHub

alt text


Features

  • Custom PropertyDrawer that displays the current Build Settings status, including BuildIndex and convenient buttons for managing it with destructive action confirmation dialogues.
  • If (and only if) the serialized Object reference is invalid but path is still valid (for example if someone merged incorrectly) will recover object using path.
  • Buttons collapse to smaller text if full text cannot be displayed.
  • Includes detailed tooltips and respects Version Control if build settings is not checked out (tested with Perforce)

alt text