如何从InputEventData获取关联的指针?

时间:2019-06-03 21:07:08

标签: mrtk

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

现在我需要获取引发此输入操作的相应指针/控制器,以找出其指向的位置,例如如果该事件是从运动控制器的指尖引发的,则我想获取与该运动控制器关联的指针。

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

        eventData.Use();
    }
}

如果没有与该动作相关的指针(例如,如果来自语音命令),那么我可以改用Gaze来不同地处理它。

1 个答案:

答案 0 :(得分:2)

答案于6/11/2019更新

您可以通过以下代码找到与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>

然后,跟踪Source 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
    }
}

之所以要使用此方法而不是使用指针,是因为可以将多个指针分配给单个输入源。由于Input事件是从输入源引发的,因此要使用哪个指针并不明显,并且这些事件可能会随着时间而改变。乍一看,抓住混合现实姿势并将它们相互关联似乎确实有些费解,但从长远来看,对您来说将更加可靠。