Hololens单击、双击

本文介绍了在Hololens上实现单击和双击手势交互的步骤,包括下载导入资源、设置场景元素、编写脚本等。通过创建WorldCursor和GazeGestureManager脚本处理交互,当立方体被单击变为蓝色,双击则变为红色。最后,文章提供了完整工程的下载链接供测试。

AI 时代程序员必备技能

Codex、Claude Code、Cursor、Hermes Agent、OpenClaw等工程化实战专栏 ,讲透 AI 如何接管脏活累活

1.下载导入资源:https://pan.baidu.com/s/19JMpg6-_zVFXD6K1UKh_bQ 
提取码:7d76

2.拖动Hologram下的Cursor到Hierarchy;

3.新建空物体,并命名为OrigamiCollection;

4.修改相机属性;

5.新建脚本WorldCursor,并添加到Cursor上;

using UnityEngine;

public class WorldCursor : MonoBehaviour
{
    private MeshRenderer meshRenderer;

    // Use this for initialization
    void Start()
    {
        // Grab the mesh renderer that's on the same object as this script.
        meshRenderer = this.gameObject.GetComponentInChildren<MeshRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        // Do a raycast into the world based on the user's
        // head position and orientation.
        var headPosition = Camera.main.transform.position;
        var gazeDirection = Camera.main.transform.forward;

        RaycastHit hitInfo;
        if (Physics.Raycast(headPosition, gazeDirection, out hitInfo))
        {
            // If the raycast hit a hologram...

            // Display the cursor mesh.
            meshRenderer.enabled = true;
            // Move the cursor to the point where the raycast hit.
            this.transform.position = hitInfo.point;
            // Rotate the cursor to hug the surface of the hologram.
            this.transform.rotation =
                Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
        }
        else
        {
            // If the raycast did not hit a hologram, hide the cursor mesh.
            meshRenderer.enabled = false;
        }
    }
}

6.新建脚本GazeGestureManager,并添加到OrigamiCollection上;

using UnityEngine;
using UnityEngine.XR.WSA.Input;

public class GazeGestureManager : MonoBehaviour
{
    public static GazeGestureManager Instance { get; private set; }

    // Represents the hologram that is currently being gazed at.
    public GameObject FocusedObject { get; private set; }

    GestureRecognizer recognizer;

    // Use this for initialization
    void Start()
    {
        Instance = this;
        // Set up a GestureRecognizer to detect Select gestures.
        //创建GestureRecognizer实例
        recognizer = new GestureRecognizer();
        //注册指定的手势类型,本例指定单击及双击手势
        recognizer.SetRecognizableGestures(GestureSettings.Tap | GestureSettings.DoubleTap);
        //订阅手势事件
        recognizer.TappedEvent += GestureRecognizer_TappedEvent;
        //开始手势识别
        recognizer.StartCapturingGestures();
    }

    private void OnTap()
    {
        if (FocusedObject != null)
        {
            FocusedObject.SendMessage("OnTap");
        }
    }
    private void OnDoubleTap()
    {
        if (FocusedObject != null)
        {
            FocusedObject.SendMessage("OnDoubleTap");
        }
    }

    private void GestureRecognizer_TappedEvent(InteractionSourceKind source, int tapCount, Ray headRay)
    {
        if (tapCount == 1)
        {
            OnTap();
        }
        else
        {
            OnDoubleTap();
        }
    }
        void LateUpdate()
        {
        // Figure out which hologram is focused this frame.
        GameObject oldFocusObject = FocusedObject;

        // Do a raycast into the world based on the user's
        // head position and orientation.
        var headPosition = Camera.main.transform.position;
        var gazeDirection = Camera.main.transform.forward;

        RaycastHit hitInfo;
        if (Physics.Raycast(headPosition, gazeDirection, out hitInfo))
        {
            // If the raycast hit a hologram, use that as the focused object.
            FocusedObject = hitInfo.collider.gameObject;
        }
        else
        {
            // If the raycast did not hit a hologram, clear the focused object.
            FocusedObject = null;
        }

        // If the focused object changed this frame,
        // start detecting fresh gestures again.
        if (FocusedObject != oldFocusObject)
        {
            recognizer.CancelGestures();
            recognizer.StartCapturingGestures();
        }
    }
    void OnDestroy()
        {
        recognizer.StopCapturingGestures();
        recognizer.TappedEvent -= GestureRecognizer_TappedEvent;
        }
    }

7.新建脚本CubeScript,并添加到Cube上,实现单击为蓝色,双击为红色;

using UnityEngine;
using System.Collections;

public class CubeScript : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }

    private void OnTap()
    {
        gameObject.GetComponent<MeshRenderer>().material.color = Color.blue;
    }

    private void OnDoubleTap()
    {
        gameObject.GetComponent<MeshRenderer>().material.color = Color.green;
    }
}

8.发布Hololens进行测试。

9.完整工程下载:https://download.csdn.net/download/exclaiming/10692685

AI 时代程序员必备技能

Codex、Claude Code、Cursor、Hermes Agent、OpenClaw等工程化实战专栏 ,讲透 AI 如何接管脏活累活

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值