You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在HoloLens 2中隐藏手部射线的默认光标?

问题1:隐藏/修改手部射线的默认光标

方法一:直接隐藏默认光标

在你现有代码的基础上,找到对应的ShellHandRayPointer实例后,通过修改光标可见性或直接禁用光标对象来隐藏:

GameObject manipulatePoint;

foreach (var source in CoreServices.InputSystem.DetectedInputSources)
{
    if (source.SourceType is InputSourceType.Hand)
    {
        foreach (var p in source.Pointers)
        {
            if (p is ShellHandRayPointer handRayPointer && p.Result != null)
            {
                if (p.PointerName == "Right_ShellHandRayPointer(Clone)")
                {
                    // 方式1:通过API设置隐藏
                    handRayPointer.CursorVisibility = CursorVisibility.Hidden;
                    
                    // 方式2:直接禁用光标对象(如果方式1不生效)
                    if (handRayPointer.Cursor != null)
                    {
                        handRayPointer.Cursor.SetActive(false);
                    }
                    
                    // 你的原有逻辑代码
                    var endPoint = p.Result.Details.Point;
                    var hitObject = p.Result.Details.Object;
                    if (hitObject != null)
                    {
                        var rayPosition = p.Result.Details.Point;
                        manipulatePoint.transform.position = rayPosition;
                    }
                }
            }
        }
    }
}

方法二:替换为自定义光标

准备好自定义光标Prefab后,直接替换指针的光标预制体:

public GameObject customCursorPrefab; // 在Inspector中赋值自定义光标
GameObject manipulatePoint;

foreach (var source in CoreServices.InputSystem.DetectedInputSources)
{
    if (source.SourceType is InputSourceType.Hand)
    {
        foreach (var p in source.Pointers)
        {
            if (p is ShellHandRayPointer handRayPointer && p.Result != null)
            {
                if (p.PointerName == "Right_ShellHandRayPointer(Clone)")
                {
                    // 替换自定义光标
                    handRayPointer.CursorPrefab = customCursorPrefab;
                    handRayPointer.CursorVisibility = CursorVisibility.Visible;
                    
                    // 你的原有逻辑代码
                    // ...
                }
            }
        }
    }
}
问题2:自定义手部射线实现MRTK式前向向量效果

MRTK手部射线的核心是基于手部自然朝向(手腕指向食指指尖的方向)生成前向向量,你可以通过获取手部关节数据来实现:

using Microsoft.MixedReality.Toolkit.Input;
using UnityEngine;

public class CustomHandRay : MonoBehaviour
{
    public GameObject manipulatePoint;
    public float defaultRayLength = 2f; // 无碰撞时射线的默认长度

    void Update()
    {
        foreach (var source in CoreServices.InputSystem.DetectedInputSources)
        {
            if (source.SourceType == InputSourceType.Hand)
            {
                // 只处理右手
                if (source is IMixedRealityHand hand && hand.Handedness == Handedness.Right)
                {
                    // 获取手腕和食指指尖的关节位置
                    if (hand.TryGetJoint(TrackedHandJoint.Wrist, out MixedRealityPose wristPose) &&
                        hand.TryGetJoint(TrackedHandJoint.IndexTip, out MixedRealityPose indexTipPose))
                    {
                        // 计算射线前向向量(从手腕指向食指)
                        Vector3 rayDirection = (indexTipPose.Position - wristPose.Position).normalized;
                        Vector3 rayOrigin = wristPose.Position; // 射线起点设为手腕

                        // 发射射线检测碰撞
                        if (Physics.Raycast(rayOrigin, rayDirection, out RaycastHit hit))
                        {
                            manipulatePoint.transform.position = hit.point;
                            Debug.DrawLine(rayOrigin, hit.point, Color.green); // 调试用射线
                        }
                        else
                        {
                            // 无碰撞时射线延伸到默认长度
                            Vector3 endPoint = rayOrigin + rayDirection * defaultRayLength;
                            Debug.DrawLine(rayOrigin, endPoint, Color.red);
                        }
                    }
                }
            }
        }
    }
}

补充说明

  • 若要更贴合MRTK的视觉效果,可以给射线添加LineRenderer组件,将起点和终点赋值给LineRenderer的顶点
  • 可以调整射线起点为手掌中心(TrackedHandJoint.Palm),让射线更符合直觉

内容的提问来源于stack exchange,提问作者翁晨毓

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 10:12:32