Skip to content

Commit

Permalink
String key unified scene
Browse files Browse the repository at this point in the history
  • Loading branch information
mewlist committed Feb 8, 2024
1 parent c9aabef commit a311800
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 8 deletions.
54 changes: 48 additions & 6 deletions Editor/Assets/UnifiedScenePropertyDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,54 @@ public class UnifiedScenePropertyDrawer : PropertyDrawer
{
private const string SceneAssetReferenceKBackingField = "<SceneAssetReference>k__BackingField";
private const string SceneReferenceKBackingField = "<SceneReference>k__BackingField";
private const string AddressableSceneKeyKBackingField = "<AddressablesSceneKey>k__BackingField";

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
#if USE_MEW_CORE_ASSETS
var sceneAssetReferenceProperty = property.FindPropertyRelative(SceneAssetReferenceKBackingField);
var addressableSceneKeyProperty = property.FindPropertyRelative(AddressableSceneKeyKBackingField);
#endif
var sceneReferenceProperty = property.FindPropertyRelative(SceneReferenceKBackingField);

#if USE_MEW_CORE_ASSETS
var sceneAssetReference = sceneAssetReferenceProperty.boxedValue as SceneAssetReference;
var addressableSceneKey = addressableSceneKeyProperty.boxedValue as AddressablesSceneKey;
#endif
var sceneReference = sceneReferenceProperty.boxedValue as SceneReference;

var drawer = new LayoutDrawer();
#if USE_MEW_CORE_ASSETS
var isAssetReference = !string.IsNullOrEmpty(sceneAssetReference.AssetGUID);
var isAddressableSceneKey = addressableSceneKey is not null;
#else
const bool isAssetReference = false;
const bool isAddressableSceneKey = false;
#endif
var isSceneReference = sceneReference.IsValid;

var labelText = isAssetReference
var drawer = new LayoutDrawer();
var labelText = (isAssetReference || isAddressableSceneKey)
? $"{label.text} (Addressables)"
: isSceneReference
? $"{label.text} : (Build Settings)"
: $"{label.text} : Select scene";
var customLabel = new GUIContent(labelText);
var originalColor = GUI.color;
if (!isAssetReference && !isSceneReference)
if (!isAssetReference && !isSceneReference && !isAddressableSceneKey)
GUI.color = Color.yellow;
drawer.DrawLabel(position, customLabel);

EditorGUI.indentLevel++;

#if USE_MEW_CORE_ASSETS
if (isAssetReference)
{
drawer.DrawProperty(position, sceneAssetReferenceProperty);
}
else if (isAddressableSceneKey)
{
drawer.DrawProperty(position, addressableSceneKeyProperty);
}
else if (isSceneReference)
{
drawer.DrawProperty(position, sceneReferenceProperty);
Expand All @@ -44,7 +64,12 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
{
drawer.DrawProperty(position, sceneAssetReferenceProperty);
drawer.DrawProperty(position, sceneReferenceProperty);
drawer.DrawProperty(position, addressableSceneKeyProperty);
}
#else
drawer.DrawProperty(position, sceneReferenceProperty);
#endif

EditorGUI.indentLevel--;
if (!isAssetReference && !isSceneReference)
GUI.color = originalColor;
Expand Down Expand Up @@ -83,26 +108,43 @@ public override float GetPropertyHeight(SerializedProperty property, GUIContent
{
var sceneAssetReferenceProperty = property.FindPropertyRelative(SceneAssetReferenceKBackingField);
var sceneReferenceProperty = property.FindPropertyRelative(SceneReferenceKBackingField);
var addressableSceneKeyProperty = property.FindPropertyRelative(AddressableSceneKeyKBackingField);
#if USE_MEW_CORE_ASSETS
var sceneAssetReference = sceneAssetReferenceProperty.boxedValue as SceneAssetReference;
var addressableSceneKey = addressableSceneKeyProperty.boxedValue as AddressablesSceneKey;
#endif
var sceneReference = sceneReferenceProperty.boxedValue as SceneReference;

var fixedHeight = EditorGUI.GetPropertyHeight(SerializedPropertyType.String, label);

if (!string.IsNullOrEmpty(sceneAssetReference.AssetGUID))
if (sceneReference.IsValid)
{
return fixedHeight +
EditorGUI.GetPropertyHeight(sceneReferenceProperty);
}
#if USE_MEW_CORE_ASSETS
else if (!string.IsNullOrEmpty(sceneAssetReference.AssetGUID))
{
return fixedHeight +
EditorGUI.GetPropertyHeight(sceneAssetReferenceProperty);
}
else if (sceneReference.IsValid)
else if (addressableSceneKey is not null)
{
return fixedHeight +
EditorGUI.GetPropertyHeight(sceneReferenceProperty);
EditorGUI.GetPropertyHeight(addressableSceneKeyProperty);
}
#endif
else
{
#if USE_MEW_CORE_ASSETS
return fixedHeight +
EditorGUI.GetPropertyHeight(sceneAssetReferenceProperty) +
EditorGUI.GetPropertyHeight(sceneReferenceProperty) +
EditorGUI.GetPropertyHeight(addressableSceneKeyProperty);
#else
return fixedHeight +
EditorGUI.GetPropertyHeight(sceneReferenceProperty);
#endif
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions Runtime/Assets/Scene/AddressablesSceneKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#if USE_MEW_CORE_ASSETS
using UnityEngine;

namespace Mew.Core.Assets
{
[CreateAssetMenu(menuName = "MewCore/AddressableSceneKey", fileName = "AddressableSceneKey", order = 0)]
public class AddressablesSceneKey : ScriptableObject
{
[field: SerializeField] public string RuntimeKey { get; set; }
public AddressablesSceneKey(string runtimeKey) { RuntimeKey = runtimeKey; }
}
}
#endif
3 changes: 3 additions & 0 deletions Runtime/Assets/Scene/AddressablesSceneKey.cs.meta

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

17 changes: 15 additions & 2 deletions Runtime/Assets/Scene/UnifiedScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class UnifiedScene
{
#if USE_MEW_CORE_ASSETS
[field: SerializeField] public SceneAssetReference SceneAssetReference { get; set; }
[field: SerializeField] public AddressablesSceneKey AddressablesSceneKey { get; set; }
public IResourceLocation SceneResourceLocation { get; set; }
#endif
[field: SerializeField] public SceneReference SceneReference { get; set; }
Expand All @@ -32,6 +33,18 @@ public bool IsSceneAssetReference
}
}

public bool IsAddressablesSceneKey
{
get
{
#if USE_MEW_CORE_ASSETS
return AddressablesSceneKey is not null;
#else
return false;
#endif
}
}

public bool IsSceneResourceLocation
{
get
Expand All @@ -44,9 +57,9 @@ public bool IsSceneResourceLocation
}
}

private bool IsValidInternal => IsSceneReference || IsSceneAssetReference || IsSceneResourceLocation;
private bool IsValidInternal => IsSceneReference || IsSceneAssetReference || IsSceneResourceLocation || IsAddressablesSceneKey;
#if UNITY_EDITOR
public bool IsValid => IsValidInternal || !string.IsNullOrEmpty(EditorScenePath);
public bool IsValid => IsValidInternal || !string.IsNullOrEmpty(EditorScenePath);
#else
public bool IsValid => IsValidInternal;
#endif
Expand Down
5 changes: 5 additions & 0 deletions Runtime/Assets/Scene/UnifiedSceneLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public static async Task<ISceneHandle> LoadAsync(UnifiedScene unifiedScene, Canc
var h = Addressables.LoadSceneAsync(unifiedScene.SceneAssetReference, parameters);
handle = new SceneInstanceHandle(h);
}
else if (unifiedScene.IsAddressablesSceneKey)
{
var h = Addressables.LoadSceneAsync(unifiedScene.AddressablesSceneKey.RuntimeKey, parameters);
handle = new SceneInstanceHandle(h);
}
else
#endif
if (unifiedScene.SceneReference is not null && unifiedScene.SceneReference.IsValid)
Expand Down

0 comments on commit a311800

Please sign in to comment.