我正在和github上的其他人一起使用YuGiOh HoloLens应用程序,而且我们一直在使用它们。我已经完成了所有功能,并使用Unity所做的OnMouseDown()函数对其进行了测试。单击对象时,将调用此函数一次。中间的代码并不重要,但我想表明那里不应该有任何时髦的事情。
void OnMouseDown()
{
Debug.Log(myGameManager);
Debug.Log(myZone);
myGameManager.setSelectedCard(this, myZone);
}
现在我想要airtap而不是click,所以我们用这段代码做了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?)或者其他什么东西?
答案 0 :(得分:1)
如果来自HoloLens Toolkit for Unity的点点滴滴,你应该避免使用Hololens Academy课程中的代码。还有一些有用的代码可以从Hololens Academy借用,但是Toolkit中的东西已经过时了,并且不如当前版本的工具包。
我的建议是按照Getting Started指南安装holotoolkit。完成项目中的所有部分(如上面的GazeGestureManager)之后,将它们替换为Holotoolkit版本(本例中为GestureManager)。
我敢打赌,在你切换到Holotoolkit之后你的问题就消失了。如果不是,那么排除故障会更容易......
答案 1 :(得分:0)
我会说你为同一个gameObject多次订阅了TappedEvent处理程序,因此当你触发事件时,它会被多次解析为onSelect。这只是我的头脑。