Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice
  2. Read here for Unity's latest plans on OpenXR.
    Dismiss Notice

Distributing a game with optional VR mode.

Discussion in 'AR/VR (XR) Discussion' started by User340, May 27, 2020.

Thread Status:
Not open for further replies.
  1. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    2,981
    Is it possible to distribute a game that is non-VR with the ability to turn on VR at runtime if a headset is present? Or do you need to make separate apps, one VR and one regular?
     
  2. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    953
    It is possible, though not based on detecting a head set. You have to manually enable/disable VR and provide the users a chance to toggle that.

    Without knowing what version of Unity you are using, and what type of VR system (Built in or XR Plugin) I can't really tell you more. However, there are plentiful resources for both in regards to how to manually enable/disable VR/XR.
     
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    2,981
    Thanks for the response.

    I'm using Unity 2018.4.22. I'm using built-in Oculus support.

    Do I need to distribute my app with "Virtual Reality Supported" turned on (in Player Settings)? Or should I leave it off and turn it on at runtime?
     
  4. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    953
    Leave it on, add the None device as the first device and load the Oculus device when you want to switch to VR. Then load the None device to disable VR when you want to switch out.
     
    User340 likes this.
  5. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    2,981
    Sounds good, thanks.
     
    AlexTor likes this.
  6. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,389
    I (and a few others) have posted here asking specifically for docs or info on exactly how to do this with XR Plugin system and had no reply.

    I'm sure it's somwhere, but it's missing from the official docs, and I haven't been able to find anything for the new system specifically.

    If you could give me a link (or a short description, if that's easier) that would be awesome (and I'll add it to the FAQ, hopefully others won't need to keep asking the same question in future :)).
     
    renard936 and MageC like this.
  7. Valyrin_

    Valyrin_

    Joined:
    Aug 24, 2020
    Posts:
    4
    Has there been any more info on this? I'm attempting to do the exact same thing (optional vr launch mode at game start) and having a hard time figuring it out

    Engine: Unity 2020.3.15f2
    Template: Universal Render Pipeline
    XR Interaction Toolkit Package: 1.0.0-pre.5 [Preview]
    XR Plugin Management: 4.0.7
     
  8. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    953
  9. Valyrin_

    Valyrin_

    Joined:
    Aug 24, 2020
    Posts:
    4
    @joejo I've read through that today and implemented it on an empty game object, then called that using StartCoroutine(StartXRCoroutine()) in the awake function of the script. However this did not work - SteamVR did not open, and if I already had it open, my HMD was not moving the camera. Is this the correct way to do this? My controls work if I check the "Start XR on Initialization" in the XR plugin manager, so I know it's a problem with the initialization code
     
  10. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    953
  11. Valyrin_

    Valyrin_

    Joined:
    Aug 24, 2020
    Posts:
    4
    @joejo In cleaning up a little of my code to upload to github, I found a mistake: I had put the function in the Start() method this time around instead of Awake(). I actually have this working now. Please, pin this somewhere as I've seen about 50 dead-end threads on this today, and can't believe I finally got it working! I believe this can also finally be marked as resolved!

    For other people who find this post, here is the solution for VR and non-VR in a single build.

    Note that this will start your game in 2D, NOT VR. After it loads past the unity splash screen, it will switch to VR.

    You need to create an empty game object in your scene (you do not have to switch scenes for this to work) and attach the below script to it. After this, build your game and launch the game with the "--enable-vr" flag after the game.exe. In a Windows Shortcut, this looks like "C:\Path\To\Game.exe" --enable-vr


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR.Management;
    5.  
    6. public class VRSwitcher : MonoBehaviour
    7. {
    8.     public void Awake()
    9.     {
    10.         StartCoroutine(StartXRCoroutine());
    11.     }
    12.  
    13.     // This function checks out startup arguments to see if we want VR
    14.     // To do this, create a desktop shortcut and add the arg at the end.
    15.     // Example: "C:\Path\To\Game.exe" --enable-vr
    16.     private static bool GetArg(string name)
    17.     {
    18.         var args = System.Environment.GetCommandLineArgs();
    19.         for (int i = 0; i < args.Length; i++)
    20.         {
    21.             Debug.Log($"Arg {i}: {args[i]}");
    22.             if (args[i] == name)
    23.             {
    24.                 return true;
    25.             }
    26.         }
    27.         return false;
    28.     }
    29.     // From unity docs
    30.     // https://docs.unity3d.com/Packages/com.unity.xr.management@4.0/manual/EndUser.html
    31.     public IEnumerator StartXRCoroutine()
    32.     {
    33.         var enableVRArg = "--enable-vr";
    34.  
    35.         // Only run the code block when we want VR
    36.         Debug.Log("Looking if VR should enable");
    37.         if (GetArg(enableVRArg))
    38.         {
    39.             Debug.Log("Initializing XR...");
    40.             yield return XRGeneralSettings.Instance.Manager.InitializeLoader();
    41.  
    42.             if (XRGeneralSettings.Instance.Manager.activeLoader == null)
    43.             {
    44.                 Debug.LogError("Initializing XR Failed. Check Editor or Player log for details.");
    45.             }
    46.             else
    47.             {
    48.                 Debug.Log("Starting XR...");
    49.                 XRGeneralSettings.Instance.Manager.StartSubsystems();
    50.             }
    51.         }
    52.         else
    53.         {
    54.             Debug.Log("Did not find VR arg, starting in 2D");
    55.         }
    56.     }
    57. }
    58.  
     
    Last edited: Aug 10, 2021
    FlightOfOne, eudendeew and polemical like this.
  12. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    953
    Good idea, done.
     
    Valyrin_ likes this.
Thread Status:
Not open for further replies.
unityunity