diff --git a/Editor/Assets/SceneReferencePropertyDrawer.cs b/Editor/Assets/SceneReferencePropertyDrawer.cs new file mode 100644 index 0000000..4b4247d --- /dev/null +++ b/Editor/Assets/SceneReferencePropertyDrawer.cs @@ -0,0 +1,45 @@ +using System.IO; +using System.Linq; +using UnityEditor; +using UnityEngine; + +namespace Mew.Core.Assets +{ + [CustomPropertyDrawer(typeof(SceneReference))] + public class SceneReferencePropertyDrawer : PropertyDrawer + { + private SceneAsset FindSceneAsset(string sceneName) + { + var targetScene = EditorBuildSettings.scenes.FirstOrDefault(x + => Path.GetFileNameWithoutExtension(x.path) == sceneName); + return targetScene != null + ? targetScene.enabled + ? AssetDatabase.LoadAssetAtPath(targetScene.path, typeof(SceneAsset)) as SceneAsset + : null + : null; + } + + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + const string fieldName = "k__BackingField"; + var serializedProperty = property.FindPropertyRelative(fieldName); + var sceneName = property.FindPropertyRelative(fieldName).stringValue; + var sceneAsset = FindSceneAsset(sceneName); + sceneAsset = (SceneAsset) EditorGUI.ObjectField(position, label, sceneAsset, typeof(SceneAsset), false); + + if (!string.IsNullOrEmpty(serializedProperty.stringValue) && sceneAsset == null) + { + serializedProperty.stringValue = string.Empty; + } + else if (sceneAsset is not null && sceneAsset.name != serializedProperty.stringValue) + { + if (FindSceneAsset(sceneAsset.name) is null) + { + Debug.LogWarning($"Scene {sceneAsset.name} is not found in build settings."); + serializedProperty.stringValue = string.Empty; + } + serializedProperty.stringValue = sceneAsset.name; + } + } + } +} \ No newline at end of file diff --git a/Editor/Assets/SceneReferencePropertyDrawer.cs.meta b/Editor/Assets/SceneReferencePropertyDrawer.cs.meta new file mode 100644 index 0000000..2681405 --- /dev/null +++ b/Editor/Assets/SceneReferencePropertyDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c7a8d397a3d9406a8bd435fcef062c36 +timeCreated: 1705579916 \ No newline at end of file diff --git a/Runtime/Assets/Scene/SceneLoader.cs b/Runtime/Assets/Scene/SceneLoader.cs index c0438af..8ac89c7 100644 --- a/Runtime/Assets/Scene/SceneLoader.cs +++ b/Runtime/Assets/Scene/SceneLoader.cs @@ -29,7 +29,7 @@ public async ValueTask LoadAsync(UnifiedScene unifiedScene, CancellationT var h = Addressables.LoadSceneAsync(unifiedScene.SceneResourceLocation, parameters); handle = new SceneInstanceHandle(h); } - else if (unifiedScene.SceneAssetReference is not null) + else if (unifiedScene.SceneAssetReference is not null && unifiedScene.SceneAssetReference.RuntimeKeyIsValid()) { var h = Addressables.LoadSceneAsync(unifiedScene.SceneAssetReference, parameters); handle = new SceneInstanceHandle(h); diff --git a/Runtime/Assets/Scene/UnifiedScene.cs b/Runtime/Assets/Scene/UnifiedScene.cs index 5ddf5cc..cf37da3 100644 --- a/Runtime/Assets/Scene/UnifiedScene.cs +++ b/Runtime/Assets/Scene/UnifiedScene.cs @@ -22,7 +22,7 @@ public bool IsSceneAssetReference get { #if USE_MEW_CORE_ASSETS - return SceneAssetReference != null; + return SceneAssetReference != null && SceneAssetReference.RuntimeKeyIsValid(); #else return false; #endif @@ -40,5 +40,7 @@ public bool IsSceneResourceLocation #endif } } + + public bool IsValid => IsSceneReference || IsSceneAssetReference || IsSceneResourceLocation; } } \ No newline at end of file