Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Unity 2022.2 is now available as the latest Tech release.
    Dismiss Notice
  3. We are making some changes to the DOTS forums.
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Enable VR with scripting?

Discussion in 'AR/VR (XR) Discussion' started by linojon, Jun 14, 2015.

  1. linojon

    linojon

    Joined:
    Aug 25, 2014
    Posts:
    118
    Unity 5.1 has integrated VR, its in the Build Player Settings as a checkbox. I want a startup scene that is not VR, then when the user starts it enables the VR camera. How can I do that?
     
  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    with the checkbox checked put "VRSettings.enabled = false;" in startup and VRSettings.enabled = true; when you want to enable VR mode.
     
    linojon likes this.
  3. EdBlais

    EdBlais

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    296
    You could do what Lennart messaged above or you could change the VRSettings.loadedDevice to none and Oculus depending on which mode you want. This can be changed at runtime, but in the Editor it will force you out of play mode. In standalone builds it will not close the window or revert the scene. It continues from where you currently are in your scene.

    This is working in 5.1.0p1. Unfortunately there was a bug with this in previous versions, so you will need to update to the latest patch if you wish to use this.
     
    jashan likes this.
  4. linojon

    linojon

    Joined:
    Aug 25, 2014
    Posts:
    118
    thanks for the responses. The VRSettings.enabled is working fine for me.

    @Ed, what are the possible values for device? i heard that GearVR has been added as a device but that would be Android, versus Oculus is just desktop. Are there any plans to include Cardboard natively for Android and iOS?
     
  5. EdBlais

    EdBlais

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    296
    This list of devices can be found here: http://docs.unity3d.com/ScriptReference/VR.VRDeviceType.html
    GearVR is included in the Oculus device type. So when you choose DeviceType oculus. You will be able to deploy to Android and run on a gearvr.

    We do have plans to add additional native support for more Plugins like Cardboard. As a side note (we haven't tested this) it may be possible to use DeviceType stereo to make Cardboard apps. However, it would be more appropriate to use the cardboard plugin.
     
    shakhruz likes this.
  6. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    The cardboard Unity asset includes distortion code for lens correction. This need to be done. A standard stereo device type will not look optimal. The distortion code also varies for the different cardboard lenses. v1 and 2
     
    CAD_Schroer_GmbH likes this.
  7. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,304
    What about OpenVR / SteamVR? For now, I prefer their SteamVR plugin (and would actually prefer to have the native Unity integration be designed much more like that). But eventually, it would be nice to have it all integrated natively into Unity.

    It also seems that sometimes, the native integration conflicts with the SteamVR plugin: I just recently had the case that I had "Virtual Reality Supported" switched off but it looked as if both was trying to render to the HMD with quite a bit of odd flickering going on ... but that could also have been something else - I just never saw that when using the SteamVR plugin in Unity 5. I'll probably add VRSettings.loadedDevice = VRDeviceType.None; in startup just to be sure.

    Another really important one is Sony Project Morpheus, of course.
     
  8. snowsrfr1

    snowsrfr1

    Joined:
    Nov 4, 2013
    Posts:
    21
    I'm working with the Note 4 / Gear VR in 5.1.2. If you check VR Supported in the Player Settings, the Android Device asks you to place your phone in the Gear VR before the game begins... What if you have some levels in VR and some in Non-VR, and want to start in a Non-VR menu?

    My test today was an .apk with VR Support unchecked in the player settings. The user starts in a non-VR level. When the user loads a VR level, I have a script in said VR level where the Start() function sets VRSettings.enabled = true and also sets VRSettings.loadedDevice to VRDeviceType.Oculus.

    However, once I start my VR level and this code executes, I do not see dual cameras rendering. Additionally, inserting my phone into the Gear VR once my VR level is loaded will launch the Oculus VR Home app. Prior to inserting my device into the Gear VR when my VR level loads, my logcat actually shows the same OVR reference errors from the Android Personal Edition bugs that Ed Unity and I worked through in versions before 5.1.2

    Maybe it's not a bug since I could understand the argument that my implementation is an edge case, but.... Repro steps:

    VR supported unchecked.
    Scene 0: Scene 0 is non VR, using new Unity GUI elements to load a VR-enabled level (call it Scene 1)
    Scene 1: When Scene 1 loads, code is executed to enable VR and set device type = oculus.
    Symptom - No dual camera rendering. No Prompt to insert device into Gear VR. When the user inserts the device into the Gear VR voluntarily, the Oculus VR Home App launches.
     
    Last edited: Aug 5, 2015
  9. EdBlais

    EdBlais

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    296
    We are planning to integrate more than just Cardboard. Like you said, eventually it would be nice to have everything integrated natively. The ground work has been done to make adding additional plugins easy. We are still adjusting the integration to better suite how you and other users feel is the best way work with VR in Unity. We are taking all advice into consideration and we thank you for your contributions to making the VR integration as best as possible. You should be seeing some changes soon based on everyone's feedback so far. A little bit more work still needs to be done for Projects settings with regards to selecting different device types. Once that work is in, we can start adding additional devices like you mentioned.

    The prompt to connect to the Device is actually done in the Oculus plugin. We have no control over the prompt. GearVR applications cannot be partially VR and partially non-VR. As in you cannot switch into or out of GearVR at any point in the application. It either is a GearVR app or it is not. So being able to switch VRDeviceType with GearVR shouldn't actually be possible.
     
    jashan likes this.
  10. snowsrfr1

    snowsrfr1

    Joined:
    Nov 4, 2013
    Posts:
    21
    Thanks for the response, Ed Unity. I guess I'm looking at making two separate yet companion apps (which is annoying but not the end of the world).

    Application.persistentDataPath is the right place for me to share something like a flat JSON file where the two apps could share data about one another's progress, correct?
     
  11. EdBlais

    EdBlais

    Unity Technologies

    Joined:
    Nov 18, 2013
    Posts:
    296
    You might be able to use persistentDataPath, but I'm not sure if two separate applications would return the same path. If they do, then you should be able to share the progress through that location. I've actually never run into this situation, so I don't know what the best solution on Android would be. Try asking on the Android Forums just to be sure.
     
  12. RHeeger

    RHeeger

    Joined:
    Jul 30, 2014
    Posts:
    2
    Do you know if there are any plans to support this functionality in the future?
     
    taguados likes this.
  13. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,304
    Thank you! That's great to hear!!!
     
  14. derkoi

    derkoi

    Joined:
    Jul 3, 2012
    Posts:
    2,206
    That's pretty annoying. I wanted players to use VR if they have a headset, now I'll need to make a separate app and sell it separately, which will not please my player base.
     
  15. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    This does not work well , please see my post in the VR forums, The problem is the app launches to VR and then only kicks out of it and into regular play. That is an unacceptable user experience. I need the app to launch smoothly into regular game play. This is for oculus.

    http://forum.unity3d.com/threads/enabling-vr-at-runtime-please-help.384314/
     
  16. taguados

    taguados

    Joined:
    Apr 19, 2017
    Posts:
    4
    Hi,

    I was wondering if changing between VR and Non-VR is still not support on GearVR.

    I'm trying to start an app with Non-VR and then activate it via script but even when I use VRSettings.enabled=false and VRSettings.loadedDevice=VRDeviceType.None the first thing the apps does is asking you to put the pone in the GearVR so then you see the Non-VR app through the glasses and if I get the pone out the app just closes automatically.

    So basically I would like to know if this is something doable or it is imposible so I don't keep trying to do something that I won't get done.

    Thanks!
     
  17. greggtwep16

    greggtwep16

    Joined:
    Aug 17, 2012
    Posts:
    1,546
    Unlike daydream or cardboard, for gear vr Oculus does not allow you to do this. Perhaps one day they'll change their mind, you could try contacting them. Unity isn't the party holding this up.

    https://forums.oculus.com/developer...ween-vr-mode-and-non-vr-mode-is-not-supported
     
    taguados likes this.
  18. taguados

    taguados

    Joined:
    Apr 19, 2017
    Posts:
    4
  19. chunk_split

    chunk_split

    Joined:
    Dec 25, 2017
    Posts:
    7
    I'd love to be able to do this, personally. In my case my goal is to have separate scenes where I can have a VR scene with the same objects, but another scene dedicated just for testing/debug which is more isolated where I don't have to enter VR. For now, I'm limited to manually going into Edit -> Project Settings -> Player -> XR Settings in order to enable/disable VR so I can have more direct control (or if my laptop isn't currently connected to my eGPU/Vive).

    ¯\_(ツ)_/¯