microsoft / OpenXR-MixedReality

OpenXR samples and preview headers for HoloLens and Windows Mixed Reality developers familiar with Visual Studio
https://aka.ms/openxr
MIT License
340 stars 96 forks source link

Disable/Enable OpenXR Device during runtime #97

Closed MiPa12 closed 2 years ago

MiPa12 commented 2 years ago

Describe the bug

Hi, I need to switch between mulitple devices during runtime (Desktop, HP Reverb G2 and Hololens 2). Currently, I'm working on the device switch between Desktop and VR. When I disable XR with StopXR() and try to enable it afterwards with StartXR(), then I'm not able to provide Inputs to the motion controllers anymore. The headset is working fine, I'm able to see the controllers but an input is not possible anymore. If there is any ohter way to do this, I would be happy about any advice.

For this I'm using the following code:

    `public void StartXR()
    {
        Debug.Log("Initializing XR...");

        XRGeneralSettings.Instance.Manager.InitializeLoaderSync();
        XRGeneralSettings.Instance.Manager.StartSubsystems();

    }

    public void StopXR()
    {
        Debug.Log("Stopping XR...");

        XRGeneralSettings.Instance.Manager.StopSubsystems();
        XRGeneralSettings.Instance.Manager.DeinitializeLoader();
        Debug.Log("XR stopped completely.");
    }`

I'm looking for something like InputSystem.RemoveDevice(inputDevice) and InputSystem.AddDevice(inputDevice).

Error message

After calling StartXR() (before that StopXR() was executed once) , then I stopped unity and after running unity again the follwing error occurs and inputs to the controller,which are implemented by the new input system, are not possible anymore:

Could not recreate input device 'HP HP Reverb G2 Controller OpenXR (XRInputV1)' with layout 'XRInputV1::HP::HPReverbG2ControllerOpenXR' and variants 'Default' after domain reload UnityEngine.InputSystem.LowLevel.NativeInputRuntime/<>cDisplayClass7_0:b0 (UnityEngineInternal.Input.NativeInputUpdateType,UnityEngineInternal.Input.NativeInputEventBuffer*) UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate (UnityEngineInternal.Input.NativeInputUpdateType,intptr) UnityEditor.EditorAssemblies:ProcessInitializeOnLoadAttributes (System.Type[])

InvalidOperationException: Cannot find parent 'devicepose' of control 'devicepose/istracked' in layout 'XRInputV1::HP::HPReverbG2ControllerOpenXR' UnityEngine.InputSystem.Layouts.InputDeviceBuilder.InsertChildControl (UnityEngine.InputSystem.Layouts.InputControlLayout layout, UnityEngine.InputSystem.Utilities.InternedString variant, UnityEngine.InputSystem.InputControl parent, System.Boolean& haveChildrenUsingStateFromOtherControls, UnityEngine.InputSystem.Layouts.InputControlLayout+ControlItem& controlItem) (at Library/PackageCache/com.unity.inputsystem@1.0.2/InputSystem/Devices/InputDeviceBuilder.cs:557) UnityEngine.InputSystem.Layouts.InputDeviceBuilder.AddChildControlIfMissing (UnityEngine.InputSystem.Layouts.InputControlLayout layout, UnityEngine.InputSystem.Utilities.InternedString variants, UnityEngine.InputSystem.InputControl parent, System.Boolean& haveChildrenUsingStateFromOtherControls, UnityEngine.InputSystem.Layouts.InputControlLayout+ControlItem& controlItem) (at Library/PackageCache/com.unity.inputsystem@1.0.2/InputSystem/Devices/InputDeviceBuilder.cs:508) UnityEngine.InputSystem.Layouts.InputDeviceBuilder.AddChildControls (UnityEngine.InputSystem.Layouts.InputControlLayout layout, UnityEngine.InputSystem.Utilities.InternedString variants, UnityEngine.InputSystem.InputControl parent, System.Boolean& haveChildrenUsingStateFromOtherControls) (at Library/PackageCache/com.unity.inputsystem@1.0.2/InputSystem/Devices/InputDeviceBuilder.cs:337) UnityEngine.InputSystem.Layouts.InputDeviceBuilder.InstantiateLayout (UnityEngine.InputSystem.Layouts.InputControlLayout layout, UnityEngine.InputSystem.Utilities.InternedString variants, UnityEngine.InputSystem.Utilities.InternedString name, UnityEngine.InputSystem.InputControl parent) (at Library/PackageCache/com.unity.inputsystem@1.0.2/InputSystem/Devices/InputDeviceBuilder.cs:193) UnityEngine.InputSystem.Layouts.InputDeviceBuilder.InstantiateLayout (UnityEngine.InputSystem.Utilities.InternedString layout, UnityEngine.InputSystem.Utilities.InternedString variants, UnityEngine.InputSystem.Utilities.InternedString name, UnityEngine.InputSystem.InputControl parent) (at Library/PackageCache/com.unity.inputsystem@1.0.2/InputSystem/Devices/InputDeviceBuilder.cs:102) UnityEngine.InputSystem.Layouts.InputDeviceBuilder.Setup (UnityEngine.InputSystem.Utilities.InternedString layout, UnityEngine.InputSystem.Utilities.InternedString variants, UnityEngine.InputSystem.Layouts.InputDeviceDescription deviceDescription) (at Library/PackageCache/com.unity.inputsystem@1.0.2/InputSystem/Devices/InputDeviceBuilder.cs:51) UnityEngine.InputSystem.InputDevice.Build[TDevice] (System.String layoutName, System.String layoutVariants, UnityEngine.InputSystem.Layouts.InputDeviceDescription deviceDescription) (at Library/PackageCache/com.unity.inputsystem@1.0.2/InputSystem/Devices/InputDevice.cs:678) UnityEngine.InputSystem.InputManager.AddDevice (UnityEngine.InputSystem.Utilities.InternedString layout, System.Int32 deviceId, System.String deviceName, UnityEngine.InputSystem.Layouts.InputDeviceDescription deviceDescription, UnityEngine.InputSystem.InputDevice+DeviceFlags deviceFlags, UnityEngine.InputSystem.Utilities.InternedString variants) (at Library/PackageCache/com.unity.inputsystem@1.0.2/InputSystem/InputManager.cs:1033) UnityEngine.InputSystem.InputManager.RestoreDeviceFromSavedState (UnityEngine.InputSystem.InputManager+DeviceState& deviceState, UnityEngine.InputSystem.Utilities.InternedString layout) (at Library/PackageCache/com.unity.inputsystem@1.0.2/InputSystem/InputManager.cs:3420) UnityEngine.InputSystem.LowLevel.<>cDisplayClass7_0:b0(NativeInputUpdateType, NativeInputEventBuffer*) UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr) UnityEngineInternal.Input.NativeInputSystem:Update(NativeInputUpdateType) UnityEditor.EditorAssemblies:ProcessInitializeOnLoadAttributes(Type[])

