Skip to content

Commit 81d3af0

Browse files
committed
Become more robust to parallel tasks
1 parent b0c0b79 commit 81d3af0

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

Assets/MRTK/Providers/OpenXR/Scripts/MicrosoftControllerModelProvider.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ public async Task<GameObject> TryGenerateControllerModelFromPlatformSDK()
4646
return null;
4747
}
4848

49-
if (ControllerModelDictionary.TryGetValue(modelKey, out GameObject controllerModel))
49+
if (ControllerModelDictionary.TryGetValue(modelKey, out gltfGameObject))
5050
{
51-
controllerModel.SetActive(true);
52-
return controllerModel;
51+
gltfGameObject.SetActive(true);
52+
return gltfGameObject;
5353
}
5454

5555
byte[] modelStream = await controllerModelProvider.TryGetControllerModel(modelKey);
@@ -65,7 +65,16 @@ public async Task<GameObject> TryGenerateControllerModelFromPlatformSDK()
6565

6666
if (gltfGameObject != null)
6767
{
68-
ControllerModelDictionary.Add(modelKey, gltfGameObject);
68+
// After all the awaits, double check that another task didn't finish earlier
69+
if (ControllerModelDictionary.TryGetValue(modelKey, out GameObject existingGameObject))
70+
{
71+
Object.Destroy(gltfGameObject);
72+
return existingGameObject;
73+
}
74+
else
75+
{
76+
ControllerModelDictionary.Add(modelKey, gltfGameObject);
77+
}
6978
}
7079
#endif // MSFT_OPENXR_0_9_4_OR_NEWER
7180

Assets/MRTK/Providers/WindowsMixedReality/Shared/WindowsMixedRealityControllerModelProvider.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ public async Task<GameObject> TryGenerateControllerModelFromPlatformSDK()
5050
return null;
5151
}
5252

53+
string key = GenerateKey(spatialInteractionSource);
54+
5355
// See if we've generated this model before and if we can return it
54-
if (ControllerModelDictionary.TryGetValue(GenerateKey(spatialInteractionSource), out GameObject controllerModel))
56+
if (ControllerModelDictionary.TryGetValue(key, out gltfGameObject))
5557
{
56-
controllerModel.SetActive(true);
57-
return controllerModel;
58+
gltfGameObject.SetActive(true);
59+
return gltfGameObject;
5860
}
5961

6062
Debug.Log("Trying to load controller model from platform SDK");
@@ -82,7 +84,16 @@ public async Task<GameObject> TryGenerateControllerModelFromPlatformSDK()
8284
gltfGameObject = await gltfObject.ConstructAsync();
8385
if (gltfGameObject != null)
8486
{
85-
ControllerModelDictionary.Add(GenerateKey(spatialInteractionSource), gltfGameObject);
87+
// After all the awaits, double check that another task didn't finish earlier
88+
if (ControllerModelDictionary.TryGetValue(key, out GameObject existingGameObject))
89+
{
90+
UnityEngine.Object.Destroy(gltfGameObject);
91+
return existingGameObject;
92+
}
93+
else
94+
{
95+
ControllerModelDictionary.Add(key, gltfGameObject);
96+
}
8697
}
8798
}
8899
#endif // WINDOWS_UWP

0 commit comments

Comments
 (0)