Skip to content

Openverse Input System

streepje8 edited this page Jun 6, 2022 · 1 revision

The Openverse has a special system for retrieving user input. Here is a simple demo script that shows how to use it:

using Openverse.Input; //Include Openverse.Input
using UnityEngine;

public class InputSystemDemo : MonoBehaviour
{
    public OpenverseDevice leftController; //Create a place to store your device
    void Awake()
    {
        OpenverseInput.AddDeviceConnectionHandler(onDeviceConnect); //Tell Openverse that this script would like to be  notified when a new device connects.
    }

    //Create a device connection handler
    public void onDeviceConnect(OpenverseDevice device)
    {
        //You can listen for any device type, but for this demo we are only looking at the left controller
        if (device.type == OpenverseDeviceType.LeftController) 
            leftController = device;
    }

    void Update()
    {
        //Map names can be found at: https://docs.unity3d.com/Manual/xr_input.html#:~:text=Unity%20Input%20system.-,XR%20input%20mappings,-The%20following%20table
        if (leftController != null) //check if the controller is actually connected
        {
            //Get your input using OpenverseDevice.Get<TYPE>(NAME); You can also do ?.Get to check if the device is connected inline, this will return null when no device is connected
            Debug.Log("Trigger value: " + leftController.Get<float>("Trigger"));
            Debug.Log("Primary joystick values: " + leftController.Get<Vector2>("primary2DAxis"));

            if (leftController.Get<bool>("primaryButton"))
            {
                Debug.Log("Primary Button Pressed!");
            }
        }
    }
}

Clone this wiki locally