openvr icon indicating copy to clipboard operation
openvr copied to clipboard

Simple Question: how do I find out which controllers are being used?

Open MiddleManGames opened this issue 2 years ago • 3 comments

how do I find out which controllers are being used in code in Unity?

Can't find this anywhere.

MiddleManGames avatar Nov 03 '23 17:11 MiddleManGames

You can use GetStringTrackedDeviceProperty and fetch Prop_ModelNumber_String.

danwillm avatar Nov 03 '23 21:11 danwillm

Thanks. I ended up using this. I take it it will work just as well.

void OnEnable() {
    InputDevices.deviceConnected += DeviceConnected;
    List<InputDevice> devices = new List<InputDevice>();
    InputDevices.GetDevices(devices);
    foreach (var device in devices)
        DeviceConnected(device);
}
void OnDisable() {
    InputDevices.deviceConnected -= DeviceConnected;
}

void DeviceConnected(InputDevice device) {
    string name = "";

    // The Left Hand
    if ((device.characteristics & InputDeviceCharacteristics.Left) != 0) {
        //Use device.name here to identify the current Left Handed Device
        print("Left: " + device.name);
        name = device.name;
    }
    // The Right hand
    else if ((device.characteristics & InputDeviceCharacteristics.Right) != 0) {
        //Use device.Name here to identify the current Right Handed Device
        print("Right: " + device.name);
        name = device.name;
    }

    if (name.Contains("quest", StringComparison.OrdinalIgnoreCase) || name.Contains("rift", StringComparison.OrdinalIgnoreCase)) {
        Menu.menu.controllerHasMenuButton = false;
        Menu.menu.controllerIsVive = false;
        Menu.menu.controllerIsIndex = false;
    } else if (name.Contains("vive", StringComparison.OrdinalIgnoreCase) && !name.Contains("cosmos", StringComparison.OrdinalIgnoreCase)) {
        Menu.menu.controllerHasMenuButton = true;
        Menu.menu.controllerIsVive = true;
        Menu.menu.controllerIsIndex = false;
    } else if (name.Contains("index", StringComparison.OrdinalIgnoreCase)) {
        Menu.menu.controllerHasMenuButton = false;
        Menu.menu.controllerIsVive = false;
        Menu.menu.controllerIsIndex = true;
    } else {
        Menu.menu.controllerHasMenuButton = true;
        Menu.menu.controllerIsVive = false;
        Menu.menu.controllerIsIndex = false;
    }
}

MiddleManGames avatar Nov 04 '23 16:11 MiddleManGames

I believe this is the wrong forum to be discussing this topic, this looks like you're using some kind of wrapper on top of openvr to achieve this.

danwillm avatar Nov 05 '23 01:11 danwillm