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. Dismiss Notice

Using OpenVR and Unity.XR with the existing UI

Discussion in 'AR/VR (XR) Discussion' started by Matt-HitIQ, Feb 25, 2019.

  1. Matt-HitIQ

    Matt-HitIQ

    Joined:
    Feb 15, 2019
    Posts:
    7
    Hi all,

    Apologies if this is answered or detailed somewhere, but I am yet to find it.

    I have a VR project aimed at Windows Desktop, with the idea to be able to use a VIVE or RIFT headset. I am using 2019.1

    I am using Unity.XR and the new XR input system. Pretty cool so far. No need for Oculus libraries or SteamVR specifically. I have a controller tracker allowing me to fire button events.

    I want to use the UI within VR but cannot seem to find simple information about how I go about doing this. It seems I will have to use the SteamVR and Oculus specific libraries, rather than just openVR on it's own.

    The way I am testing it now is to use a line renderer and place a box collider on the buttons in VR space I want it to interact with. This works but then I have to write code to invoke the buttons behaviours and turn them off. It's all rather dirty.

    My questions boil down to
    1) What do I have to use to get the new XR system working with the existing UI system? Do I have to add this in manually as I am starting to do so now?
    2) Or is there an event system component for XR like the OVR one that I missed?

    If I am to build 1, how would someone suggest (psuedo code-ish) I do this.

    My plan is -
    • Add a line renderer onto my VR controller gameobject
    • When it points (hovers) over an existing button box collider I invoke the
      button.Select();
      method.
    • When it moves away from the selected button, I invoke the event system to stop selecting
      EventSystem.current.SetSelectedGameObject(null);
      method.
    This is pretty tailored to the button right now, but using Selectable I could write something a little nicer.

    Any thoughts?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,842
    That's a tempting path, but it is the wrong one.

    The right way is to create an EventSystem input module. This will hook into the whole event system and so automatically work with all controls. You can model this off the OVR one, or the one that comes with VRTK, or just dig into the docs and do it from first principles.
     
  3. Matt-HitIQ

    Matt-HitIQ

    Joined:
    Feb 15, 2019
    Posts:
    7
    At this stage our project only needs a laser pointer and the ability to click UI buttons in VR and nothing else.
    Based off a good read of the new XR Input system + [this helpful script by @fariazz](https://forum.unity.com/threads/any-example-of-the-new-2019-1-xr-input-system.629824/) I was able to add in my own `VRButtonInput` class that simply
    • subscribes to a 'PointerOver' event in my laser pointer script. (Steam VR laser pointer as a reference)
    • subscribes to a 'Trigger' event from the XR input script
    • Uses the event system to send the correct events to the button selected (pointed at)
    Cheers
     
    JohnGerard and fariazz like this.