首页 > 解决方案 > 如何暂时禁用 MixedRealityToolkit.InputSystem?

问题描述

例如,一旦用户单击按钮,我想在某些转换期间禁用所有输入。

当然,我可以禁用该按钮,但我正在寻找更通用的解决方案,以避免每个按钮再次出现相同的错误。

我尝试了PushInputDisable/ PopInputDisable,这似乎是我正在寻找的东西,但它inputsimulationservice在弹出后会出现问题,并且总体而言,从 inputsystem 引发的大多数输入事件都没有插入到该禁用堆栈中。

我可以制作一个实现所有接口并使用的输入处理程序PushModalInputHandler,但这对于我想要实现的目标来说似乎有点矫枉过正。另外,例如,它可能无法捕捉到语音命令。

任何简单的解决方案?

标签: mrtk

解决方案


您可以使用以下代码来禁用和启用输入系统:

public class DisableInputSystemTest : MonoBehaviour
{
    private IMixedRealityInputSystem inputSystem = null;
    private IMixedRealityInputSystem InputSystem
    {
        get
        {
            if (inputSystem == null)
            {
                MixedRealityServiceRegistry.TryGetService<IMixedRealityInputSystem>(out inputSystem);
            }
            return inputSystem;
        }
    }

    public void DisableInputSystem()
    {
        InputSystem.Disable();
    }

    public void EnableInputSystem()
    {
        InputSystem.Enable();
    }
}

请注意,在最新mrtk_development的分支中存在一个错误(问题5085),在重新启用后,您将获得大量空指针,说“NullReferenceException:对象引用未设置为 Microsoft.MixedReality.Toolkit.Input.FocusProvider.RegisterPointers 对象的实例(Microsoft.MixedReality.Toolkit.Input.IMixedRealityInputSource inputSource) (在 Assets/MixedRealityToolkit.Services/InputSystem/FocusProvider.cs:689)"

要解决此问题,请将以下代码从MixedRealityInputSystem.Initalize()的开头移动MixedRealityInputSystem.Enable()

MixedRealityInputSystemProfile profile = ConfigurationProfile as MixedRealityInputSystemProfile;

if (profile.PointerProfile != null)
{
    if (profile.PointerProfile.GazeProviderType?.Type != null)
    {
        GazeProvider = CameraCache.Main.gameObject.EnsureComponent(profile.PointerProfile.GazeProviderType.Type) as IMixedRealityGazeProvider;
        GazeProvider.GazeCursorPrefab = profile.PointerProfile.GazeCursorPrefab;
        // Current implementation implements both provider types in one concrete class.
        EyeGazeProvider = GazeProvider as IMixedRealityEyeGazeProvider;
    }
    else
    {
        Debug.LogError("The Input system is missing the required GazeProviderType!");
        return;
    }
}
else
{
    Debug.LogError("The Input system is missing the required Pointer Profile!");
    return;
}

推荐阅读