+// Public Domain. NO WARRANTIES. License: https://opensource.org/licenses/0BSD
+using System.Collections.Generic;
+using Guid = System.String; // could use 128-bit int type to serialize, but it would be less legible in text assets
+///<summary>SceneReferences contains the GUID to build index mapping, used to reference scenes at runtime.</summary>
+///<remarks>This is auto-generated on build as a preloaded asset.</remarks>
+public class SceneReferences : ScriptableObject
+ ///<summary>The singleton instance, automatically preloaded at runtime.</summary>
+ public static SceneReferences instance => asset
+ ?? (asset = ScriptableObject.CreateInstance<SceneReferences>())
+ ; static SceneReferences asset;
+ ///<summary>The GUID for each scene asset, indexed by its build index in the array.</summary>
+ public Guid[] guids => sceneGuids; [SerializeField] Guid[] sceneGuids;
+ Dictionary<Guid, int> cache;
+ sceneGuids = Array.ConvertAll(UnityEditor.EditorBuildSettings.scenes, s => s.guid.ToString());
+ if (sceneGuids == null) sceneGuids = new Guid[0];
+ int n = sceneGuids.Length;
+ cache = new Dictionary<Guid, int>(n);
+ for (int i = 0; i < n; i++)
+ cache.Add(sceneGuids[i], i);
+ ///<summary>The build index of a scene by GUID. It can be used to load it or to obtain scene info.</summary>
+ ///<remarks>All mappings are cached on a dictionary when the asset is preloaded at startup.</remarks>
+ public int SceneIndex(Guid sceneGuid) => cache.TryGetValue(sceneGuid, out int i) ? i : -1;
+///<summary>SceneReference is used to reference a Scene by its guid at runtime.</summary>
+public struct SceneReference
+ ///<summary>The GUID that uniquely identifies this scene asset, used to serialize scene references reliably.</summary>
+ ///<remarks>Even if you move/rename the scene asset, GUID references stay valid.</remarks>
+ public Guid guid => sceneGuid; [SerializeField] Guid sceneGuid;
+ ///<summary>The build index of this scene, which can be used to load it or to obtain scene info.</summary>
+ public int sceneIndex => this.sceneGuid switch {
+ var sceneGuid => Array.FindIndex(UnityEditor.EditorBuildSettings.scenes, s => s.guid.ToString() == sceneGuid)
+ public int sceneIndex => SceneReferences.instance.SceneIndex(sceneGuid);