unity-xr-plugin icon indicating copy to clipboard operation
unity-xr-plugin copied to clipboard

OpenVR Loader initializes regardless of Unity XR "Initialize XR on Startup" option.

Open nitz opened this issue 3 years ago • 12 comments

Hello!

I've been in the process of moving from a much older version of OVR/SteamVR to the new Unity XR Plug-in system. Most things have been pretty straight forward, but with this project I've hit a blocking issue: this application needs to be running two instances.

Currently, regardless of build settings, opening a second instance causes the first to immediately quit, as it has initialized the VR hardware. (I'm using a Vive Pro Eye at the moment, not sure if that matters.)

Previously we were able to work around this issue by building the project twice: once with "VR Enabled" checked and once with "VR Enabled" unchecked. That would allow us to start one of the instance where it was enabled, and then any other number of instances with the disabled one. I've tried scripting the adjustment of XRGeneralSettings.Instance.InitManagerOnStart to both true and false for building two instances of the player too, but it doesn't seem to make a difference there either.

I'm attempting to replicate that now, but it seems the OpenVR Loader plugin completely ignores the "Initialize XR on Startup" option. Regardless of that setting, OpenVR seems to initialize and open the hardware, because opening another instance always causes the first to close.

Ideally I'd like to be able to have just a single build, and then either launch to a "chooser" scene that would allow me to go the VR route or the non-VR route, but it seems no solution is working at the moment, or I'm misunderstanding the systems.

Is there something I'm missing or doing wrong, or is this a defect?

Below are some screenshots of the settings I'm trying.

image

image

Here is the tiny build script I use to build two copies of the player build script gist.

nitz avatar Feb 25 '21 19:02 nitz

Maybe you're running SteamVR related scripts that are triggering Unity XR? Try setting an empty scene as your startup scene and see if you encounter the same issue.

zite avatar Feb 26 '21 00:02 zite

I created an empty scene and changed my build script to reference only that scene. Opening a new instance still causes the first instance to close. I'm trying to get a build going with minimal managed/native plugins to see if it's something in one of those causing it to kick off, but the logs don't make it look like anything, and the close seems "graceful".

nitz avatar Feb 26 '21 01:02 nitz

I added a script to print out my XRSettings in an [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)], and was taking a look at the message above my message. Looks like the XR devices are getting scanned and matched before it unity ever even creates the GfxDevice, while it's setting up the subsystem scaffolding. Interestingly enough, it does seem like my InitManagerOnStart is false like I had hoped, just doesn't seem to be doing what I'd want 😅

Mono path[0] = 'C:/Users/cmd/Desktop/output/PROJECT_viewer/viewer_Data/Managed'
Mono config path = 'C:/Users/cmd/Desktop/output/PROJECT_viewer/MonoBleedingEdge/etc'
Initialize engine version: 2020.2.6f1 (8a2143876886)
[Subsystems] Discovering subsystems at path C:/Users/cmd/Desktop/output/PROJECT_viewer/viewer_Data/UnitySubsystems
[Subsystems] No descriptors matched for  examples in UnitySubsystems/UnityMockHMD/UnitySubsystemsManifest.json.
[Subsystems] 1 'inputs' descriptors matched in UnitySubsystems/UnityMockHMD/UnitySubsystemsManifest.json
[Subsystems] 1 'displays' descriptors matched in UnitySubsystems/UnityMockHMD/UnitySubsystemsManifest.json
[Subsystems] No descriptors matched for  meshings in UnitySubsystems/UnityMockHMD/UnitySubsystemsManifest.json.
[Subsystems] No descriptors matched for  examples in UnitySubsystems/XRSDKOpenVR/UnitySubsystemsManifest.json.
[Subsystems] 1 'inputs' descriptors matched in UnitySubsystems/XRSDKOpenVR/UnitySubsystemsManifest.json
[Subsystems] 1 'displays' descriptors matched in UnitySubsystems/XRSDKOpenVR/UnitySubsystemsManifest.json
[Subsystems] No descriptors matched for  meshings in UnitySubsystems/XRSDKOpenVR/UnitySubsystemsManifest.json.
GfxDevice: creating device client; threaded=1
Direct3D:
    Version:  Direct3D 11.0 [level 11.1]
    Renderer: NVIDIA GeForce RTX 2080 Ti (ID=0x1e04)
    Vendor:   
    VRAM:     11049 MB
    Driver:   27.21.14.6140
Begin MonoManager ReloadAssembly
- Completed reload, in  0.069 seconds
XRGeneral Settings awakening...

D3D11 device created for Microsoft Media Foundation video decoding.
[I][20:12:38.775339][] Log started.

[I][20:12:38.775839][] Subsystems registered.

[I][20:12:38.776340][] Running after assemblies loaded...

