Skip to content

Commit 84733b2

Browse files
keveleighradicalad
authored andcommitted
Read the default profile and default platforms from the ExtensionServiceAttribute (#3506)
* Allow ability to specify a default profile for extension services * Improve reliability of path name loading * Add ability to auto-select the supported platforms when adding a service * Remove a duplicate cast * Combine method calls to reduce Linq/reflection calls
1 parent 5c26eb5 commit 84733b2

File tree

5 files changed

+69
-19
lines changed

5 files changed

+69
-19
lines changed

Assets/MixedRealityToolkit.Providers/WindowsMixedReality/WindowsMixedRealitySpatialMeshObserver.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ namespace Microsoft.MixedReality.Toolkit.Providers.WindowsMixedReality
2121
{
2222
[MixedRealityDataProvider(
2323
typeof(IMixedRealitySpatialAwarenessSystem),
24-
SupportedPlatforms.WindowsUniversal)]
24+
SupportedPlatforms.WindowsUniversal,
25+
"Profiles/DefaultMixedRealitySpatialAwarenessMeshObserverProfile.asset", "MixedRealityToolkit.SDK")]
2526
public class WindowsMixedRealitySpatialMeshObserver : BaseSpatialObserver, IMixedRealitySpatialAwarenessMeshObserver
2627
{
2728
/// <summary>

Assets/MixedRealityToolkit/Attributes/MixedRealityExtensionServiceAttribute.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.MixedReality.Toolkit.Core.Definitions;
55
using Microsoft.MixedReality.Toolkit.Core.Definitions.Utilities;
66
using System;
7+
using System.Linq;
78
using UnityEngine;
89

910
#if UNITY_EDITOR
@@ -16,7 +17,7 @@ namespace Microsoft.MixedReality.Toolkit.Core.Attributes
1617
/// <summary>
1718
/// Attribute that defines the properties of a Mixed Reality Toolkit extension service.
1819
/// </summary>
19-
[AttributeUsage(System.AttributeTargets.Class, AllowMultiple = false)]
20+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
2021
public class MixedRealityExtensionServiceAttribute : Attribute
2122
{
2223
/// <summary>
@@ -27,25 +28,25 @@ public class MixedRealityExtensionServiceAttribute : Attribute
2728
/// <summary>
2829
/// The file path to the default profile asset relative to the package folder.
2930
/// </summary>
30-
public virtual string ProfilePath { get; }
31+
public virtual string DefaultProfilePath { get; }
3132

3233
/// <summary>
33-
/// The package where the asset resides.
34+
/// The package where the default profile asset resides.
3435
/// </summary>
3536
public virtual string PackageFolder { get; }
3637

3738
/// <summary>
3839
/// The default profile.
3940
/// </summary>
40-
public virtual BaseMixedRealityProfile Profile
41+
public virtual BaseMixedRealityProfile DefaultProfile
4142
{
4243
get
4344
{
4445
#if UNITY_EDITOR
4546
string path;
4647
if (EditorProjectUtilities.FindRelativeDirectory(PackageFolder, out path))
4748
{
48-
return AssetDatabase.LoadAssetAtPath<BaseMixedRealityProfile>(System.IO.Path.Combine(path, ProfilePath));
49+
return AssetDatabase.LoadAssetAtPath<BaseMixedRealityProfile>(System.IO.Path.Combine(path, DefaultProfilePath));
4950
}
5051

5152
Debug.LogError("Unable to find or load the profile.");
@@ -58,16 +59,24 @@ public virtual BaseMixedRealityProfile Profile
5859
/// Constructor
5960
/// </summary>
6061
/// <param name="runtimePlatforms">The platforms on which the extension service is supported.</param>
61-
/// <param name="profilePath">The relative path to the profile asset.</param>
62-
/// <param name="packageFolder">The folder to which the path is relative.</param>
62+
/// <param name="profilePath">The relative path to the default profile asset.</param>
63+
/// <param name="packageFolder">The package folder to which the path is relative.</param>
6364
public MixedRealityExtensionServiceAttribute(
6465
SupportedPlatforms runtimePlatforms,
65-
string profilePath = "",
66+
string defaultProfilePath = "",
6667
string packageFolder = "MixedRealityToolkit")
6768
{
6869
RuntimePlatforms = runtimePlatforms;
69-
ProfilePath = profilePath;
70+
DefaultProfilePath = defaultProfilePath;
7071
PackageFolder = packageFolder;
7172
}
73+
74+
/// <summary>
75+
/// Convenience function for retrieving the attribute given a certain class type.
76+
/// </summary>
77+
public static MixedRealityExtensionServiceAttribute Find(Type type)
78+
{
79+
return type.GetCustomAttributes(typeof(MixedRealityExtensionServiceAttribute), true).FirstOrDefault() as MixedRealityExtensionServiceAttribute;
80+
}
7281
}
7382
}

Assets/MixedRealityToolkit/Inspectors/Profiles/BaseMixedRealityProfileInspector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private static bool RenderProfileInternal(SerializedProperty property, GUIConten
102102
if (GUILayout.Button(new GUIContent("</>", "Replace with a copy of the default profile."), EditorStyles.miniButton, GUILayout.Width(32f)))
103103
{
104104
profileToCopy = renderedProfile;
105-
var profileTypeName = property.type.Replace("PPtr<$", string.Empty).Replace(">", string.Empty);
105+
var profileTypeName = property.objectReferenceValue.GetType().Name;
106106
Debug.Assert(profileTypeName != null, "No Type Found");
107107

108108
ScriptableObject instance = CreateInstance(profileTypeName);

Assets/MixedRealityToolkit/Inspectors/Profiles/MixedRealityRegisteredServiceProviderProfileInspector.cs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4+
using Microsoft.MixedReality.Toolkit.Core.Attributes;
45
using Microsoft.MixedReality.Toolkit.Core.Definitions;
6+
using Microsoft.MixedReality.Toolkit.Core.Definitions.Utilities;
57
using Microsoft.MixedReality.Toolkit.Core.Inspectors.Utilities;
68
using Microsoft.MixedReality.Toolkit.Core.Services;
79
using UnityEditor;
@@ -95,6 +97,8 @@ private void RenderList(SerializedProperty list)
9597
GUILayout.EndHorizontal();
9698
EditorGUILayout.Space();
9799

100+
bool changed = false;
101+
98102
for (int i = 0; i < list.arraySize; i++)
99103
{
100104
SerializedProperty managerConfig = list.GetArrayElementAtIndex(i);
@@ -113,9 +117,9 @@ private void RenderList(SerializedProperty list)
113117
{
114118
list.DeleteArrayElementAtIndex(i);
115119
serializedObject.ApplyModifiedProperties();
116-
MixedRealityToolkit.Instance.ResetConfiguration(MixedRealityToolkit.Instance.ActiveProfile);
117120
EditorGUILayout.EndHorizontal();
118121
GUILayout.EndVertical();
122+
changed = true;
119123
break;
120124
}
121125

@@ -124,21 +128,35 @@ private void RenderList(SerializedProperty list)
124128
if (configFoldouts[i])
125129
{
126130
EditorGUI.indentLevel++;
127-
EditorGUI.BeginChangeCheck();
128131

132+
EditorGUI.BeginChangeCheck();
129133
EditorGUILayout.PropertyField(componentName);
130-
EditorGUILayout.PropertyField(componentType);
131-
EditorGUILayout.PropertyField(priority);
132-
EditorGUILayout.PropertyField(runtimePlatform);
133-
EditorGUILayout.PropertyField(configurationProfile);
134+
changed |= EditorGUI.EndChangeCheck();
134135

136+
EditorGUI.BeginChangeCheck();
137+
EditorGUILayout.PropertyField(componentType);
135138
if (EditorGUI.EndChangeCheck())
136139
{
140+
// Try to assign default configuration profile when type changes.
137141
serializedObject.ApplyModifiedProperties();
138-
MixedRealityToolkit.Instance.ResetConfiguration(MixedRealityToolkit.Instance.ActiveProfile);
142+
AssignDefaultConfigurationValues(((MixedRealityRegisteredServiceProvidersProfile)serializedObject.targetObject).Configurations[i].ComponentType, configurationProfile, runtimePlatform);
143+
changed = true;
144+
145+
GUILayout.EndVertical();
146+
break;
139147
}
140148

149+
EditorGUI.BeginChangeCheck();
150+
EditorGUILayout.PropertyField(priority);
151+
EditorGUILayout.PropertyField(runtimePlatform);
152+
153+
changed |= EditorGUI.EndChangeCheck();
154+
155+
changed |= RenderProfile(configurationProfile);
156+
141157
EditorGUI.indentLevel--;
158+
159+
serializedObject.ApplyModifiedProperties();
142160
}
143161

144162
GUILayout.EndVertical();
@@ -147,6 +165,26 @@ private void RenderList(SerializedProperty list)
147165

148166
GUILayout.EndVertical();
149167
GUILayout.EndVertical();
168+
169+
if (changed)
170+
{
171+
EditorApplication.delayCall += () => MixedRealityToolkit.Instance.ResetConfiguration(MixedRealityToolkit.Instance.ActiveProfile);
172+
}
173+
}
174+
175+
private void AssignDefaultConfigurationValues(System.Type componentType, SerializedProperty configurationProfile, SerializedProperty runtimePlatform)
176+
{
177+
configurationProfile.objectReferenceValue = null;
178+
runtimePlatform.intValue = -1;
179+
180+
if (componentType != null &&
181+
MixedRealityExtensionServiceAttribute.Find(componentType) is MixedRealityExtensionServiceAttribute attr)
182+
{
183+
configurationProfile.objectReferenceValue = attr.DefaultProfile;
184+
runtimePlatform.intValue = (int)attr.RuntimePlatforms;
185+
}
186+
187+
serializedObject.ApplyModifiedProperties();
150188
}
151189
}
152190
}

Assets/MixedRealityToolkit/Utilities/Editor/Setup/MixedRealityEditorSettings.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ internal static bool FindDirectory(string directoryPathToSearch, string director
202202

203203
private static string MakePathRelativeToProject(string absolutePath)
204204
{
205-
return absolutePath.Replace(Application.dataPath + "\\", "Assets/");
205+
return absolutePath.Replace(
206+
Application.dataPath + Path.DirectorySeparatorChar,
207+
"Assets" + Path.DirectorySeparatorChar);
206208
}
207209

208210
private static void SetIconTheme()

0 commit comments

Comments
 (0)