Skip to content

Commit 418a0f0

Browse files
authored
Fix null ref when stopping playmode with spatial mapping (#10593)
* null check the GameObject before accessing * Use non-obsolete property
1 parent 28bccb2 commit 418a0f0

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

Assets/MRTK/Examples/Common/Scripts/DemoSpatialMeshHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public virtual void OnObservationRemoved(MixedRealitySpatialAwarenessEventData<S
9898
protected void AddToData(int eventDataId)
9999
{
100100
// A new mesh has been added.
101-
Debug.Log($"Tracking mesh {eventDataId}");
101+
Debug.Log($"Started tracking mesh {eventDataId}");
102102
meshUpdateData.Add(eventDataId, 0);
103103
}
104104

Assets/MRTK/Providers/WindowsMixedReality/XR2018/WindowsMixedRealitySpatialMeshObserver.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public WindowsMixedRealitySpatialMeshObserver(
7272
/// </summary>
7373
protected override void CreateObserver()
7474
{
75-
if (SpatialAwarenessSystem == null) { return; }
75+
if (Service == null) { return; }
7676

7777
#if UNITY_WSA
7878
if (observer == null)
@@ -352,7 +352,7 @@ public override void ClearObservations()
352352
/// </summary>
353353
private void UpdateObserver()
354354
{
355-
if (SpatialAwarenessSystem == null || HolographicSettings.IsDisplayOpaque || !XRDevice.isPresent) { return; }
355+
if (Service == null || HolographicSettings.IsDisplayOpaque || !XRDevice.isPresent) { return; }
356356

357357
using (UpdateObserverPerfMarker.Auto())
358358
{
@@ -525,7 +525,7 @@ protected void RemoveMeshObject(int id)
525525

526526
// Send the mesh removed event
527527
meshEventData.Initialize(this, id, null);
528-
SpatialAwarenessSystem?.HandleEvent(meshEventData, OnMeshRemoved);
528+
Service?.HandleEvent(meshEventData, OnMeshRemoved);
529529
}
530530
}
531531
}
@@ -545,8 +545,11 @@ protected void ReclaimMeshObject(SpatialAwarenessMeshObject availableMeshObject)
545545
// Do not destroy the game object, destroy the meshes.
546546
SpatialAwarenessMeshObject.Cleanup(availableMeshObject, false);
547547

548-
availableMeshObject.GameObject.name = "Unused Spatial Mesh";
549-
availableMeshObject.GameObject.SetActive(false);
548+
if (availableMeshObject.GameObject != null)
549+
{
550+
availableMeshObject.GameObject.name = "Unused Spatial Mesh";
551+
availableMeshObject.GameObject.SetActive(false);
552+
}
550553

551554
spareMeshObject = availableMeshObject;
552555
}
@@ -713,11 +716,11 @@ private void SurfaceObserver_OnDataReady(SurfaceData cookedData, bool outputWrit
713716
meshEventData.Initialize(this, meshObject.Id, meshObject);
714717
if (isMeshUpdate)
715718
{
716-
SpatialAwarenessSystem?.HandleEvent(meshEventData, OnMeshUpdated);
719+
Service?.HandleEvent(meshEventData, OnMeshUpdated);
717720
}
718721
else
719722
{
720-
SpatialAwarenessSystem?.HandleEvent(meshEventData, OnMeshAdded);
723+
Service?.HandleEvent(meshEventData, OnMeshAdded);
721724
}
722725
}
723726
}

Assets/MRTK/Providers/XRSDK/GenericXRSDKSpatialMeshObserver.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,7 @@ protected void RemoveMeshObject(int id)
407407
{
408408
using (RemoveMeshObjectPerfMarker.Auto())
409409
{
410-
SpatialAwarenessMeshObject mesh;
411-
if (meshes.TryGetValue(id, out mesh))
410+
if (meshes.TryGetValue(id, out SpatialAwarenessMeshObject mesh))
412411
{
413412
// Remove the mesh object from the collection.
414413
meshes.Remove(id);
@@ -438,8 +437,11 @@ protected void ReclaimMeshObject(SpatialAwarenessMeshObject availableMeshObject)
438437
// Do not destroy the game object, destroy the meshes.
439438
SpatialAwarenessMeshObject.Cleanup(availableMeshObject, false);
440439

441-
availableMeshObject.GameObject.name = "Unused Spatial Mesh";
442-
availableMeshObject.GameObject.SetActive(false);
440+
if (availableMeshObject.GameObject != null)
441+
{
442+
availableMeshObject.GameObject.name = "Unused Spatial Mesh";
443+
availableMeshObject.GameObject.SetActive(false);
444+
}
443445

444446
spareMeshObject = availableMeshObject;
445447
}

0 commit comments

Comments
 (0)