【问题标题】:How to run the airtap OnSelect Method exactly Once on Tap?如何在点击时准确地运行 airtap OnSelect 方法?
【发布时间】:2016-08-18 22:44:00
【问题描述】:

我正在与 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?)或类似的东西?

【问题讨论】:

    标签: c# unity3d hololens


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2011-05-31
        • 2023-04-11
        • 2010-11-03
        • 1970-01-01
        • 2013-10-20
        • 1970-01-01
        • 1970-01-01
        • 2018-09-28
        • 2021-08-02
        相关资源
        最近更新 更多