Search Unity

Set VR SDK from script

Discussion in 'AR/VR (XR) Discussion' started by TonyWongHulu, Jul 30, 2016.

  1. TonyWongHulu

    TonyWongHulu

    Joined:
    Jun 3, 2016
    Posts:
    17
    Hi,

    I see that you can enable VR in the build settings from script, but is it also possible to set which VR SDK's will be used from script as well?

    Thanks
    Tony
     
  2. ScottF

    ScottF

    Vice President, Platforms

    Joined:
    Jul 31, 2013
    Posts:
    96
  3. TonyWongHulu

    TonyWongHulu

    Joined:
    Jun 3, 2016
    Posts:
    17
    I'm not calling that at all at the moment, but by default Unity is already trying to start VR with one of the devices in the list.

    Should I turn off VR as soon as I can to stop this happening?

    Thanks
    Tony
     
  4. ScottF

    ScottF

    Vice President, Platforms

    Joined:
    Jul 31, 2013
    Posts:
    96
    If you add 'None' to the top of the VRDevices list Unity will always start up without a VR device enabled.
     
  5. Gruguir

    Gruguir

    Joined:
    Nov 30, 2010
    Posts:
    340
    @ScottF I've made some tests with a script switching between OpenVR and no VR, and i can see OpenVR initializing when i switch to it, but VRSettings.enabled keeps returning false and i can't get vr display or tracking again.

    Same if i set "None" before "OpenVr" in "Virtual Reality SDKs" list. OpenVR works as intended if in first position.

    One of several attempts :

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.VR;
    4. using UnityEngine.UI;
    5.  
    6. public class VrDeviceSwitcherScript : MonoBehaviour {
    7.  
    8.     public Text threeDModeMessage;
    9.     public Text vrSettingStateMessage;
    10.     public string vrLoadedDeviceString;
    11.  
    12.     void Update ()
    13.     {
    14.         vrSettingStateMessage.text = "Vr Settings enabled =  " + VRSettings.enabled;
    15.         vrLoadedDeviceString = VRSettings.loadedDeviceName;
    16.         if (vrLoadedDeviceString == "") vrLoadedDeviceString = "None";
    17.         threeDModeMessage.text = "3D Mode : [M] key to switch : " + vrLoadedDeviceString;
    18.         if (Input.GetKeyDown("m")) VrSwitchDevice();
    19.     }
    20.  
    21.     void VrSwitchDevice()
    22.     {
    23.         if (vrLoadedDeviceString == "None") VRSettings.LoadDeviceByName("OpenVR");
    24.         else if (vrLoadedDeviceString == "OpenVR") VRSettings.LoadDeviceByName("");
    25.     }
    26.  
    27. }
    28.  
    Edit : i also tried to switch VRSettings.enabled by script.
     
  6. TsViCo

    TsViCo

    Joined:
    Jul 25, 2016
    Posts:
    4
    Is it save to have the Oculus SDK and the OpenVR SDK in the SDK list or can this lead to bugs and problems for our players?

    We have an active steam game and our players want to choose between Oculus and OpenVR. Right now we have only OpenVR support. Our preferred way is to deliver two builds, one for Oculus and one for OpenVR. But if you can guarantee that it will work just fine with both SDK activated we will try this.
     
  7. Gruguir

    Gruguir

    Joined:
    Nov 30, 2010
    Posts:
    340
    I guess that you'd better include only the VR sdk related to the distribution platform.

    I'm not sure to understand, as OpenVR handles Oculus Rift anyway.

    Now, i'm worried not being able to switch back and forth between VR and non VR mode (VrSettings.enabled returns false). Maybe i'm doing something wrong, i'd appreciate some advices.
     
  8. ScottF

    ScottF

    Vice President, Platforms

    Joined:
    Jul 31, 2013
    Posts:
    96
    @Gruguir In your sample code I do not see where you ever set VRSettings.enabled = true? It will remain fails even after calling LoadDeviceByName. I saw your edited post to say you tried toggling this but that appears to be what is missing.

    @TsViCo It is not necessary to build multiple versions of your game. We regularly test with multiple SDKs and switching between them. The system works by moving down the list until the best SDK is matched. So if you set order to: Oculus, OpenVR your game would always use Oculus if an Rift was in use and would fallback to OpenVR for Vive. As you said you could of course also give end-users control over which SDK, in which case starting with None is likely a good strategy.
     
  9. Gruguir

    Gruguir

    Joined:
    Nov 30, 2010
    Posts:
    340
    @ScottF Thanks for your help. I tried to call 'VRSettings.enabled = true' about everywhere and it didn't worked until i put it in Update(). I'll have to take another look at the script flow, but i got the switch to work.

    Anyway The new VR SDK system looks very convenient. Good to know that we can easily deal with multiple VR sdks in one build. I'm really impressed by 5.4 so far :)
     
  10. Deleted User

    Deleted User

    Guest

    In my case, I use None at the top of the Android SDK list, so I can run in editor without SteamVR starting. I also have a build pipeline. If None is at the top in that case, the Oculus Quest won't load the required SDK. So what I use prior and post build:
    Code (CSharp):
    1. PlayerSettings.SetVirtualRealitySDKs(BuildTargetGroup targetGroup, string[] sdks);
    2. AssetDataBase.SaveAssets();
    I don't know when that was added, but in older Unity Versions you might find
    Code (CSharp):
    1. UnityEditorInternal.VR.VREditor.SetVirtualRealitySDKs(BuildTargetGroup targetGroup, string[] sdks);
     
    Last edited by a moderator: Mar 19, 2020