From d224a36b5200b7cc66eb49c66d0e8ee18f7fbdb0 Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Thu, 10 Jun 2021 18:00:20 -0700 Subject: [PATCH] cleaned up service configuration profiles a bit (#852) # XRTK - Mixed Reality Toolkit Pull Request ## Overview Cleaned up and simplified configuration serialized properties a bit. --- Editor/Data/ConfigurationProperty.cs | 79 ++++++++++++++ Editor/Data/ConfigurationProperty.cs.meta | 11 ++ ...ormServiceConfigurationProfileInspector.cs | 103 +++++++++--------- ...dRealityServiceProviderProfileInspector.cs | 92 ++++++++-------- .../PlatformEntryPropertyDrawer.cs | 96 +++++++++------- Runtime/Definitions/Utilities/SystemType.cs | 13 ++- 6 files changed, 262 insertions(+), 132 deletions(-) create mode 100644 Editor/Data/ConfigurationProperty.cs create mode 100644 Editor/Data/ConfigurationProperty.cs.meta diff --git a/Editor/Data/ConfigurationProperty.cs b/Editor/Data/ConfigurationProperty.cs new file mode 100644 index 000000000..1b8b8a0dd --- /dev/null +++ b/Editor/Data/ConfigurationProperty.cs @@ -0,0 +1,79 @@ +// Copyright (c) XRTK. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +using System; +using UnityEditor; +using XRTK.Definitions.Utilities; + +namespace XRTK.Editor.Data +{ + internal class ConfigurationProperty + { + private readonly SerializedProperty configuration; + + public bool IsExpanded + { + get => configuration.isExpanded; + set => configuration.isExpanded = value; + } + + private readonly SerializedProperty name; + + public string Name + { + get => name.stringValue; + set => name.stringValue = value; + } + + private readonly SerializedProperty priority; + + public uint Priority + { + get => (uint)priority.intValue; + set => priority.intValue = (int)value; + } + + private readonly SerializedProperty instancedType; + private readonly SerializedProperty reference; + + public Type InstancedType + { + get => new SystemType(instancedType); + set => reference.stringValue = value == null ? string.Empty : value.GUID.ToString(); + } + + private readonly SerializedProperty platformEntries; + private readonly SerializedProperty runtimePlatforms; + + private readonly SerializedProperty profile; + + public UnityEngine.Object Profile + { + get => profile.objectReferenceValue; + set => profile.objectReferenceValue = value; + } + + public ConfigurationProperty(SerializedProperty property, bool clearPlatforms = false) + { + configuration = property; + name = property.FindPropertyRelative(nameof(name)); + priority = property.FindPropertyRelative(nameof(priority)); + instancedType = property.FindPropertyRelative(nameof(instancedType)); + reference = instancedType.FindPropertyRelative(nameof(reference)); + platformEntries = property.FindPropertyRelative(nameof(platformEntries)); + runtimePlatforms = platformEntries.FindPropertyRelative(nameof(runtimePlatforms)); + + if (clearPlatforms) + { + runtimePlatforms.ClearArray(); + } + + profile = property.FindPropertyRelative(nameof(profile)); + } + + public void ApplyModifiedProperties() + { + configuration.serializedObject.ApplyModifiedProperties(); + } + } +} diff --git a/Editor/Data/ConfigurationProperty.cs.meta b/Editor/Data/ConfigurationProperty.cs.meta new file mode 100644 index 000000000..0a32e3235 --- /dev/null +++ b/Editor/Data/ConfigurationProperty.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b4fe9afd2c97447484e57767b0a0469f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {fileID: 2800000, guid: 8ac5213854cf4dbabd140decf8df1946, type: 3} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/Profiles/MixedRealityPlatformServiceConfigurationProfileInspector.cs b/Editor/Profiles/MixedRealityPlatformServiceConfigurationProfileInspector.cs index 693c10db2..36b768215 100644 --- a/Editor/Profiles/MixedRealityPlatformServiceConfigurationProfileInspector.cs +++ b/Editor/Profiles/MixedRealityPlatformServiceConfigurationProfileInspector.cs @@ -10,6 +10,7 @@ using XRTK.Attributes; using XRTK.Definitions; using XRTK.Definitions.Utilities; +using XRTK.Editor.Data; using XRTK.Editor.Extensions; using XRTK.Editor.PropertyDrawers; using XRTK.Extensions; @@ -20,6 +21,7 @@ namespace XRTK.Editor.Profiles [CustomEditor(typeof(MixedRealityPlatformServiceConfigurationProfile))] public class MixedRealityPlatformServiceConfigurationProfileInspector : BaseMixedRealityProfileInspector { + private readonly GUIContent profileContent = new GUIContent("Profile", "The settings profile for this service."); private ReorderableList configurationList; private int currentlySelectedConfigurationOption; @@ -55,7 +57,8 @@ protected override void OnEnable() private void UpdatePlatformList() { - var runtimePlatforms = platformEntries.FindPropertyRelative("runtimePlatforms"); + SerializedProperty runtimePlatforms; + runtimePlatforms = platformEntries.FindPropertyRelative(nameof(runtimePlatforms)); if (runtimePlatforms.arraySize > 0) { @@ -66,7 +69,7 @@ private void UpdatePlatformList() for (int i = 0; i < runtimePlatforms.arraySize; i++) { - platforms[i] = new SystemType(runtimePlatforms.GetArrayElementAtIndex(i).FindPropertyRelative("reference").stringValue); + platforms[i] = new SystemType(runtimePlatforms.GetArrayElementAtIndex(i)); } } else @@ -143,34 +146,40 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive, var configurationProperty = configurations.GetArrayElementAtIndex(index); - var nameProperty = configurationProperty.FindPropertyRelative("name"); - var priorityProperty = configurationProperty.FindPropertyRelative("priority"); - var instanceTypeProperty = configurationProperty.FindPropertyRelative("instancedType"); - var platformEntriesProperty = configurationProperty.FindPropertyRelative("platformEntries"); - var globalRuntimePlatformProperty = platformEntries.FindPropertyRelative("runtimePlatforms"); - var runtimePlatformProperty = platformEntriesProperty.FindPropertyRelative("runtimePlatforms"); - var configurationProfileProperty = configurationProperty.FindPropertyRelative("profile"); - var systemTypeReference = new SystemType(instanceTypeProperty.FindPropertyRelative("reference").stringValue); + SerializedProperty instancedType; + SerializedProperty priority; + SerializedProperty runtimePlatforms; + SerializedProperty profile; + + var nameProperty = configurationProperty.FindPropertyRelative(nameof(name)); + priority = configurationProperty.FindPropertyRelative(nameof(priority)); + instancedType = configurationProperty.FindPropertyRelative(nameof(instancedType)); + var platformEntriesProperty = configurationProperty.FindPropertyRelative(nameof(platformEntries)); + var globalRuntimePlatforms = platformEntries.FindPropertyRelative(nameof(runtimePlatforms)); + runtimePlatforms = platformEntriesProperty.FindPropertyRelative(nameof(runtimePlatforms)); + profile = configurationProperty.FindPropertyRelative(nameof(profile)); + var systemTypeReference = new SystemType(instancedType); bool addPlatforms = false; - if (runtimePlatformProperty.arraySize != globalRuntimePlatformProperty.arraySize) + if (runtimePlatforms.arraySize != globalRuntimePlatforms.arraySize) { addPlatforms = true; - runtimePlatformProperty.ClearArray(); + runtimePlatforms.ClearArray(); } - if (globalRuntimePlatformProperty.arraySize > 0) + if (globalRuntimePlatforms.arraySize > 0) { - for (int i = 0; i < globalRuntimePlatformProperty.arraySize; i++) + for (int i = 0; i < globalRuntimePlatforms.arraySize; i++) { if (addPlatforms) { - runtimePlatformProperty.InsertArrayElementAtIndex(i); + runtimePlatforms.InsertArrayElementAtIndex(i); } - var globalPlatform = globalRuntimePlatformProperty.GetArrayElementAtIndex(i).FindPropertyRelative("reference").stringValue; - runtimePlatformProperty.GetArrayElementAtIndex(i).FindPropertyRelative("reference").stringValue = globalPlatform; + SerializedProperty reference; + reference = globalRuntimePlatforms.GetArrayElementAtIndex(i).FindPropertyRelative(nameof(reference)); + runtimePlatforms.GetArrayElementAtIndex(i).FindPropertyRelative(nameof(reference)).stringValue = reference.stringValue; } } @@ -210,7 +219,7 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive, } } - priorityProperty.intValue = index; + priority.intValue = index; var lastMode = EditorGUIUtility.wideMode; var prevLabelWidth = EditorGUIUtility.labelWidth; @@ -227,8 +236,6 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive, var profileRect = new Rect(rectX, rect.y + halfFieldHeight * 11, rectWidth, EditorGUIUtility.singleLineHeight); var runtimeRect = new Rect(rectX, rect.y + halfFieldHeight * (hasProfile ? 16 : 11), rectWidth, EditorGUIUtility.singleLineHeight); - EditorGUI.BeginChangeCheck(); - if (configurationProperty.isExpanded) { EditorGUI.PropertyField(nameRect, nameProperty); @@ -244,12 +251,15 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive, configurationProperty.isExpanded = EditorGUI.Foldout(nameRect, configurationProperty.isExpanded, nameProperty.stringValue, true); } + var hasChanged = false; + if (configurationProperty.isExpanded) { + EditorGUI.BeginChangeCheck(); TypeReferencePropertyDrawer.FilterConstraintOverride = IsConstraintSatisfied; TypeReferencePropertyDrawer.GroupingOverride = TypeGrouping.NoneByNameNoNamespace; - EditorGUI.PropertyField(typeRect, instanceTypeProperty); - systemTypeReference = new SystemType(instanceTypeProperty.FindPropertyRelative("reference").stringValue); + EditorGUI.PropertyField(typeRect, instancedType); + systemTypeReference = new SystemType(instancedType); GUI.enabled = false; @@ -259,13 +269,19 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive, if (hasProfile) { MixedRealityProfilePropertyDrawer.ProfileTypeOverride = profileType; - EditorGUI.PropertyField(profileRect, configurationProfileProperty, profileContent); + EditorGUI.PropertyField(profileRect, profile, profileContent); } + + hasChanged = EditorGUI.EndChangeCheck() && + runtimePlatforms.arraySize > 0 && + systemTypeReference.Type != null; } - if (configurationProfileProperty.objectReferenceValue != null) + serializedObject.ApplyModifiedProperties(); + + if (profile.objectReferenceValue != null) { - var renderedProfile = configurationProfileProperty.objectReferenceValue as BaseMixedRealityProfile; + var renderedProfile = profile.objectReferenceValue as BaseMixedRealityProfile; Debug.Assert(renderedProfile != null); if (renderedProfile.ParentProfile.IsNull() || @@ -275,16 +291,9 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive, } } - if (EditorGUI.EndChangeCheck()) + if (MixedRealityToolkit.IsInitialized && hasChanged) { - serializedObject.ApplyModifiedProperties(); - - if (MixedRealityToolkit.IsInitialized && - runtimePlatformProperty.arraySize > 0 && - systemTypeReference.Type != null) - { - MixedRealityToolkit.Instance.ResetProfile(MixedRealityToolkit.Instance.ActiveProfile); - } + MixedRealityToolkit.Instance.ResetProfile(MixedRealityToolkit.Instance.ActiveProfile); } EditorGUIUtility.wideMode = lastMode; @@ -313,20 +322,16 @@ private void OnConfigurationOptionAdded(ReorderableList list) configurations.arraySize += 1; var index = configurations.arraySize - 1; - var configuration = configurations.GetArrayElementAtIndex(index); - configuration.isExpanded = true; - var nameProperty = configuration.FindPropertyRelative("name"); - var priorityProperty = configuration.FindPropertyRelative("priority"); - var instancedTypeProperty = configuration.FindPropertyRelative("instancedType"); - var platformEntriesProperty = configuration.FindPropertyRelative("platformEntries"); - var configurationProfileProperty = configuration.FindPropertyRelative("profile"); - var runtimePlatformsProperty = platformEntriesProperty.FindPropertyRelative("runtimePlatforms"); - - nameProperty.stringValue = $"New Configuration {index}"; - instancedTypeProperty.FindPropertyRelative("reference").stringValue = string.Empty; - priorityProperty.intValue = index; - runtimePlatformsProperty.ClearArray(); - configurationProfileProperty.objectReferenceValue = null; + var configuration = new ConfigurationProperty(configurations.GetArrayElementAtIndex(index), true) + { + IsExpanded = true, + Name = $"New Configuration {index}", + InstancedType = null, + Priority = (uint)index, + Profile = null, + }; + + configuration.ApplyModifiedProperties(); configListHeightFlags.Add(new Tuple(true, false)); serializedObject.ApplyModifiedProperties(); } @@ -346,4 +351,4 @@ private void OnConfigurationOptionRemoved(ReorderableList list) } } } -} \ No newline at end of file +} diff --git a/Editor/Profiles/MixedRealityServiceProviderProfileInspector.cs b/Editor/Profiles/MixedRealityServiceProviderProfileInspector.cs index 2b3034357..b39814612 100644 --- a/Editor/Profiles/MixedRealityServiceProviderProfileInspector.cs +++ b/Editor/Profiles/MixedRealityServiceProviderProfileInspector.cs @@ -10,6 +10,7 @@ using XRTK.Definitions; using XRTK.Definitions.Platforms; using XRTK.Definitions.Utilities; +using XRTK.Editor.Data; using XRTK.Editor.Extensions; using XRTK.Editor.PropertyDrawers; using XRTK.Extensions; @@ -141,14 +142,19 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive, serializedObject.Update(); var configurationProperty = configurations.GetArrayElementAtIndex(index); - - var nameProperty = configurationProperty.FindPropertyRelative("name"); - var priorityProperty = configurationProperty.FindPropertyRelative("priority"); - var instanceTypeProperty = configurationProperty.FindPropertyRelative("instancedType"); - var systemTypeReference = new SystemType(instanceTypeProperty.FindPropertyRelative("reference").stringValue); - var platformEntriesProperty = configurationProperty.FindPropertyRelative("platformEntries"); - var runtimePlatformProperty = platformEntriesProperty.FindPropertyRelative("runtimePlatforms"); - var configurationProfileProperty = configurationProperty.FindPropertyRelative("profile"); + SerializedProperty priority; + SerializedProperty instancedType; + SerializedProperty platformEntries; + SerializedProperty runtimePlatforms; + SerializedProperty profile; + + var nameProperty = configurationProperty.FindPropertyRelative(nameof(name)); + priority = configurationProperty.FindPropertyRelative(nameof(priority)); + instancedType = configurationProperty.FindPropertyRelative(nameof(instancedType)); + var systemTypeReference = new SystemType(instancedType); + platformEntries = configurationProperty.FindPropertyRelative(nameof(platformEntries)); + runtimePlatforms = platformEntries.FindPropertyRelative(nameof(runtimePlatforms)); + profile = configurationProperty.FindPropertyRelative(nameof(profile)); var hasProfile = false; Type profileType = null; @@ -185,7 +191,7 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive, } } - priorityProperty.intValue = index - 1; + priority.intValue = index - 1; var lastMode = EditorGUIUtility.wideMode; var prevLabelWidth = EditorGUIUtility.labelWidth; @@ -220,26 +226,26 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive, else { configurationProperty.isExpanded = EditorGUI.Foldout(dropdownRect, configurationProperty.isExpanded, GUIContent.none, false) || - hasProfile && configurationProfileProperty.objectReferenceValue == null; + hasProfile && profile.objectReferenceValue == null; - if (!configurationProfileProperty.isExpanded) + if (!profile.isExpanded) { if (hasProfile) { if (GUI.Button(labelRect, nameProperty.stringValue, ButtonGuiStyle) && - configurationProfileProperty.objectReferenceValue != null) + profile.objectReferenceValue != null) { - var profile = configurationProfileProperty.objectReferenceValue as BaseMixedRealityProfile; + var profileInstance = profile.objectReferenceValue as BaseMixedRealityProfile; - Debug.Assert(profile != null); + Debug.Assert(profileInstance != null); - if (profile.ParentProfile.IsNull() || - profile.ParentProfile != ThisProfile) + if (profileInstance.ParentProfile.IsNull() || + profileInstance.ParentProfile != ThisProfile) { - profile.ParentProfile = ThisProfile; + profileInstance.ParentProfile = ThisProfile; } - Selection.activeObject = profile; + Selection.activeObject = profileInstance; } } else @@ -261,15 +267,15 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive, TypeReferencePropertyDrawer.CreateNewTypeOverride = ServiceConstraint; EditorGUI.BeginChangeCheck(); - EditorGUI.PropertyField(typeRect, instanceTypeProperty); - systemTypeReference = new SystemType(instanceTypeProperty.FindPropertyRelative("reference").stringValue); + EditorGUI.PropertyField(typeRect, instancedType); + systemTypeReference = new SystemType(instancedType); if (EditorGUI.EndChangeCheck()) { if (systemTypeReference.Type == null) { nameProperty.stringValue = string.Empty; - configurationProfileProperty.objectReferenceValue = null; + profile.objectReferenceValue = null; } else { @@ -277,32 +283,32 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive, if (IsSystemConfiguration) { - configurationProfileProperty.objectReferenceValue = null; + profile.objectReferenceValue = null; } } } if (!IsSystemConfiguration) { - EditorGUI.PropertyField(runtimeRect, platformEntriesProperty); - runtimePlatformProperty = platformEntriesProperty.FindPropertyRelative("runtimePlatforms"); + EditorGUI.PropertyField(runtimeRect, platformEntries); + runtimePlatforms = platformEntries.FindPropertyRelative(nameof(runtimePlatforms)); } else { - runtimePlatformProperty = platformEntriesProperty.FindPropertyRelative("runtimePlatforms"); - runtimePlatformProperty.arraySize = 1; - runtimePlatformProperty.GetArrayElementAtIndex(0).FindPropertyRelative("reference").stringValue = AllPlatformsGuid.ToString(); + runtimePlatforms = platformEntries.FindPropertyRelative(nameof(runtimePlatforms)); + runtimePlatforms.arraySize = 1; + runtimePlatforms.GetArrayElementAtIndex(0).FindPropertyRelative("reference").stringValue = AllPlatformsGuid.ToString(); } if (hasProfile) { MixedRealityProfilePropertyDrawer.ProfileTypeOverride = profileType; - EditorGUI.PropertyField(profileRect, configurationProfileProperty, profileContent); + EditorGUI.PropertyField(profileRect, profile, profileContent); } - if (configurationProfileProperty.objectReferenceValue != null) + if (profile.objectReferenceValue != null) { - var renderedProfile = configurationProfileProperty.objectReferenceValue as BaseMixedRealityProfile; + var renderedProfile = profile.objectReferenceValue as BaseMixedRealityProfile; Debug.Assert(renderedProfile != null); if (renderedProfile.ParentProfile.IsNull() || @@ -318,7 +324,7 @@ private void DrawConfigurationOptionElement(Rect rect, int index, bool isActive, serializedObject.ApplyModifiedProperties(); if (MixedRealityToolkit.IsInitialized && - runtimePlatformProperty.arraySize > 0 && + runtimePlatforms.arraySize > 0 && systemTypeReference.Type != null) { MixedRealityToolkit.Instance.ResetProfile(MixedRealityToolkit.Instance.ActiveProfile); @@ -335,20 +341,16 @@ private void OnConfigurationOptionAdded(ReorderableList list) configurations.arraySize += 1; var index = configurations.arraySize - 1; - var configuration = configurations.GetArrayElementAtIndex(index); - configuration.isExpanded = true; - var nameProperty = configuration.FindPropertyRelative("name"); - var priorityProperty = configuration.FindPropertyRelative("priority"); - var instancedTypeProperty = configuration.FindPropertyRelative("instancedType"); - var platformEntriesProperty = configuration.FindPropertyRelative("platformEntries"); - var configurationProfileProperty = configuration.FindPropertyRelative("profile"); - var runtimePlatformsProperty = platformEntriesProperty.FindPropertyRelative("runtimePlatforms"); - - nameProperty.stringValue = $"New Configuration {index}"; - instancedTypeProperty.FindPropertyRelative("reference").stringValue = string.Empty; - priorityProperty.intValue = index; - runtimePlatformsProperty.ClearArray(); - configurationProfileProperty.objectReferenceValue = null; + var configuration = new ConfigurationProperty(configurations.GetArrayElementAtIndex(index), true) + { + IsExpanded = true, + Name = $"New Configuration {index}", + InstancedType = null, + Priority = (uint)index, + Profile = null + }; + + configuration.ApplyModifiedProperties(); configListHeightFlags.Add(new Tuple(true, false)); serializedObject.ApplyModifiedProperties(); } diff --git a/Editor/PropertyDrawers/PlatformEntryPropertyDrawer.cs b/Editor/PropertyDrawers/PlatformEntryPropertyDrawer.cs index 6d28cc217..024d75d64 100644 --- a/Editor/PropertyDrawers/PlatformEntryPropertyDrawer.cs +++ b/Editor/PropertyDrawers/PlatformEntryPropertyDrawer.cs @@ -43,6 +43,36 @@ public class PlatformEntryPropertyDrawer : PropertyDrawer private static int selectionControlId; private static int arraySize = 0; + private class SerializedTypeProperty + { + private readonly SerializedProperty reference; + + public Type ReferenceType + { + get + { + TypeExtensions.TryResolveType(reference.stringValue, out Type referenceType); + return referenceType; + } + } + + public Guid TypeReference + { + get => Guid.TryParse(reference.stringValue, out var guid) ? guid : Guid.Empty; + set => reference.stringValue = value.ToString(); + } + + public SerializedTypeProperty(SerializedProperty property) + { + reference = property.FindPropertyRelative(nameof(reference)); + } + + public void ApplyModifiedProperties() + { + reference.serializedObject.ApplyModifiedProperties(); + } + } + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { position = EditorGUI.PrefixLabel(position, RuntimePlatformContent); @@ -162,20 +192,18 @@ bool IsPlatformActive(Type platformType) for (int i = 0; i < runtimePlatformsProperty.arraySize; i++) { - var systemTypeProperty = runtimePlatformsProperty.GetArrayElementAtIndex(i); - var referenceProperty = systemTypeProperty.FindPropertyRelative("reference"); - TypeExtensions.TryResolveType(referenceProperty.stringValue, out var referenceType); + var serializedType = new SerializedTypeProperty(runtimePlatformsProperty.GetArrayElementAtIndex(i)); // Clean up any broken references - if (referenceType == null) + if (serializedType.ReferenceType == null) { - Debug.LogError($"Failed to resolve {referenceProperty.stringValue}! Removing from runtime platform entry..."); + Debug.LogError($"Failed to resolve {serializedType.TypeReference}! Removing from runtime platform entry..."); runtimePlatformsProperty.DeleteArrayElementAtIndex(i); runtimePlatformsProperty.serializedObject.ApplyModifiedProperties(); continue; } - if (platformType == referenceType) + if (platformType == serializedType.ReferenceType) { isActive = true; } @@ -203,11 +231,10 @@ string GetDropdownContentText() return EditorOnly; } - var systemTypeProperty = runtimePlatformsProperty.GetArrayElementAtIndex(0); - var classRefProperty = systemTypeProperty.FindPropertyRelative("reference"); + var systemTypeProperty = new SerializedTypeProperty(runtimePlatformsProperty.GetArrayElementAtIndex(0)); - return TypeExtensions.TryResolveType(classRefProperty.stringValue, out var resolvedType) - ? resolvedType.Name.Replace(Platform, string.Empty).ToProperCase() + return systemTypeProperty.ReferenceType != null + ? systemTypeProperty.ReferenceType.Name.Replace(Platform, string.Empty).ToProperCase() : Nothing; } @@ -221,13 +248,11 @@ string GetDropdownContentText() for (int i = 0; i < runtimePlatformsProperty.arraySize; i++) { - var systemTypeProperty = runtimePlatformsProperty.GetArrayElementAtIndex(i); - var classRefProperty = systemTypeProperty.FindPropertyRelative("reference"); + var systemTypeProperty = new SerializedTypeProperty(runtimePlatformsProperty.GetArrayElementAtIndex(i)); - if (TypeExtensions.TryResolveType(classRefProperty.stringValue, out var resolvedType) && - resolvedType != EditorBuildTargetType) + if (systemTypeProperty.ReferenceType != EditorBuildTargetType) { - type = resolvedType; + type = systemTypeProperty.ReferenceType; break; } } @@ -265,16 +290,14 @@ void OnEditorSelected(object _) for (int i = 0; i < runtimePlatformsProperty.arraySize; i++) { - var typeProperty = runtimePlatformsProperty.GetArrayElementAtIndex(i); - var refProperty = typeProperty.FindPropertyRelative("reference"); - TypeExtensions.TryResolveType(refProperty.stringValue, out var referenceType); + var typeProperty = new SerializedTypeProperty(runtimePlatformsProperty.GetArrayElementAtIndex(i)); - if (referenceType == EditorBuildTargetType) + if (typeProperty.ReferenceType == EditorBuildTargetType) { isCurrentBuildTargetPlatformActive = true; } - if (referenceType == AllPlatformsType) + if (typeProperty.ReferenceType == AllPlatformsType) { isAllPlatformsActive = true; } @@ -340,17 +363,17 @@ platform is EditorPlatform || void OnSelectedTypeName(object typeRef) { - var selectedPlatformType = typeRef as Type; + if (!(typeRef is Type selectedPlatformType)) { return; } - if (selectedPlatformType == null) { return; } + var selectedTypeGuid = selectedPlatformType.GUID; - if (selectedPlatformType.GUID == Guid.Empty) + if (selectedTypeGuid == Guid.Empty) { Debug.LogError($"{selectedPlatformType.Name} does not implement a required {nameof(GuidAttribute)}"); return; } - if (!TryRemovePlatformReference(selectedPlatformType.GUID)) + if (!TryRemovePlatformReference(selectedTypeGuid)) { if (runtimePlatformsProperty.arraySize == MixedRealityToolkit.AvailablePlatforms.Count - 3) { @@ -358,7 +381,7 @@ void OnSelectedTypeName(object typeRef) } else { - TryAddPlatformReference(selectedPlatformType.GUID); + TryAddPlatformReference(selectedTypeGuid); } } @@ -376,11 +399,10 @@ void TryAddPlatformReference(Guid classReference) for (int i = 0; i < runtimePlatformsProperty.arraySize; i++) { - var existingSystemTypeProperty = runtimePlatformsProperty.GetArrayElementAtIndex(i); - var existingReferenceProperty = existingSystemTypeProperty.FindPropertyRelative("reference"); + var existingSystemTypeProperty = new SerializedTypeProperty(runtimePlatformsProperty.GetArrayElementAtIndex(i)); - if (!TypeExtensions.TryResolveType(existingReferenceProperty.stringValue, out var existingPlatformType) || - selectedPlatformType == existingPlatformType) + if (existingSystemTypeProperty.ReferenceType == null || + existingSystemTypeProperty.ReferenceType == selectedPlatformType) { return; } @@ -389,9 +411,11 @@ void TryAddPlatformReference(Guid classReference) var index = runtimePlatformsProperty.arraySize; runtimePlatformsProperty.serializedObject.ApplyModifiedProperties(); runtimePlatformsProperty.InsertArrayElementAtIndex(index); - var systemTypeProperty = runtimePlatformsProperty.GetArrayElementAtIndex(index); - var referenceProperty = systemTypeProperty.FindPropertyRelative("reference"); - referenceProperty.stringValue = classReference.ToString(); + var systemTypeProperty = new SerializedTypeProperty(runtimePlatformsProperty.GetArrayElementAtIndex(index)) + { + TypeReference = classReference + }; + systemTypeProperty.ApplyModifiedProperties(); runtimePlatformsProperty.serializedObject.ApplyModifiedProperties(); } @@ -420,11 +444,9 @@ bool TryRemovePlatformReference(Guid classReference) for (int i = 0; i < runtimePlatformsProperty.arraySize; i++) { - var systemTypeProperty = runtimePlatformsProperty.GetArrayElementAtIndex(i); - var referenceProperty = systemTypeProperty.FindPropertyRelative("reference"); + var systemTypeProperty = new SerializedTypeProperty(runtimePlatformsProperty.GetArrayElementAtIndex(i)); - if (TypeExtensions.TryResolveType(referenceProperty.stringValue, out var referenceType) && - selectedPlatformType == referenceType) + if (systemTypeProperty.ReferenceType == selectedPlatformType) { if (runtimePlatformsProperty.arraySize == 2 && IsPlatformActive(EditorBuildTargetType) && @@ -446,4 +468,4 @@ bool TryRemovePlatformReference(Guid classReference) } } } -} \ No newline at end of file +} diff --git a/Runtime/Definitions/Utilities/SystemType.cs b/Runtime/Definitions/Utilities/SystemType.cs index 587ce82f7..41a8e9090 100644 --- a/Runtime/Definitions/Utilities/SystemType.cs +++ b/Runtime/Definitions/Utilities/SystemType.cs @@ -13,6 +13,17 @@ namespace XRTK.Definitions.Utilities [Serializable] public sealed class SystemType : ISerializationCallbackReceiver { +#if UNITY_EDITOR + /// + /// Initializes a new instance of the class. + /// + /// The serialized property of the type reference. + public SystemType(UnityEditor.SerializedProperty property) + { + TypeExtensions.TryResolveType(property.FindPropertyRelative(nameof(reference)).stringValue, out var resolvedType); + Type = resolvedType; + } +#endif /// /// Initializes a new instance of the class. /// @@ -142,4 +153,4 @@ public override string ToString() return Type?.FullName ?? (string.IsNullOrWhiteSpace(reference) ? "{None}" : reference); } } -} \ No newline at end of file +}