private MixedRealityHandController GetOrAddController(Handedness handedness)
        {
            // If a device is already registered with the handedness, just return it.
            if (TryGetController(handedness, out var existingController))
            {
                return(existingController);
            }

            MixedRealityHandController detectedController;

            try
            {
                detectedController = new MixedRealityHandController(this, TrackingState.Tracked, handedness, GetControllerMappingProfile(typeof(MixedRealityHandController), handedness));
            }
            catch (Exception e)
            {
                Debug.LogError($"Failed to create {nameof(MixedRealityHandController)}!\n{e}");
                return(null);
            }

            detectedController.TryRenderControllerModel();
            AddController(detectedController);
            activeControllers.Add(handedness, detectedController);
            InputSystem?.RaiseSourceDetected(detectedController.InputSource, detectedController);

            return(detectedController);
        }
        private bool TryGetController(Handedness handedness, out MixedRealityHandController controller)
        {
            if (activeControllers.ContainsKey(handedness))
            {
                var existingController = activeControllers[handedness];
                Debug.Assert(existingController != null, $"Hand Controller {handedness} has been destroyed but remains in the active controller registry.");
                controller = existingController;
                return(true);
            }

            controller = null;
            return(false);
        }
        /// <summary>
        /// Creates a new <see cref="BaseController"/> instance for the provided <see cref="SpatialInteractionSource"/>.
        /// </summary>
        /// <param name="spatialInteractionSource">Source provided by the SDK.</param>
        /// <returns>The created controller instance ready for use.</returns>
        private BaseController CreateController(SpatialInteractionSource spatialInteractionSource)
        {
            var handedness     = spatialInteractionSource.Handedness.ToHandedness();
            var controllerType = spatialInteractionSource.Kind.ToControllerType();

            try
            {
                BaseController controller = null;
                if (controllerType == typeof(WindowsMixedRealityMotionController))
                {
                    controller = new WindowsMixedRealityMotionController(this, TrackingState.NotApplicable, handedness, GetControllerMappingProfile(controllerType, handedness));
                }
                else if (controllerType == typeof(WindowsMixedRealityHololensOneController))
                {
                    controller = new WindowsMixedRealityHololensOneController(this, TrackingState.NotApplicable, handedness, GetControllerMappingProfile(controllerType, handedness));
                }
                else if (controllerType == typeof(MixedRealityHandController))
                {
                    controller = new MixedRealityHandController(this, TrackingState.NotApplicable, handedness, GetControllerMappingProfile(controllerType, handedness));
                }
                else
                {
                    return(null);
                }

                InputSystem?.RaiseSourceDetected(controller.InputSource, controller);
                controller.TryRenderControllerModel();
                activeControllers.Add(spatialInteractionSource.Id, controller);
                AddController(controller);

                return(controller);
            }
            catch (Exception e)
            {
                Debug.LogError($"Failed to create {nameof(controllerType)}!\n{e}");
                return(null);
            }
        }