From b7066865d436243d9fadf1155f6c10d48ae0b03b Mon Sep 17 00:00:00 2001 From: Stephen Hodgson Date: Fri, 18 Dec 2020 12:04:29 -0500 Subject: [PATCH] fixed teleport component (#724) * fixed teleport component * updated examples submodule * updated examples checkout * renamed IMixedRealityTeleportComponentHandler -> IMixedRealityTeleportProvider * updated examples * fixed unit tests --- ...edRealityTeleportSystemProfileInspector.cs | 19 ++++--- .../MixedRealityTeleportSystemProfile.cs | 16 +++--- ...er.cs => IMixedRealityTeleportProvider.cs} | 2 +- ... => IMixedRealityTeleportProvider.cs.meta} | 0 .../MixedRealityTeleportSystem.cs | 49 +++++++++---------- Tests/Services/TestTeleportProvider.cs | 37 ++++++++++++++ Tests/Services/TestTeleportProvider.cs.meta | 11 +++++ Tests/TestUtilities.cs | 14 ++++-- 8 files changed, 106 insertions(+), 42 deletions(-) rename Runtime/Interfaces/TeleportSystem/Handlers/{IMixedRealityTeleportComponentHandler.cs => IMixedRealityTeleportProvider.cs} (81%) rename Runtime/Interfaces/TeleportSystem/Handlers/{IMixedRealityTeleportComponentHandler.cs.meta => IMixedRealityTeleportProvider.cs.meta} (100%) create mode 100644 Tests/Services/TestTeleportProvider.cs create mode 100644 Tests/Services/TestTeleportProvider.cs.meta diff --git a/Editor/Profiles/MixedRealityTeleportSystemProfileInspector.cs b/Editor/Profiles/MixedRealityTeleportSystemProfileInspector.cs index 6c292c355..360aebc86 100644 --- a/Editor/Profiles/MixedRealityTeleportSystemProfileInspector.cs +++ b/Editor/Profiles/MixedRealityTeleportSystemProfileInspector.cs @@ -3,19 +3,20 @@ using UnityEditor; using XRTK.Definitions.TeleportSystem; +using XRTK.Services; namespace XRTK.Editor.Profiles { [CustomEditor(typeof(MixedRealityTeleportSystemProfile))] public class MixedRealityTeleportSystemProfileInspector : MixedRealityServiceProfileInspector { - private SerializedProperty teleportHandlerComponent; + private SerializedProperty teleportProvider; protected override void OnEnable() { base.OnEnable(); - teleportHandlerComponent = serializedObject.FindProperty(nameof(teleportHandlerComponent)); + teleportProvider = serializedObject.FindProperty(nameof(teleportProvider)); } public override void OnInspectorGUI() @@ -23,13 +24,19 @@ public override void OnInspectorGUI() RenderHeader("The teleport system profile defines default behaviour for the teleport system."); serializedObject.Update(); + EditorGUI.BeginChangeCheck(); - EditorGUILayout.PropertyField(teleportHandlerComponent); - - EditorGUILayout.Space(); - base.OnInspectorGUI(); + EditorGUILayout.PropertyField(teleportProvider); serializedObject.ApplyModifiedProperties(); + + if (EditorGUI.EndChangeCheck() && + MixedRealityToolkit.IsInitialized) + { + EditorApplication.delayCall += () => MixedRealityToolkit.Instance.ResetProfile(MixedRealityToolkit.Instance.ActiveProfile); + } + + base.OnInspectorGUI(); } } } \ No newline at end of file diff --git a/Runtime/Definitions/TeleportSystem/MixedRealityTeleportSystemProfile.cs b/Runtime/Definitions/TeleportSystem/MixedRealityTeleportSystemProfile.cs index d33127b20..39e740686 100644 --- a/Runtime/Definitions/TeleportSystem/MixedRealityTeleportSystemProfile.cs +++ b/Runtime/Definitions/TeleportSystem/MixedRealityTeleportSystemProfile.cs @@ -16,13 +16,17 @@ namespace XRTK.Definitions.TeleportSystem public class MixedRealityTeleportSystemProfile : BaseMixedRealityServiceProfile { [SerializeField] - [Implements(typeof(IMixedRealityTeleportComponentHandler), TypeGrouping.ByNamespaceFlat)] - [Tooltip("The concrete teleport handler component to use for teleportation.")] - private SystemType teleportHandlerComponent = null; + [Tooltip("The concrete teleport provider to use for teleportation.")] + [Implements(typeof(IMixedRealityTeleportProvider), TypeGrouping.ByNamespaceFlat)] + private SystemType teleportProvider; /// - /// The concrete teleport handler component to use for teleportation. + /// The concrete teleport provider to use for teleportation. /// - public SystemType TeleportHandlerComponent => teleportHandlerComponent; + public SystemType TeleportProvider + { + get => teleportProvider; + internal set => teleportProvider = value; + } } -} \ No newline at end of file +} diff --git a/Runtime/Interfaces/TeleportSystem/Handlers/IMixedRealityTeleportComponentHandler.cs b/Runtime/Interfaces/TeleportSystem/Handlers/IMixedRealityTeleportProvider.cs similarity index 81% rename from Runtime/Interfaces/TeleportSystem/Handlers/IMixedRealityTeleportComponentHandler.cs rename to Runtime/Interfaces/TeleportSystem/Handlers/IMixedRealityTeleportProvider.cs index 3378d654b..9033bf180 100644 --- a/Runtime/Interfaces/TeleportSystem/Handlers/IMixedRealityTeleportComponentHandler.cs +++ b/Runtime/Interfaces/TeleportSystem/Handlers/IMixedRealityTeleportProvider.cs @@ -7,5 +7,5 @@ namespace XRTK.Interfaces.TeleportSystem.Handlers /// Interface to implement for handling teleport events by the /// in components. /// - public interface IMixedRealityTeleportComponentHandler : IMixedRealityTeleportHandler { } + public interface IMixedRealityTeleportProvider : IMixedRealityTeleportHandler { } } diff --git a/Runtime/Interfaces/TeleportSystem/Handlers/IMixedRealityTeleportComponentHandler.cs.meta b/Runtime/Interfaces/TeleportSystem/Handlers/IMixedRealityTeleportProvider.cs.meta similarity index 100% rename from Runtime/Interfaces/TeleportSystem/Handlers/IMixedRealityTeleportComponentHandler.cs.meta rename to Runtime/Interfaces/TeleportSystem/Handlers/IMixedRealityTeleportProvider.cs.meta diff --git a/Runtime/Services/TeleportSystem/MixedRealityTeleportSystem.cs b/Runtime/Services/TeleportSystem/MixedRealityTeleportSystem.cs index 9faf9e754..a586f8afa 100644 --- a/Runtime/Services/TeleportSystem/MixedRealityTeleportSystem.cs +++ b/Runtime/Services/TeleportSystem/MixedRealityTeleportSystem.cs @@ -1,6 +1,7 @@ // Copyright (c) XRTK. All rights reserved. // Licensed under the MIT License. See LICENSE in the project root for license information. +using System; using UnityEngine; using UnityEngine.EventSystems; using XRTK.Definitions.TeleportSystem; @@ -11,6 +12,7 @@ using XRTK.Interfaces.TeleportSystem; using XRTK.Interfaces.TeleportSystem.Handlers; using XRTK.Utilities; +using Object = UnityEngine.Object; namespace XRTK.Services.Teleportation { @@ -27,12 +29,18 @@ public class MixedRealityTeleportSystem : BaseEventSystem, IMixedRealityTeleport public MixedRealityTeleportSystem(MixedRealityTeleportSystemProfile profile) : base(profile) { - teleportHandlerComponent = profile.TeleportHandlerComponent; + if (profile.TeleportProvider?.Type == null) + { + throw new Exception($"The {nameof(MixedRealityTeleportSystemProfile)} is missing the required {teleportProvider}!"); + } + + teleportProvider = profile.TeleportProvider; } + private readonly SystemType teleportProvider; + private TeleportEventData teleportEventData; private bool isTeleporting = false; - private readonly SystemType teleportHandlerComponent; #region IMixedRealityService Implementation @@ -41,38 +49,27 @@ public override void Initialize() { base.Initialize(); - var checkedHandlerComponent = false; - if (!Application.isPlaying) - { - VerifyHandlerComponent(); - checkedHandlerComponent = true; - } - else + if (Application.isPlaying) { teleportEventData = new TeleportEventData(EventSystem.current); } - if (!checkedHandlerComponent) - { - VerifyHandlerComponent(); - } + CameraCache.Main.gameObject.EnsureComponent(teleportProvider.Type); } - private void VerifyHandlerComponent() + /// + public override void Disable() { - var activeHandler = (Object)CameraCache.Main.GetComponent(); - if (activeHandler.IsNull()) - { - // No teleport handler attached to the camera yet, we can safely - // add the configured handler. - CameraCache.Main.gameObject.EnsureComponent(teleportHandlerComponent.Type); - } - else if (activeHandler.GetType() != teleportHandlerComponent.Type) + base.Disable(); + + if (!Application.isPlaying) { - // There is handler attached to the camera but it's not the one configured - // in the profile. - Debug.LogWarning($"There is a {activeHandler.GetType().Name} attached to the camera but the active teleport system configuration requests a {teleportHandlerComponent.Type.Name}. " + - $"Likely you want to check your teleport system configuration."); + var component = CameraCache.Main.GetComponent() as Component; + + if (!component.IsNull()) + { + Object.DestroyImmediate(component); + } } } diff --git a/Tests/Services/TestTeleportProvider.cs b/Tests/Services/TestTeleportProvider.cs new file mode 100644 index 000000000..438549074 --- /dev/null +++ b/Tests/Services/TestTeleportProvider.cs @@ -0,0 +1,37 @@ +// Copyright (c) XRTK. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +using UnityEngine; +using XRTK.EventDatum.Teleport; +using XRTK.Interfaces.TeleportSystem.Handlers; + +public class TestTeleportProvider : MonoBehaviour, IMixedRealityTeleportProvider +{ + #region Implementation of IMixedRealityTeleportHandler + + /// + public void OnTeleportRequest(TeleportEventData eventData) + { + throw new System.NotImplementedException(); + } + + /// + public void OnTeleportStarted(TeleportEventData eventData) + { + throw new System.NotImplementedException(); + } + + /// + public void OnTeleportCompleted(TeleportEventData eventData) + { + throw new System.NotImplementedException(); + } + + /// + public void OnTeleportCanceled(TeleportEventData eventData) + { + throw new System.NotImplementedException(); + } + + #endregion +} diff --git a/Tests/Services/TestTeleportProvider.cs.meta b/Tests/Services/TestTeleportProvider.cs.meta new file mode 100644 index 000000000..d0ef6f8e8 --- /dev/null +++ b/Tests/Services/TestTeleportProvider.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: db897991d503b7a46b5c24956ee151a8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {fileID: 2800000, guid: 8ac5213854cf4dbabd140decf8df1946, type: 3} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Tests/TestUtilities.cs b/Tests/TestUtilities.cs index 9c2be85e0..23bb3eb68 100644 --- a/Tests/TestUtilities.cs +++ b/Tests/TestUtilities.cs @@ -37,9 +37,17 @@ public static void InitializeMixedRealityToolkitScene(bool useDefaultProfile) Assert.IsNotNull(MixedRealityToolkit.Instance); Assert.IsFalse(MixedRealityToolkit.HasActiveProfile); - var configuration = useDefaultProfile - ? GetDefaultMixedRealityProfile() - : ScriptableObject.CreateInstance(); + MixedRealityToolkitRootProfile configuration; + if (useDefaultProfile) + { + configuration = GetDefaultMixedRealityProfile(); + Debug.Assert(configuration.TeleportSystemProfile != null); + configuration.TeleportSystemProfile.TeleportProvider = typeof(TestTeleportProvider); + } + else + { + configuration = ScriptableObject.CreateInstance(); + } Assert.IsTrue(configuration != null, "Failed to find the Default Mixed Reality Root Profile"); MixedRealityToolkit.Instance.ResetProfile(configuration);