如何从 InputEventData 获取关联指针?

How to get associated pointer from an InputEventData?

我已经设置了一个双轴输入动作和相应的处理程序。

现在我需要获取引发此输入操作的相应 pointer/controller 以确定它指向的位置,例如如果事件是从运动控制器的拇指杆引发的,我想获取与该运动控制器关联的指针。

public void OnInputChanged(InputEventData<Vector2> eventData)
{
    if (eventData.MixedRealityInputAction.Description == "MyInputAction")
    {
        // How to do something like this?
        // var pointer = eventData.Pointer;

        eventData.Use();
    }
}

如果没有指针与操作相关联(例如,如果它来自语音命令),那么我可能会以不同的方式处理它,而是使用 Gaze。

答案更新于 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
}

最后,在您的处理程序中获取此数据:

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
    }
}

您想使用这种方法而不是使用指针的原因是您可以将多个指针分配给一个输入源。由于输入事件是从输入源引发的,因此您想要使用哪个指针并不明显,而且这些指针可能会随着时间而改变。抓住混合现实姿势并将它们关联起来一开始看起来确实有点复杂,但从长远来看对你来说会更可靠。