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

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

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

            MixedRealityHandTrackingProfile handTrackingProfile = inputSystemProfile.HandTrackingProfile;

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

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

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

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

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

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

        /// <summary>
        /// 显示/隐藏手部关节
        /// </summary>
        /// <param name="isShow">是否显示</param>
        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);
        }

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

        /// <summary>
        /// 显示/关闭手部射线
        /// </summary>
        /// <param name="isEnable">是否开启</param>
        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
    {
        /// <summary>
        /// 切换性能面板显示状态
        /// </summary>
        public void ToggleProfilerVisibility()
        {
            if (CoreServices.DiagnosticsSystem != null)
            {
                CoreServices.DiagnosticsSystem.ShowProfiler = !CoreServices.DiagnosticsSystem.ShowProfiler;
            }
        }

        /// <summary>
        /// 显示/关闭性能面板
        /// </summary>
        /// <param name="isVisible">是否显示</param>
        public void SetProfilerVisibility(bool isVisible)
        {
            if (CoreServices.DiagnosticsSystem != null)
            {
                CoreServices.DiagnosticsSystem.ShowProfiler = isVisible;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值