0

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

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

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

        eventData.Use();
    }
}

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

4

1 回答 1

2

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

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

于 2019-06-06T05:10:28.907 回答