diff --git a/Assets/MRTK/Core/Utilities/CoreServices.cs b/Assets/MRTK/Core/Utilities/CoreServices.cs index fcc11a381d7..88925a78c51 100644 --- a/Assets/MRTK/Core/Utilities/CoreServices.cs +++ b/Assets/MRTK/Core/Utilities/CoreServices.cs @@ -20,64 +20,94 @@ namespace Microsoft.MixedReality.Toolkit /// public static class CoreServices { + private static IMixedRealityBoundarySystem boundarySystem; + /// /// Cached reference to the active instance of the boundary system. /// If system is destroyed, reference will be invalid. Please use ResetCacheReferences() /// - public static IMixedRealityBoundarySystem BoundarySystem => GetService(); + public static IMixedRealityBoundarySystem BoundarySystem => boundarySystem ?? (boundarySystem = GetService()); + + private static IMixedRealityCameraSystem cameraSystem; /// /// Cached reference to the active instance of the camera system. /// If system is destroyed, reference will be invalid. Please use ResetCacheReferences() /// - public static IMixedRealityCameraSystem CameraSystem => GetService(); + public static IMixedRealityCameraSystem CameraSystem => cameraSystem ?? (cameraSystem = GetService()); + + private static IMixedRealityDiagnosticsSystem diagnosticsSystem; /// /// Cached reference to the active instance of the diagnostics system. /// If system is destroyed, reference will be invalid. Please use ResetCacheReferences() /// - public static IMixedRealityDiagnosticsSystem DiagnosticsSystem => GetService(); + public static IMixedRealityDiagnosticsSystem DiagnosticsSystem => diagnosticsSystem ?? (diagnosticsSystem = GetService()); + + private static IMixedRealityFocusProvider focusProvider; /// /// Cached reference to the active instance of the focus provider. /// If system is destroyed, reference will be invalid. Please use ResetCacheReferences() /// - public static IMixedRealityFocusProvider FocusProvider => GetService(); + public static IMixedRealityFocusProvider FocusProvider => focusProvider ?? (focusProvider = GetService()); + + private static IMixedRealityInputSystem inputSystem; /// /// Cached reference to the active instance of the input system. /// If system is destroyed, reference will be invalid. Please use ResetCacheReferences() /// - public static IMixedRealityInputSystem InputSystem => GetService(); + public static IMixedRealityInputSystem InputSystem => inputSystem ?? (inputSystem = GetService()); + + private static IMixedRealityRaycastProvider raycastProvider; /// /// Cached reference to the active instance of the raycast provider. /// If system is destroyed, reference will be invalid. Please use ResetCacheReferences() /// - public static IMixedRealityRaycastProvider RaycastProvider => GetService(); + public static IMixedRealityRaycastProvider RaycastProvider => raycastProvider ?? (raycastProvider = GetService()); + + private static IMixedRealitySceneSystem sceneSystem; /// /// Cached reference to the active instance of the scene system. /// If system is destroyed, reference will be invalid. Please use ResetCacheReferences() /// - public static IMixedRealitySceneSystem SceneSystem => GetService(); + public static IMixedRealitySceneSystem SceneSystem => sceneSystem ?? (sceneSystem = GetService()); + + private static IMixedRealitySpatialAwarenessSystem spatialAwarenessSystem; /// /// Cached reference to the active instance of the spatial awareness system. /// If system is destroyed, reference will be invalid. Please use ResetCacheReferences() /// - public static IMixedRealitySpatialAwarenessSystem SpatialAwarenessSystem => GetService(); + public static IMixedRealitySpatialAwarenessSystem SpatialAwarenessSystem => spatialAwarenessSystem ?? (spatialAwarenessSystem = GetService()); + + private static IMixedRealityTeleportSystem teleportSystem; /// /// Cached reference to the active instance of the teleport system. /// If system is destroyed, reference will be invalid. Please use ResetCacheReferences() /// - public static IMixedRealityTeleportSystem TeleportSystem => GetService(); + public static IMixedRealityTeleportSystem TeleportSystem => teleportSystem ?? (teleportSystem = GetService()); /// /// Resets all cached system references to null /// - public static void ResetCacheReferences() => serviceCache.Clear(); + public static void ResetCacheReferences() + { + serviceCache.Clear(); + boundarySystem = null; + cameraSystem = null; + diagnosticsSystem = null; + focusProvider = null; + inputSystem = null; + raycastProvider = null; + sceneSystem = null; + spatialAwarenessSystem = null; + teleportSystem = null; + } /// /// Clears the cache of the reference with key of given type if present and applicable @@ -91,6 +121,7 @@ public static bool ResetCacheReference(Type serviceType) if (serviceCache.ContainsKey(serviceType)) { serviceCache.Remove(serviceType); + ResetCacheReferenceFromType(serviceType); return true; } } @@ -102,6 +133,46 @@ public static bool ResetCacheReference(Type serviceType) return false; } + private static void ResetCacheReferenceFromType(Type serviceType) + { + if (typeof(IMixedRealityBoundarySystem).IsAssignableFrom(serviceType)) + { + boundarySystem = null; + } + if (typeof(IMixedRealityCameraSystem).IsAssignableFrom(serviceType)) + { + cameraSystem = null; + } + if (typeof(IMixedRealityDiagnosticsSystem).IsAssignableFrom(serviceType)) + { + diagnosticsSystem = null; + } + if (typeof(IMixedRealityFocusProvider).IsAssignableFrom(serviceType)) + { + focusProvider = null; + } + if (typeof(IMixedRealityInputSystem).IsAssignableFrom(serviceType)) + { + inputSystem = null; + } + if (typeof(IMixedRealityRaycastProvider).IsAssignableFrom(serviceType)) + { + raycastProvider = null; + } + if (typeof(IMixedRealitySceneSystem).IsAssignableFrom(serviceType)) + { + sceneSystem = null; + } + if (typeof(IMixedRealitySpatialAwarenessSystem).IsAssignableFrom(serviceType)) + { + sceneSystem = null; + } + if (typeof(IMixedRealityTeleportSystem).IsAssignableFrom(serviceType)) + { + teleportSystem = null; + } + } + /// /// Gets first matching or extension thereof for CoreServices.InputSystem /// @@ -147,7 +218,7 @@ public static T GetDataProvider(IMixedRealityService service) where T : IMixe // We do not want to keep a service around so use WeakReference private static readonly Dictionary> serviceCache = new Dictionary>(); - private static T GetService() where T : IMixedRealityService + private static T GetService() where T : IMixedRealityService { Type serviceType = typeof(T); @@ -176,4 +247,4 @@ private static T GetService() where T : IMixedRealityService return service; } } -} +} \ No newline at end of file