@@ -20,64 +20,94 @@ namespace Microsoft.MixedReality.Toolkit
20
20
/// </summary>
21
21
public static class CoreServices
22
22
{
23
+ private static IMixedRealityBoundarySystem boundarySystem ;
24
+
23
25
/// <summary>
24
26
/// Cached reference to the active instance of the boundary system.
25
27
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
26
28
/// </summary>
27
- public static IMixedRealityBoundarySystem BoundarySystem => GetService < IMixedRealityBoundarySystem > ( ) ;
29
+ public static IMixedRealityBoundarySystem BoundarySystem => boundarySystem ?? ( boundarySystem = GetService < IMixedRealityBoundarySystem > ( ) ) ;
30
+
31
+ private static IMixedRealityCameraSystem cameraSystem ;
28
32
29
33
/// <summary>
30
34
/// Cached reference to the active instance of the camera system.
31
35
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
32
36
/// </summary>
33
- public static IMixedRealityCameraSystem CameraSystem => GetService < IMixedRealityCameraSystem > ( ) ;
37
+ public static IMixedRealityCameraSystem CameraSystem => cameraSystem ?? ( cameraSystem = GetService < IMixedRealityCameraSystem > ( ) ) ;
38
+
39
+ private static IMixedRealityDiagnosticsSystem diagnosticsSystem ;
34
40
35
41
/// <summary>
36
42
/// Cached reference to the active instance of the diagnostics system.
37
43
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
38
44
/// </summary>
39
- public static IMixedRealityDiagnosticsSystem DiagnosticsSystem => GetService < IMixedRealityDiagnosticsSystem > ( ) ;
45
+ public static IMixedRealityDiagnosticsSystem DiagnosticsSystem => diagnosticsSystem ?? ( diagnosticsSystem = GetService < IMixedRealityDiagnosticsSystem > ( ) ) ;
46
+
47
+ private static IMixedRealityFocusProvider focusProvider ;
40
48
41
49
/// <summary>
42
50
/// Cached reference to the active instance of the focus provider.
43
51
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
44
52
/// </summary>
45
- public static IMixedRealityFocusProvider FocusProvider => GetService < IMixedRealityFocusProvider > ( ) ;
53
+ public static IMixedRealityFocusProvider FocusProvider => focusProvider ?? ( focusProvider = GetService < IMixedRealityFocusProvider > ( ) ) ;
54
+
55
+ private static IMixedRealityInputSystem inputSystem ;
46
56
47
57
/// <summary>
48
58
/// Cached reference to the active instance of the input system.
49
59
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
50
60
/// </summary>
51
- public static IMixedRealityInputSystem InputSystem => GetService < IMixedRealityInputSystem > ( ) ;
61
+ public static IMixedRealityInputSystem InputSystem => inputSystem ?? ( inputSystem = GetService < IMixedRealityInputSystem > ( ) ) ;
62
+
63
+ private static IMixedRealityRaycastProvider raycastProvider ;
52
64
53
65
/// <summary>
54
66
/// Cached reference to the active instance of the raycast provider.
55
67
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
56
68
/// </summary>
57
- public static IMixedRealityRaycastProvider RaycastProvider => GetService < IMixedRealityRaycastProvider > ( ) ;
69
+ public static IMixedRealityRaycastProvider RaycastProvider => raycastProvider ?? ( raycastProvider = GetService < IMixedRealityRaycastProvider > ( ) ) ;
70
+
71
+ private static IMixedRealitySceneSystem sceneSystem ;
58
72
59
73
/// <summary>
60
74
/// Cached reference to the active instance of the scene system.
61
75
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
62
76
/// </summary>
63
- public static IMixedRealitySceneSystem SceneSystem => GetService < IMixedRealitySceneSystem > ( ) ;
77
+ public static IMixedRealitySceneSystem SceneSystem => sceneSystem ?? ( sceneSystem = GetService < IMixedRealitySceneSystem > ( ) ) ;
78
+
79
+ private static IMixedRealitySpatialAwarenessSystem spatialAwarenessSystem ;
64
80
65
81
/// <summary>
66
82
/// Cached reference to the active instance of the spatial awareness system.
67
83
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
68
84
/// </summary>
69
- public static IMixedRealitySpatialAwarenessSystem SpatialAwarenessSystem => GetService < IMixedRealitySpatialAwarenessSystem > ( ) ;
85
+ public static IMixedRealitySpatialAwarenessSystem SpatialAwarenessSystem => spatialAwarenessSystem ?? ( spatialAwarenessSystem = GetService < IMixedRealitySpatialAwarenessSystem > ( ) ) ;
86
+
87
+ private static IMixedRealityTeleportSystem teleportSystem ;
70
88
71
89
/// <summary>
72
90
/// Cached reference to the active instance of the teleport system.
73
91
/// If system is destroyed, reference will be invalid. Please use ResetCacheReferences()
74
92
/// </summary>
75
- public static IMixedRealityTeleportSystem TeleportSystem => GetService < IMixedRealityTeleportSystem > ( ) ;
93
+ public static IMixedRealityTeleportSystem TeleportSystem => teleportSystem ?? ( teleportSystem = GetService < IMixedRealityTeleportSystem > ( ) ) ;
76
94
77
95
/// <summary>
78
96
/// Resets all cached system references to null
79
97
/// </summary>
80
- public static void ResetCacheReferences ( ) => serviceCache . Clear ( ) ;
98
+ public static void ResetCacheReferences ( )
99
+ {
100
+ serviceCache . Clear ( ) ;
101
+ boundarySystem = null ;
102
+ cameraSystem = null ;
103
+ diagnosticsSystem = null ;
104
+ focusProvider = null ;
105
+ inputSystem = null ;
106
+ raycastProvider = null ;
107
+ sceneSystem = null ;
108
+ spatialAwarenessSystem = null ;
109
+ teleportSystem = null ;
110
+ }
81
111
82
112
/// <summary>
83
113
/// 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)
91
121
if ( serviceCache . ContainsKey ( serviceType ) )
92
122
{
93
123
serviceCache . Remove ( serviceType ) ;
124
+ ResetCacheReferenceFromType ( serviceType ) ;
94
125
return true ;
95
126
}
96
127
}
@@ -102,6 +133,46 @@ public static bool ResetCacheReference(Type serviceType)
102
133
return false ;
103
134
}
104
135
136
+ private static void ResetCacheReferenceFromType ( Type serviceType )
137
+ {
138
+ if ( typeof ( IMixedRealityBoundarySystem ) . IsAssignableFrom ( serviceType ) )
139
+ {
140
+ boundarySystem = null ;
141
+ }
142
+ if ( typeof ( IMixedRealityCameraSystem ) . IsAssignableFrom ( serviceType ) )
143
+ {
144
+ cameraSystem = null ;
145
+ }
146
+ if ( typeof ( IMixedRealityDiagnosticsSystem ) . IsAssignableFrom ( serviceType ) )
147
+ {
148
+ diagnosticsSystem = null ;
149
+ }
150
+ if ( typeof ( IMixedRealityFocusProvider ) . IsAssignableFrom ( serviceType ) )
151
+ {
152
+ focusProvider = null ;
153
+ }
154
+ if ( typeof ( IMixedRealityInputSystem ) . IsAssignableFrom ( serviceType ) )
155
+ {
156
+ inputSystem = null ;
157
+ }
158
+ if ( typeof ( IMixedRealityRaycastProvider ) . IsAssignableFrom ( serviceType ) )
159
+ {
160
+ raycastProvider = null ;
161
+ }
162
+ if ( typeof ( IMixedRealitySceneSystem ) . IsAssignableFrom ( serviceType ) )
163
+ {
164
+ sceneSystem = null ;
165
+ }
166
+ if ( typeof ( IMixedRealitySpatialAwarenessSystem ) . IsAssignableFrom ( serviceType ) )
167
+ {
168
+ sceneSystem = null ;
169
+ }
170
+ if ( typeof ( IMixedRealityTeleportSystem ) . IsAssignableFrom ( serviceType ) )
171
+ {
172
+ teleportSystem = null ;
173
+ }
174
+ }
175
+
105
176
/// <summary>
106
177
/// Gets first matching <see cref="Microsoft.MixedReality.Toolkit.Input.IMixedRealityInputDeviceManager"/> or extension thereof for CoreServices.InputSystem
107
178
/// </summary>
@@ -147,7 +218,7 @@ public static T GetDataProvider<T>(IMixedRealityService service) where T : IMixe
147
218
// We do not want to keep a service around so use WeakReference
148
219
private static readonly Dictionary < Type , WeakReference < IMixedRealityService > > serviceCache = new Dictionary < Type , WeakReference < IMixedRealityService > > ( ) ;
149
220
150
- private static T GetService < T > ( ) where T : IMixedRealityService
221
+ private static T GetService < T > ( ) where T : IMixedRealityService
151
222
{
152
223
Type serviceType = typeof ( T ) ;
153
224
@@ -176,4 +247,4 @@ private static T GetService<T>() where T : IMixedRealityService
176
247
return service ;
177
248
}
178
249
}
179
- }
250
+ }
0 commit comments