Skip to content

Commit

Permalink
Merge pull request #15 from mewlist/unified-scene
Browse files Browse the repository at this point in the history
Unified scene validation
  • Loading branch information
mewlist authored Jan 20, 2024
2 parents b14532b + ec7e057 commit 1305675
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
45 changes: 45 additions & 0 deletions Editor/Assets/SceneReferencePropertyDrawer.cs
Original file line number Diff line number Diff line change
@@ -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 = "<SceneName>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;
}
}
}
}
3 changes: 3 additions & 0 deletions Editor/Assets/SceneReferencePropertyDrawer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Runtime/Assets/Scene/SceneLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async ValueTask<Scene> 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);
Expand Down
4 changes: 3 additions & 1 deletion Runtime/Assets/Scene/UnifiedScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,5 +40,7 @@ public bool IsSceneResourceLocation
#endif
}
}

public bool IsValid => IsSceneReference || IsSceneAssetReference || IsSceneResourceLocation;
}
}

0 comments on commit 1305675

Please sign in to comment.