// Public Domain. NO WARRANTIES. License: https://opensource.org/licenses/0BSDusingSystem.Text.RegularExpressions;usingUnityEngine;usingUnityEditor;[CustomPropertyDrawer(typeof(SceneReference))]publicclassSceneReferenceDrawer:PropertyDrawer{staticreadonlyRegexrArrayItemProp=newRegex(@"\.Array\.data\[(\d+)\]$");publicoverridevoidOnGUI(Rectposition,SerializedPropertyproperty,GUIContentlabel){if(property.name=="data"){varmatchArrayItem=rArrayItemProp.Match(property.propertyPath);if(matchArrayItem.Success)// property is array itemlabel.text="Element "+matchArrayItem.Groups[1].Value;// label should not be GUID in array item}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;// NOTE: doesn't handle missing references, which are shown as "None (Scene Asset)"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){varpreloaded=PlayerSettings.GetPreloadedAssets();if(preloaded!=null){intn=preloaded.Length-1,i=n;for(;i>=0;i--)if(preloaded[i]isSceneReferences)break;// i = Array.FindLastIndexif(i>=0){varnewPreloaded=newUnityEngine.Object[n];Array.Copy(preloaded,newPreloaded,i);Array.Copy(preloaded,i+1,newPreloaded,i,n-i);PlayerSettings.SetPreloadedAssets(newPreloaded);}}AssetDatabase.DeleteAsset(resourcePath);AssetDatabase.SaveAssets();}}
// 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_EDITORbase.hideFlags=HideFlags.NotEditable;sceneGuids=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>///<param name="sceneGuid">The GUID of the scene to look up.</param>///<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>Create a reference to a scene using its GUID.</summary>///<param name="guid">The GUID of the scene, found in its .scene.meta file, or obtained from AssetDatabase.</param>publicSceneReference(Guidguid)=>this.sceneGuid=guid;///<summary>The build index of this scene, which can be used to load it or to obtain scene info.</summary>#if UNITY_EDITORpublicintsceneIndex{get{vars=UnityEditor.EditorBuildSettings.scenes;for(inti=0,n=s.Length;i<n;i++)if(s[i].guid.ToString()==sceneGuid)returni;return-1;}}#elsepublicintsceneIndex=>SceneReferences.instance.SceneIndex(sceneGuid);#endif}
Comments (2)
Alfish
This snippet will no longer be updated here.
It works, but doesn’t handle special case of missing references (they’re displayed the same way as “none” references).
For updated code, see: Unity Package: Scene References repository, which can be added in Unity’s Package Manager via git url or .tgz files in download section.
This snippet will no longer be updated here.
It works, but doesn’t handle special case of missing references (they’re displayed the same way as “none” references).
For updated code, see: Unity Package: Scene References repository, which can be added in Unity’s Package Manager via git url or .tgz files in download section.
This code corresponds to version 0.1.1 of the package: com.unity_x.modules.sceneref-0.1.1.tgz