Hololens2代码控制手部网格、手部关节、手部射线、性能面板的显示状态

  • 手部网格和关节状态控制
using Microsoft.MixedReality.Toolkit;
using Microsoft.MixedReality.Toolkit.Input;
using UnityEngine;

namespace MRTK.Tools
{
    [AddComponentMenu("AddScript/MRTKTools/SetHandVisualisation")]
    public class SetHandVisualisation : MonoBehaviour
    {
        /// 
        /// 切换手部网格状态
        /// 
        public void ToggleHandMesh()
        {
            MixedRealityInputSystemProfile inputSystemProfile = CoreServices.InputSystem?.InputSystemProfile;
            if (inputSystemProfile == null)
                return;

            MixedRealityHandTrackingProfile handTrackingProfile = inputSystemProfile.HandTrackingProfile;

            if (handTrackingProfile != null)
                handTrackingProfile.EnableHandMeshVisualization = !handTrackingProfile.EnableHandMeshVisualization;
        }

        /// 
        /// 切换手部关节状态
        /// 
        public void ToggleHandJoint()
        {
            MixedRealityHandTrackingProfile handTrackingProfile = null;

            if (CoreServices.InputSystem?.InputSystemProfile != null)
                handTrackingProfile = CoreServices.InputSystem.InputSystemProfile.HandTrackingProfile;

            if (handTrackingProfile != null)
                handTrackingProfile.EnableHandJointVisualization = !handTrackingProfile.EnableHandJointVisualization;
        }

        /// 
        /// 显示/隐藏手部Mesh
        /// 
        /// 是否显示
        public void ShowHandMesh(bool isShow)
        {
            MixedRealityInputSystemProfile inputSystemProfile = CoreServices.InputSystem?.InputSystemProfile;
            if (inputSystemProfile == null)
            {
                return;
            }

            MixedRealityHandTrackingProfile handTrackingProfile = inputSystemProfile.HandTrackingProfile;
            if (handTrackingProfile != null)
            {
                handTrackingProfile.EnableHandMeshVisualization = isShow;
            }
        }

        /// 
        /// 显示/隐藏手部关节
        /// 
        /// 是否显示
        public void ShowHandJoint(bool isShow)
        {
            MixedRealityHandTrackingProfile handTrackingProfile = null;
            if (CoreServices.InputSystem?.InputSystemProfile != null)
            {
                handTrackingProfile = CoreServices.InputSystem.InputSystemProfile.HandTrackingProfile;
                if (handTrackingProfile != null)
                {
                    handTrackingProfile.EnableHandJointVisualization = isShow;
                }
            }
        }
    }
}
  • 手部射线状态控制
using Microsoft.MixedReality.Toolkit.Input;
using Microsoft.MixedReality.Toolkit.Utilities;
using UnityEngine;

namespace MRTK.Tools
{
    [AddComponentMenu("AddScript/MRTKTools/SetPointerBehavior")]
    public class SetPointerBehavior : MonoBehaviour
    {
        private void TogglePointerEnabled<T>(InputSourceType inputType) where T : class, IMixedRealityPointer
        {
            PointerBehavior oldBehavior = PointerUtils.GetPointerBehavior<T>(Handedness.Any, inputType);
            PointerBehavior newBehavior;
            if (oldBehavior == PointerBehavior.AlwaysOff)
            {
                newBehavior = PointerBehavior.Default;
            }
            else
            {
                newBehavior = PointerBehavior.AlwaysOff;
            }

            PointerUtils.SetPointerBehavior<T>(newBehavior, inputType);
        }

        /// 
        /// 开启/关闭手部射线
        /// 
        public void ToggleHandRayEnabled()
        {
            TogglePointerEnabled<ShellHandRayPointer>(InputSourceType.Hand);
        }

        /// 
        /// 显示/关闭手部射线
        /// 
        /// 是否开启
        public void SetHandRayEnable(bool isEnable)
        {
            PointerBehavior point;

            point = isEnable ? PointerBehavior.Default : PointerBehavior.AlwaysOff;

            PointerUtils.SetPointerBehavior<ShellHandRayPointer>(point, InputSourceType.Hand);
        }
    }
}
  • 性能面板显示状态控制
using Microsoft.MixedReality.Toolkit;
using UnityEngine;

namespace MRTK.Tools
{
    [AddComponentMenu("AddScript/MRTKTools/SetProfilerVisual")]
    public class SetProfilerVisual : MonoBehaviour
    {
        /// 
        /// 切换性能面板显示状态
        /// 
        public void ToggleProfilerVisibility()
        {
            if (CoreServices.DiagnosticsSystem != null)
            {
                CoreServices.DiagnosticsSystem.ShowProfiler = !CoreServices.DiagnosticsSystem.ShowProfiler;
            }
        }

        /// 
        /// 显示/关闭性能面板
        /// 
        /// 是否显示
        public void SetProfilerVisibility(bool isVisible)
        {
            if (CoreServices.DiagnosticsSystem != null)
            {
                CoreServices.DiagnosticsSystem.ShowProfiler = isVisible;
            }
        }
    }
}

你可能感兴趣的:(Unity,hololens2,unity,hololens2,mrtk)