Search Unity

Question Toggle VR and Non-VR Gameplay?

Discussion in 'XR Interaction Toolkit and Input' started by Revenir, Apr 16, 2021.

  1. Revenir

    Revenir

    Joined:
    Jun 8, 2015
    Posts:
    6
    Hi y'all. I'm using the XR Interaction Toolkit and Unity 2020.3.1. I'd like to set up a menu button where I could turn VR on or off from within the game. It seems like it should be feasible, but I'm not finding anything in the documentation on what settings I'd need to change.

    Could someone point me in the right direction this? Any resources would be super helpful.
     
  2. freso

    freso

    Joined:
    Mar 19, 2013
    Posts:
    73
    Depending on what you mean with "Switch VR on/off", I have done this to support game being played both in headset and on desktop flatscreen.

    I have one XRRig for VR, and one CharacterController for non-VR, then switch between them. Also make sure to update canvas event cameras and input controllers.
    To switch from VR to non-vr, I do something like this (add optimizations and error handling):
    Code (CSharp):
    1. XRRig.SetActive(false);
    2. CharacterControllerGameObject.SetActive(true);
    3. foreach (Canvas canvas in Resources.FindObjectsOfTypeAll<Canvas>()) {
    4.   // Reset eventcamera in all canvases
    5.   canvas.worldCamera = CharacterControllerGameObject.playerCamera;
    6.   // Reset UI Raycasters
    7.   canvas.GetComponent<GraphicRaycaster>().enabled = true;
    8.   canvas.GetComponent<TrackedDeviceGraphicRaycaster>().enabled = false;
    9. }
    10. // Switch Input controllers
    11. inputHandlerVRGameObject.SetActive(false);
    12. inputHandlerDesktopGameObject.SetActive(true);
    PS. As I recently found out, it's important to retag the cameras with the tag "MainCamera", when switching.
     
    Last edited: Apr 18, 2021