/// <summary> /// Try to render a controller model for this controller from the visualization profile. /// </summary> /// <param name="controllerType">The type of controller to load the model for.</param> /// <param name="inputSourceType">Whether the model represents a hand or a controller.</param> /// <returns>True if a model was successfully loaded or model rendering is disabled. False if a model tried to load but failed.</returns> protected virtual bool TryRenderControllerModel(Type controllerType, InputSourceType inputSourceType) { MixedRealityControllerVisualizationProfile controllerVisualizationProfile = GetControllerVisualizationProfile(); bool controllerVisualizationProfilePresent = controllerVisualizationProfile != null; if (!controllerVisualizationProfilePresent || !controllerVisualizationProfile.RenderMotionControllers) { return(true); } // Try to use the profile's override model first GameObject controllerModel = controllerVisualizationProfile.GetControllerModelOverride(controllerType, ControllerHandedness); // If the Controller model is still null in the end, use the global defaults. if (controllerModel == null) { if (inputSourceType == InputSourceType.Controller) { if (ControllerHandedness == Handedness.Left && controllerVisualizationProfile.GlobalLeftHandModel != null) { controllerModel = controllerVisualizationProfile.GlobalLeftHandModel; } else if (ControllerHandedness == Handedness.Right && controllerVisualizationProfile.GlobalRightHandModel != null) { controllerModel = controllerVisualizationProfile.GlobalRightHandModel; } } else if (inputSourceType == InputSourceType.Hand) { if (ControllerHandedness == Handedness.Left && controllerVisualizationProfile.GlobalLeftHandVisualizer != null) { controllerModel = controllerVisualizationProfile.GlobalLeftHandVisualizer; } else if (ControllerHandedness == Handedness.Right && controllerVisualizationProfile.GlobalRightHandVisualizer != null) { controllerModel = controllerVisualizationProfile.GlobalRightHandVisualizer; } } } if (controllerModel == null) { // no controller model available return(false); } // If we've got a controller model prefab, then create it and place it in the scene. GameObject controllerObject = UnityEngine.Object.Instantiate(controllerModel); return(TryAddControllerModelToSceneHierarchy(controllerObject)); }
/// <summary> /// Tries to read the controller visualization profile to apply a visualization script to the passed-in controller model. /// </summary> /// <remarks>Automatically disables DestroyOnSourceLost to encourage controller model creators to manage their life-cycle themselves.</remarks> /// <param name="controllerModel">The GameObject to modify.</param> /// <param name="controllerType">The type of controller this model represents.</param> /// <param name="handedness">The handedness of this controller.</param> /// <returns>True if a visualization script could be loaded and applied.</returns> public static bool TryAddVisualizationScript(GameObject controllerModel, Type controllerType, Handedness handedness) { if (controllerModel != null) { if (visualizationProfile == null && CoreServices.InputSystem?.InputSystemProfile != null) { visualizationProfile = CoreServices.InputSystem.InputSystemProfile.ControllerVisualizationProfile; } if (visualizationProfile != null) { var visualizationType = visualizationProfile.GetControllerVisualizationTypeOverride(controllerType, handedness); if (visualizationType != null) { // Set the platform controller model to not be destroyed when the source is lost. It'll be disabled instead, // and re-enabled when the same controller is re-detected. if (controllerModel.EnsureComponent(visualizationType.Type) is IMixedRealityControllerPoseSynchronizer visualizer) { visualizer.DestroyOnSourceLost = false; } return(true); } else { Debug.LogError("Controller visualization type not defined for controller visualization profile"); } } else { Debug.LogError("Failed to obtain a controller visualization profile"); } } return(false); }