示例#1
0
    void LoadOffThread(uint controllerId, SpatialInteractionController controller, SpatialInteractionSourceHandedness handedness)
    {
#if UNITY_WSA && ENABLE_WINMD_SUPPORT
        System.Diagnostics.Debug.WriteLine("ControllerModelProvider Loading Model");
        System.Threading.Tasks.Task.Run(() =>
        {
            try
            {
                var getModelTask = controller.TryGetRenderableModelAsync().AsTask();
                getModelTask.Wait();
                var modelStream  = getModelTask.Result;
                byte[] fileBytes = new byte[modelStream.Size];
                using (DataReader reader = new DataReader(modelStream))
                {
                    var loadTask = reader.LoadAsync((uint)modelStream.Size).AsTask();
                    loadTask.Wait();
                    reader.ReadBytes(fileBytes);
                    System.Diagnostics.Debug.WriteLine("LoadOffThread read: " + fileBytes.Length);
                }
                AddController(controllerId, controller, fileBytes, handedness);
            } catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                System.Diagnostics.Debug.WriteLine(ex.StackTrace);
            }
        });
#endif
    }
        private IEnumerator LoadControllerModel(SpatialInteractionController controller, SpatialInteractionSource source)
        {
            GameObject controllerModelGameObject;

            if (source.Handedness == SpatialInteractionSourceHandedness.Left && LeftControllerOverride != null)
            {
                controllerModelGameObject = Instantiate(LeftControllerOverride);
            }
            else if (source.Handedness == SpatialInteractionSourceHandedness.Right && RightControllerOverride != null)
            {
                controllerModelGameObject = Instantiate(RightControllerOverride);
            }
            else
            {
                if (GLTFMaterial == null)
                {
                    Debug.Log("If using glTF, please specify a material on " + name + ".");
                    yield break;
                }

                // This API returns the appropriate glTF file according to the motion controller you're currently using, if supported.
                IAsyncOperation <IRandomAccessStreamWithContentType> modelTask = controller.TryGetRenderableModelAsync();

                if (modelTask == null)
                {
                    Debug.Log("Model task is null.");
                    yield break;
                }

                while (modelTask.Status == AsyncStatus.Started)
                {
                    yield return(null);
                }

                IRandomAccessStreamWithContentType modelStream = modelTask.GetResults();

                if (modelStream == null)
                {
                    Debug.Log("Model stream is null.");
                    yield break;
                }

                if (modelStream.Size == 0)
                {
                    Debug.Log("Model stream is empty.");
                    yield break;
                }

                byte[] fileBytes = new byte[modelStream.Size];

                using (DataReader reader = new DataReader(modelStream))
                {
                    DataReaderLoadOperation loadModelOp = reader.LoadAsync((uint)modelStream.Size);

                    while (loadModelOp.Status == AsyncStatus.Started)
                    {
                        yield return(null);
                    }

                    reader.ReadBytes(fileBytes);
                }

                controllerModelGameObject = new GameObject();
                GLTFComponentStreamingAssets gltfScript = controllerModelGameObject.AddComponent <GLTFComponentStreamingAssets>();
                gltfScript.ColorMaterial   = GLTFMaterial;
                gltfScript.NoColorMaterial = GLTFMaterial;
                gltfScript.GLTFData        = fileBytes;

                yield return(gltfScript.LoadModel());
            }

            FinishControllerSetup(controllerModelGameObject, source.Handedness.ToString(), source.Id);
        }