123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- using Valve.VR;
- using Debug = UnityEngine.Debug;
- namespace Tracking
- {
- public class TrackerIndexManager : MonoBehaviour
- {
- private IEnumerable<IViveTracker> trackers;
- private void OnEnable()
- {
- SteamVR_Events.DeviceConnected.Listen(OnDeviceConnected);
- trackers = GetComponentsInChildren<IViveTracker>();
- }
- 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.GenericTracker) return;
- var error = ETrackedPropertyError.TrackedProp_Success;
- var serialNumberBuilder = new StringBuilder();
- OpenVR.System.GetStringTrackedDeviceProperty((uint) index,
- ETrackedDeviceProperty.Prop_SerialNumber_String, serialNumberBuilder, 64, ref error);
- if (error != ETrackedPropertyError.TrackedProp_Success)
- {
- Debug.LogError($"Could not get serial number for tracker at index {index}");
- }
- else
- {
- UpdateTrackerObject(serialNumberBuilder.ToString(), index);
- }
- }
- private void UpdateTrackerObject(string id, int index)
- {
- Debug.Log($"Tracker with serial number {id} got connected at index {index}");
- var t = trackers.FirstOrDefault(tracker => tracker.Id == id);
- t?.TrackedObject.SetDeviceIndex(index);
- }
- }
- }
|