[I][20:12:38.870840][] XR Settings: {
  "Manager": {
    "automaticLoading": false,
    "automaticRunning": false,
    "loaders": [
      {
        "displaySubsystem": null,
        "inputSubsystem": null,
        "name": "Open VR Loader",
        "hideFlags": 0
      }
    ],
    "activeLoaders": [
      {
        "displaySubsystem": null,
        "inputSubsystem": null,
        "name": "Open VR Loader",
        "hideFlags": 0
      }
    ],
    "isInitializationComplete": false,
    "activeLoader": null,
    "name": "Standalone Providers",
    "hideFlags": 0
  },
  "AssignedSettings": {
    "automaticLoading": false,
    "automaticRunning": false,
    "loaders": [
      {
        "displaySubsystem": null,
        "inputSubsystem": null,
        "name": "Open VR Loader",
        "hideFlags": 0
      }
    ],
    "activeLoaders": [
      {
        "displaySubsystem": null,
        "inputSubsystem": null,
        "name": "Open VR Loader",
        "hideFlags": 0
      }
    ],
    "isInitializationComplete": false,
    "activeLoader": null,
    "name": "Standalone Providers",
    "hideFlags": 0
  },
  "InitManagerOnStart": false,
  "name": "Standalone Settings",
  "hideFlags": 0
}

nitz avatar Feb 26 '21 01:02 nitz

Hmm, and just to check your Player Settings -> Resolution -> Force Single Instance setting is not checked, right?

zite avatar Feb 26 '21 18:02 zite

Affirmative, along with allowing run in background.

image

nitz avatar Feb 26 '21 18:02 nitz

I've set up the scaffolding of an empty project to get a minimum reproducible example. Ran into a small issue with that, but that's okay!

I imported the SteamVR Package which brought along the OpenVR plugin, and configured the project like mine:

Player Settings

  • [x] Run in background
  • [ ] Force Single Instance

XR Plug-in Management

  • [ ] Initialize XR on Startup
    • Plugin Providers
    • [x] OpenVR Loader

SteamVR_Resources/Resources/SteamVR_Settings.asset

  • [ ] Auto Enable VR

There is also a small script, BuildHooks.cs that has the RuntimeInitializeOnLoadMethod hooks in it to log when that code runs. It also prints the XRGeneralSettings.Instance & XRGeneralSettings.Instance.Manager as JSON.

I've included a build of the project, as well as the project itself. You can see the issue by running one instance of the build, wait for it to open, then starting a second instance. The first instance will close.

Github wasn't a fan of a 100MB zip file, so I've instead uploaded it here.

What else should I take a look at?

Thanks for the help so far!

nitz avatar Feb 26 '21 19:02 nitz

Good morning!

Alright, I'm back with what seems to be a workaround for my case here at the moment! I'm writing this while my project is building with the full scenes, but this worked for me using the empty scene.

In my build script, for my "viewer" side of my application, I'm attempting to set the loaders to an empty list, like so:

public static bool BuildViewer()
{
	var bpo = GetDefaultBuildOptions();
	bpo.scenes = new[] { "Assets/viewer.unity" };
	bpo.locationPathName = ModifyLocationPathName(bpo.locationPathName, "PROJECT_viewer", "viewer");

	// hold on to current XR settings
	bool xrEnabled = XRSettings.enabled;
	bool xrInitManagerOnStart = XRGeneralSettings.Instance.InitManagerOnStart;
	var managerLoaders = new List<XRLoader>(XRGeneralSettings.Instance.Manager.activeLoaders);
	var assignedSettingsLoaders = new List<XRLoader>(XRGeneralSettings.Instance.AssignedSettings.activeLoaders);

	// set viewer specific XR settings
	XRSettings.enabled = false;
	XRGeneralSettings.Instance.InitManagerOnStart = false;
	XRGeneralSettings.Instance.Manager.TrySetLoaders(new List<XRLoader>());
	XRGeneralSettings.Instance.AssignedSettings.TrySetLoaders(new List<XRLoader>());

	// do the build
	bool buildResult = Build(bpo);
	
	// return the loaders to their previous configuration
	XRSettings.enabled = xrEnabled;
	XRGeneralSettings.Instance.InitManagerOnStart = xrInitManagerOnStart;
	XRGeneralSettings.Instance.Manager.TrySetLoaders(managerLoaders);
	XRGeneralSettings.Instance.AssignedSettings.TrySetLoaders(assignedSettingsLoaders);
	
	return buildResult;
}

I'm doing it to both .Manager and .AssignedSettings, but reflecting makes it look like they're the same internal variable?

// class XRGeneralSettings

public XRManagerSettings Manager
{
	get => this.m_LoaderManagerInstance;
	set => this.m_LoaderManagerInstance = value;
}

public XRManagerSettings AssignedSettings
{
	get => this.m_LoaderManagerInstance;
	set => this.m_LoaderManagerInstance = value;
}