Furthermore, when I enable/disable XR multiple times I got multiple Instances of those devices as depicted in the image below. When I stop Unity and run Unity again those device duplicates stay. I need to restart unity or remove those device duplicates manually to get it work again: switch_device

Expected Behavior

run Unity: run_unity

stop Unity: stop_unity

To reproduce

Steps to reproduce the behavior:

  1. Setup a Scene with Motion Controllers and HP Reverb enabled
  2. Set up a Second camera
  3. Script - Switch between the VR camera and the Second camera and using the above mentioned code snippet to enable/disable the VR Device
  4. Stop Unity and run Unity again

Your setup (please complete the following information)

Target platform (please complete the following information)

Thanks.

yl-msft commented 2 years ago

@MiPa12 , thanks for the feedback. The issue you are reporting is Unity specific challenge, and probably better to report on this GitHub repo, which is also monitored by our team: https://github.com/microsoft/OpenXR-Unity-MixedReality-Samples

From the error message, this is likely an issue in Unity's Input system. I admit we've not tested with the scenario as you said. So it will be better reported to Unity team through Unity's bug or Unity's Forum.

This is the error message that seems to matter:

Could not recreate input device 'HP HP Reverb G2 Controller OpenXR (XRInputV1)' with layout 'XRInputV1::HP::HPReverbG2ControllerOpenXR' and variants 'Default' after domain reload UnityEngine.InputSystem.LowLevel.NativeInputRuntime/<>cDisplayClass7_0:b0 (UnityEngineInternal.Input.NativeInputUpdateType,UnityEngineInternal.Input.NativeInputEventBuffer*) UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate (UnityEngineInternal.Input.NativeInputUpdateType,intptr) UnityEditor.EditorAssemblies:ProcessInitializeOnLoadAttributes (System.Type[])

I wonder if you have a Unity project that can reproduce this issue to share? It might help us or Unity team to investigate. We will also reach out to Unity team to see if they have insights.

yl-msft commented 2 years ago

@MiPa12 ,
Could you double check what is the "Input System" package you are using? There are some related bugs fixed in latest version, so worth to try our project again with upgrading input system package to 1.2.0 image

yl-msft commented 2 years ago

@MiPa12 I hope the latest Unity input system package solved the problem for you. If not please reactive this issue and let us know what we can help further.