// Public Domain. NO WARRANTIES. License: https://opensource.org/licenses/0BSDusingUnityEngine;usingUnityEditor;[CustomPropertyDrawer(typeof(SceneReference))]publicclassSceneReferenceDrawer:PropertyDrawer{publicoverridevoidOnGUI(Rectposition,SerializedPropertyproperty,GUIContentlabel){if(!property.NextVisible(true))return;// could use 128-bit type to serialize, like rectIntValue; but it would be less legible in text assetsvaroldGuid=property.stringValue;varoldPath=AssetDatabase.GUIDToAssetPath(oldGuid);varoldObj=AssetDatabase.LoadAssetAtPath<SceneAsset>(oldPath);varnewObj=EditorGUI.ObjectField(position,label,oldObj,typeof(SceneAsset),false)asSceneAsset;if(newObj==oldObj)return;if(newObj==null)property.stringValue="";elseif(AssetDatabase.TryGetGUIDAndLocalFileIdentifier(newObj,outstringnewGuid,outlong_))property.stringValue=newGuid;}}
// Public Domain. NO WARRANTIES. License: https://opensource.org/licenses/0BSDusingSystem;usingUnityEditor;usingUnityEditor.Build;usingUnityEditor.Build.Reporting;usingUnityEngine;classSceneReferencesBuildProcessor:IPreprocessBuildWithReport,IPostprocessBuildWithReport{conststringresourcePath="Assets/_autoBuild_SceneIndicesByGuid.asset";publicintcallbackOrder=>0;publicvoidOnPreprocessBuild(BuildReportreport){varsceneReferences=ScriptableObject.CreateInstance<SceneReferences>();AssetDatabase.CreateAsset(sceneReferences,resourcePath);varpreloaded=PlayerSettings.GetPreloadedAssets();varn=preloaded.Length;Array.Resize(refpreloaded,n+1);preloaded[n]=sceneReferences;PlayerSettings.SetPreloadedAssets(preloaded);}publicvoidOnPostprocessBuild(BuildReportreport){AssetDatabase.DeleteAsset(resourcePath);varpreloaded=PlayerSettings.GetPreloadedAssets();vari=Array.FindLastIndex(preloaded,o=>oisSceneReferences);if(i<0)return;varn=preloaded.Length-1;varnewPreloaded=newUnityEngine.Object[n];Array.Copy(preloaded,newPreloaded,i);Array.Copy(preloaded,i+1,newPreloaded,i,n-i);PlayerSettings.SetPreloadedAssets(newPreloaded);}}
// Public Domain. NO WARRANTIES. License: https://opensource.org/licenses/0BSDusingSystem;usingSystem.Collections.Generic;usingUnityEngine;usingGuid=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>publicclassSceneReferences:ScriptableObject{///<summary>The singleton instance, automatically preloaded at runtime.</summary>publicstaticSceneReferencesinstance=>asset#if UNITY_EDITOR??(asset=ScriptableObject.CreateInstance<SceneReferences>())#endif;staticSceneReferencesasset;///<summary>The GUID for each scene asset, indexed by its build index in the array.</summary>publicGuid[]guids=>sceneGuids;[SerializeField]Guid[]sceneGuids;Dictionary<Guid,int>cache;privatevoidAwake(){#if UNITY_EDITORsceneGuids=Array.ConvertAll(UnityEditor.EditorBuildSettings.scenes,s=>s.guid.ToString());#endifif(sceneGuids==null)sceneGuids=newGuid[0];intn=sceneGuids.Length;cache=newDictionary<Guid,int>(n);for(inti=0;i<n;i++)cache.Add(sceneGuids[i],i);asset=this;}///<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>publicintSceneIndex(GuidsceneGuid)=>cache.TryGetValue(sceneGuid,outinti)?i:-1;}///<summary>SceneReference is used to reference a Scene by its guid at runtime.</summary>[Serializable]publicstructSceneReference{///<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>publicGuidguid=>sceneGuid;[SerializeField]GuidsceneGuid;///<summary>The build index of this scene, which can be used to load it or to obtain scene info.</summary>#if UNITY_EDITORpublicintsceneIndex=>this.sceneGuidswitch{varsceneGuid=>Array.FindIndex(UnityEditor.EditorBuildSettings.scenes,s=>s.guid.ToString()==sceneGuid)};#elsepublicintsceneIndex=>SceneReferences.instance.SceneIndex(sceneGuid);#endif}
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.