But I figure best to just cover my bases.

Anyways, now when I run my 'viewer', which is what I don't want to have any XR enabled, the first lines of the output log now look like this:

Mono path[0] = 'C:/Users/cmd/Desktop/output/PROJECT_viewer/viewer_Data/Managed'
Mono config path = 'C:/Users/cmd/Desktop/output/PROJECT_viewer/MonoBleedingEdge/etc'
Initialize engine version: 2020.2.6f1 (8a2143876886)
Plugins: Failed to load 'XRSDKOpenVR' because one or more of its dependencies could not be loaded.

[Subsystems] Discovering subsystems at path C:/Users/cmd/Desktop/output/PROJECT_viewer/viewer_Data/UnitySubsystems
[Subsystems] No descriptors matched for  examples in UnitySubsystems/UnityMockHMD/UnitySubsystemsManifest.json.
[Subsystems] 1 'inputs' descriptors matched in UnitySubsystems/UnityMockHMD/UnitySubsystemsManifest.json
[Subsystems] 1 'displays' descriptors matched in UnitySubsystems/UnityMockHMD/UnitySubsystemsManifest.json
[Subsystems] No descriptors matched for  meshings in UnitySubsystems/UnityMockHMD/UnitySubsystemsManifest.json.
[Subsystems] No descriptors matched for  examples in UnitySubsystems/XRSDKOpenVR/UnitySubsystemsManifest.json.
[Subsystems] 1 'inputs' descriptors matched in UnitySubsystems/XRSDKOpenVR/UnitySubsystemsManifest.json
[Subsystems] 1 'displays' descriptors matched in UnitySubsystems/XRSDKOpenVR/UnitySubsystemsManifest.json
[Subsystems] No descriptors matched for  meshings in UnitySubsystems/XRSDKOpenVR/UnitySubsystemsManifest.json.
GfxDevice: creating device client; threaded=1
Direct3D:
    Version:  Direct3D 11.0 [level 11.1]
    Renderer: NVIDIA GeForce GTX 1070 (ID=0x1b81)
    Vendor:   
    VRAM:     8088 MB
    Driver:   27.21.14.5655
Begin MonoManager ReloadAssembly
- Completed reload, in  0.241 seconds
D3D11 device created for Microsoft Media Foundation video decoding.
[I][12:07:57.579901][] Log started.

[I][12:07:57.583401][] Subsystems registered.

[I][12:07:57.584402][] Running after assemblies loaded...

[I][12:07:57.623901][] XR Settings: null

[I][12:07:57.624401][] XR Manager Settings: null

Most notably, I think is this line before the subsystems: Plugins: Failed to load 'XRSDKOpenVR' because one or more of its dependencies could not be loaded., and of course, the XR Settings being null.

So it still even seems to be trying to load XRSDKOpenVR, but because it fails, the behavior is what I wanted! But perhaps that provides a clue as to where the "loading regardless of the init on startup" issue is coming from?

One more side note that might help us find the source. I also have the VIU in this project. Once after building with "Initialize XR on Startup" disabled, and "OpenVR Loader" unchecked, it prompted me to turn VR back on like so:

image

I clicked "Use recommended (True)". It re-enabled the "OpenVR Loader" checkbox in the XR Plug-in Management section, but still left "Initialize XR on Startup" disabled. So it was pleased enough to think Virtual Reality was enabled regardless of that checkbox too!

nitz avatar Mar 01 '21 17:03 nitz

I second this issue as it's still persistent in 1.1.4.

I have a solution and can do a PR but I want to make sure this is the same issue. @nitz does this only happen to you in a build? For me it didn't auto-initialize in-editor but in a build it would always auto-initialize and the SteamVR window would pop up.

Verex avatar Jul 30 '21 15:07 Verex

I was only testing it in builds. My development machine isn't the same machine I typically use for testing, so I wasn't investigating it in the editor at all. I'm not in this particular project this week but can check sometime next week!

nitz avatar Jul 30 '21 15:07 nitz

@nitz I posted a PR for this that possibly fixes it. If you have time to test check out that request and if it solves your issue and please leave a comment on your results. Thanks!

Verex avatar Jul 30 '21 16:07 Verex

@Verex I had this exact same issue and your modifications definitely helped me with it, although now I'm having a critical error whenever I start SteamVR, although that may be completely unrelated, so really appreciate that pull request, you're a savior!

emongev avatar Aug 05 '21 23:08 emongev

Remember how I said I hoped to check this out "next week" like 3 weeks ago, @Verex?

Well, I've finished putting out the fires I needed to for unrelated projects, and have finally got to give this a shot.

It worked fantastically. #102 should definitely be merged! Thanks for figuring it out!

nitz avatar Aug 16 '21 16:08 nitz