0

我正在与 github 上的其他一些人一起开发 YuGiOh HoloLens 应用程序,但我们被困在了 airtap 上。我已经完成了所有功能并使用 Unity 的 OnMouseDown() 函数对其进行了测试。单击对象时会调用该函数一次。两者之间的代码并不重要,但我想表明不应该有任何时髦的事情发生。

void OnMouseDown()
{
    Debug.Log(myGameManager);
    Debug.Log(myZone);
    myGameManager.setSelectedCard(this, myZone);
}

现在我想点击而不是点击,所以我们使用以下代码执行了 OnSelectMethod:

void OnSelect()
{
    Debug.Log(myGameManager);
    Debug.Log(myZone);
    myGameManager.setSelectedCard(this, myZone);
}

并且有一个 GazeGestureManager 附加到注册事件的对象上。我们从 Hololens Academy 提取了这段代码。

using UnityEngine;
using UnityEngine.VR.WSA.Input;

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

private Vector3 moveDirection = Vector3.zero;

// 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.
    recognizer = new GestureRecognizer();
    recognizer.TappedEvent += (source, tapCount, ray) =>
    {
        // Send an OnSelect message to the focused object and its ancestors.
        if (FocusedObject != null)
        {
            FocusedObject.SendMessageUpwards("OnSelect");
        }
    };
    recognizer.StartCapturingGestures();
}

// Update is called once per frame
void Update()
{
    // 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();
    }
}
}

现在我们已经多次使用此代码,并且我们设置它的任何方式 OnSelect() 方法都会被调用 4-36 次。为什么不只调用一次?

难道airtab是一个连续的事件?

在水龙头进行时,哪个会不断被轮询?如果是这样,是否有更适合使用的事件?(OnAirTapEnd?)或类似的东西?

4

2 回答 2

1

你应该避免使用 Hololens Academy 课程中的代码,因为它是来自HoloLens Toolkit for Unity的零碎代码。仍然有一些有用的代码可以从 Hololens Academy 借用,但是 Toolkit 中的东西已经过时了,而且不如当前版本的工具包。

我的建议是按照入门指南安装 holotoolkit 。完成后,从项目中删除所有部分(如上面的 GazeGestureManager)并用 Holotoolkit 版本(在本例中为 GestureManager)替换它们。

我敢打赌,在您切换到 Holotoolkit 后,您的问题就会消失。如果没有,故障排除会容易得多......

于 2016-08-19T01:12:07.987 回答
0

我会说您为同一个游戏对象多次订阅了 TappedEvent 处理程序,因此当您触发事件时,它会多次解析为 onSelect。这只是我的想法。

于 2016-09-07T14:16:30.793 回答