SteamVR_TrackingReferenceManager.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Valve.VR
  5. {
  6. public class SteamVR_TrackingReferenceManager : MonoBehaviour
  7. {
  8. private Dictionary<uint, TrackingReferenceObject> trackingReferences = new Dictionary<uint, TrackingReferenceObject>();
  9. private void OnEnable()
  10. {
  11. SteamVR_Events.NewPoses.AddListener(OnNewPoses);
  12. }
  13. private void OnDisable()
  14. {
  15. SteamVR_Events.NewPoses.RemoveListener(OnNewPoses);
  16. }
  17. private void OnNewPoses(TrackedDevicePose_t[] poses)
  18. {
  19. if (poses == null)
  20. return;
  21. for (uint deviceIndex = 0; deviceIndex < poses.Length; deviceIndex++)
  22. {
  23. if (trackingReferences.ContainsKey(deviceIndex) == false)
  24. {
  25. ETrackedDeviceClass deviceClass = OpenVR.System.GetTrackedDeviceClass(deviceIndex);
  26. if (deviceClass == ETrackedDeviceClass.TrackingReference)
  27. {
  28. TrackingReferenceObject trackingReference = new TrackingReferenceObject();
  29. trackingReference.trackedDeviceClass = deviceClass;
  30. trackingReference.gameObject = new GameObject("Tracking Reference " + deviceIndex.ToString());
  31. trackingReference.gameObject.transform.parent = this.transform;
  32. trackingReference.trackedObject = trackingReference.gameObject.AddComponent<SteamVR_TrackedObject>();
  33. trackingReference.renderModel = trackingReference.gameObject.AddComponent<SteamVR_RenderModel>();
  34. trackingReference.renderModel.createComponents = false;
  35. trackingReference.renderModel.updateDynamically = false;
  36. trackingReferences.Add(deviceIndex, trackingReference);
  37. trackingReference.gameObject.SendMessage("SetDeviceIndex", (int)deviceIndex, SendMessageOptions.DontRequireReceiver);
  38. }
  39. else
  40. {
  41. trackingReferences.Add(deviceIndex, new TrackingReferenceObject() { trackedDeviceClass = deviceClass });
  42. }
  43. }
  44. }
  45. }
  46. private class TrackingReferenceObject
  47. {
  48. public ETrackedDeviceClass trackedDeviceClass;
  49. public GameObject gameObject;
  50. public SteamVR_RenderModel renderModel;
  51. public SteamVR_TrackedObject trackedObject;
  52. }
  53. }
  54. }