123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using System;
- using UnityEngine;
- using Valve.VR;
- namespace SicknessReduction.Visual
- {
- public class IpdInfo : MonoBehaviour
- {
- private SteamVR_Events.Action ipdChangedAction;
- public delegate void OnIpdChangedEvent(float ipdMeters);
- public event OnIpdChangedEvent onIpdChanged; // data event
- private void OnEnable()
- {
- SteamVR_Events.DeviceConnected.Listen(OnDeviceConnected);
- ipdChangedAction = SteamVR_Events.SystemAction(EVREventType.VREvent_IpdChanged, OnIpdChanged);
- ipdChangedAction.enabled = true;
- }
- private void OnIpdChanged(VREvent_t vrEvent)
- {
- var ipdMeters = vrEvent.data.ipd.ipdMeters;
- onIpdChanged?.Invoke(ipdMeters);
- Debug.Log($"IPD changed to {ipdMeters}");
- }
- private void OnDisable()
- {
- ipdChangedAction.enabled = false;
- }
- private void OnDeviceConnected(int index, bool connected)
- {
- if (index < 0) return;
- if (!connected || OpenVR.System == null) return;
- var deviceClass = OpenVR.System.GetTrackedDeviceClass((uint) index);
- if (deviceClass != ETrackedDeviceClass.HMD) return;
- var error = ETrackedPropertyError.TrackedProp_Success;
- var ipdMeters = OpenVR.System.GetFloatTrackedDeviceProperty((uint) index,
- ETrackedDeviceProperty.Prop_UserIpdMeters_Float, ref error);
- if (error != ETrackedPropertyError.TrackedProp_Success)
- {
- Debug.LogError($"Could not get ipd for HMD at index {index}");
- }
- else
- {
- onIpdChanged?.Invoke(ipdMeters);
- Debug.Log($"Initial IPD = {ipdMeters}");
- }
- Debug.Log("Controller got connected at index:" + index);
- }
- }
- }
|