答案更新于 2019 年 6 月 11 日
您可以通过以下代码找到与 Input{Down,Up,Updated} 事件关联的指针:
public void OnInputDown(InputEventData eventData)
{
foreach(var ptr in eventData.InputSource.Pointers)
{
// An input source has several pointers associated with it, if you handle OnInputDown all you get is the input source
// If you want the pointer as a field of eventData, implement IMixedRealityPointerHandler
if (ptr.Result != null && ptr.Result.CurrentPointerTarget.transform.IsChildOf(transform))
{
Debug.Log($"InputDown and Pointer {ptr.PointerName} is focusing this object or a descendant");
}
Debug.Log($"InputDown fired, pointer {ptr.PointerName} is attached to input source that fired InputDown");
}
}
请注意,如果您关心指针,则可能使用了错误的事件处理程序。考虑改为实施IMixedRealityPointerHandler
而不是IMixedRealityInputHandler
.
原始答案
如果您想获得与运动控制器相对应的控制器旋转,我认为最可靠的方法是首先为 MixedRealityPoses 实现一个新的输入处理程序。这将允许您在底层控制器更改时获取更新。
class MyClass : IMixedRealityInputHandler<Vector2>, IMixedRealityInputHandler<MixedRealityPose>
然后,跟踪源 ID 以进行姿势映射...
Dictionary<uint, MixedRealityPose> controllerPoses = new Dictionary<uint, MixedRealityPose>;
public void OnInputChanged(InputEventData<MixedRealityPose> eventData)
{
controllerPoses[eventData.SourceId] = eventData.InputData
}
最后,在 Handler 中获取这些数据:
MixedRealityInputAction myInputAction;
public void OnInputChanged(InputEventData<Vector2> eventData)
{
// NOTE: would recommend exposing a field myInputAction in Unity editor
// and assigning there to avoid string comparison which could break more easily.
if (eventData.MixedRealityInputAction == myInputAction)
{
MixedRealityPose controllerPose = controllerPoses[eventData.SourceId];
// Do something with the pointer
}
}
您想使用这种方法而不是使用指针的原因是因为您可以将多个指针分配给单个输入源。由于输入事件是从输入源引发的,因此您要使用哪个指针并不明显,并且这些指针可能会随着时间而改变。一开始,抓取混合现实姿势并将它们关联起来似乎有点复杂,但从长远来看,这对你来说会更可靠。