/// <summary> /// Attempts to load the glTF controller model from the Windows SDK. /// </summary> /// <returns>The controller model as a GameObject or null if it was unobtainable.</returns> public async Task <GameObject> TryGenerateControllerModelFromPlatformSDK() { if (spatialInteractionSource == null) { return(null); } // See if we've generated this model before and if we can return it if (ControllerModelDictionary.TryGetValue(GenerateKey(spatialInteractionSource), out GameObject controllerModel)) { controllerModel.SetActive(true); return(controllerModel); } Debug.Log("Trying to load controller model from platform SDK"); byte[] fileBytes = null; var controllerModelStream = await spatialInteractionSource.Controller.TryGetRenderableModelAsync(); if (controllerModelStream == null || controllerModelStream.Size == 0) { Debug.LogError("Failed to obtain controller model from driver"); } else { fileBytes = new byte[controllerModelStream.Size]; using (DataReader reader = new DataReader(controllerModelStream)) { await reader.LoadAsync((uint)controllerModelStream.Size); reader.ReadBytes(fileBytes); } } GameObject gltfGameObject = null; if (fileBytes != null) { Utilities.Gltf.Schema.GltfObject gltfObject = GltfUtility.GetGltfObjectFromGlb(fileBytes); gltfGameObject = await gltfObject.ConstructAsync(); if (gltfGameObject != null) { ControllerModelDictionary.Add(GenerateKey(spatialInteractionSource), gltfGameObject); } } return(gltfGameObject); }
// Disables "This async method lacks 'await' operators and will run synchronously." when the correct OpenXR package isn't installed #pragma warning disable CS1998 /// <summary> /// Attempts to load the glTF controller model from OpenXR. /// </summary> /// <returns>The controller model as a GameObject or null if it was unobtainable.</returns> public async Task <GameObject> TryGenerateControllerModelFromPlatformSDK() { GameObject gltfGameObject = null; #if MSFT_OPENXR && (UNITY_STANDALONE_WIN || UNITY_WSA || UNITY_ANDROID) if (!controllerModelProvider.TryGetControllerModelKey(out ulong modelKey)) { Debug.LogError("Failed to obtain controller model key from platform."); return(null); } if (ControllerModelDictionary.TryGetValue(modelKey, out gltfGameObject)) { gltfGameObject.SetActive(true); return(gltfGameObject); } byte[] modelStream = await controllerModelProvider.TryGetControllerModel(modelKey); if (modelStream == null || modelStream.Length == 0) { Debug.LogError("Failed to obtain controller model from platform."); return(null); } Utilities.Gltf.Schema.GltfObject gltfObject = GltfUtility.GetGltfObjectFromGlb(modelStream); gltfGameObject = await gltfObject.ConstructAsync(); if (gltfGameObject != null) { // After all the awaits, double check that another task didn't finish earlier if (ControllerModelDictionary.TryGetValue(modelKey, out GameObject existingGameObject)) { Object.Destroy(gltfGameObject); return(existingGameObject); } else { ControllerModelDictionary.Add(modelKey, gltfGameObject); } } #endif // MSFT_OPENXR && (UNITY_STANDALONE_WIN || UNITY_WSA || UNITY_ANDROID) return(gltfGameObject); }
// Disables "This async method lacks 'await' operators and will run synchronously." for non-UWP #pragma warning disable CS1998 /// <summary> /// Attempts to load the glTF controller model from the Windows SDK. /// </summary> /// <returns>The controller model as a GameObject or null if it was unobtainable.</returns> public async Task <GameObject> TryGenerateControllerModelFromPlatformSDK() { GameObject gltfGameObject = null; #if WINDOWS_UWP if (spatialInteractionSource == null) { return(null); } string key = GenerateKey(spatialInteractionSource); // See if we've generated this model before and if we can return it if (ControllerModelDictionary.TryGetValue(key, out gltfGameObject)) { gltfGameObject.SetActive(true); return(gltfGameObject); } Debug.Log("Trying to load controller model from platform SDK"); byte[] fileBytes = null; var controllerModelStream = await spatialInteractionSource.Controller.TryGetRenderableModelAsync(); if (controllerModelStream == null || controllerModelStream.Size == 0) { Debug.LogError("Failed to obtain controller model from driver"); } else { fileBytes = new byte[controllerModelStream.Size]; using (DataReader reader = new DataReader(controllerModelStream)) { await reader.LoadAsync((uint)controllerModelStream.Size); reader.ReadBytes(fileBytes); } } if (fileBytes != null) { Utilities.Gltf.Schema.GltfObject gltfObject = GltfUtility.GetGltfObjectFromGlb(fileBytes); gltfGameObject = await gltfObject.ConstructAsync(); if (gltfGameObject != null) { // After all the awaits, double check that another task didn't finish earlier if (ControllerModelDictionary.TryGetValue(key, out GameObject existingGameObject)) { UnityEngine.Object.Destroy(gltfGameObject); return(existingGameObject); } else { ControllerModelDictionary.Add(key, gltfGameObject); } } } #endif // WINDOWS_UWP return(gltfGameObject); }