Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

XR choice by code

Discussion in 'AR/VR (XR) Discussion' started by ghiboz, Sep 26, 2019.

  1. ghiboz

    ghiboz

    Joined:
    Sep 7, 2012
    Posts:
    465
    hi all!! I have a question: I'm migrating my project from 2017 to 2018 and the vr doesn't work.....
    I made this in 2017: in the project settings I added in order as VR:
    • none
    • oculus
    • openvr
    and by code (reading a configuration) I enable the selected element....but seems that in 2018 it doesn't work

    here is my set:



    and that's my code I use at startup to choose whick XR:
    Code (CSharp):
    1. /// <summary>
    2.     /// load the device
    3.     /// </summary>
    4.     /// <param name="newDevice"></param>
    5.     /// <returns></returns>
    6.     IEnumerator LoadDevice(string newDevice)
    7.     {
    8.         if (String.Compare(XRSettings.loadedDeviceName, newDevice, true) != 0)
    9.         {
    10.             XRSettings.LoadDeviceByName(newDevice);
    11.             yield return null;
    12.             XRSettings.enabled = true;
    13.         }
    14.     }
    where as newDevice I use "Oculus" or "OpenVR"
    the strange thing seems that the code XRSettings.enabled = true; doesn't work for example... seems that everything is 'read only'

    thanks in advance
     
    Pixygon likes this.
  2. Pixygon

    Pixygon

    Joined:
    May 9, 2013
    Posts:
    26
    I solved it with a build script which sets up/removes the SDK based on what platform I am targeting. This must be done before build, since, as you noticed, that list is only readonly at runtime
     
  3. ROBYER1

    ROBYER1

    Joined:
    Oct 9, 2015
    Posts:
    1,450
    Do you have an example of how to write this kind of build script?
     
  4. BiggerInsideVR

    BiggerInsideVR

    Joined:
    Nov 23, 2017
    Posts:
    7
    @ROBYER1

    Example :

    Code (CSharp):
    1. [MenuItem("Set OpenVR SDK")]
    2. public static void SetOpenVRSDK()
    3. {
    4.             PlayerSettings.SetVirtualRealitySupported(BuildTargetGroup.Standalone, true);
    5.             PlayerSettings.SetVirtualRealitySDKs(BuildTargetGroup.Standalone, new[] { "OpenVR" });
    6. }
     
    Pixygon likes this.