TrackedPoseDriverEditor.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditorInternal;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System;
  7. #if ENABLE_VR || ENABLE_AR
  8. using UnityEngine.Experimental.XR.Interaction;
  9. namespace UnityEngine.SpatialTracking
  10. {
  11. [CustomEditor(typeof(TrackedPoseDriver))]
  12. internal class TrackedPoseDriverEditor : Editor
  13. {
  14. static class Styles
  15. {
  16. public static GUIContent deviceLabel = EditorGUIUtility.TrTextContent("Device", "The Device to read tracking data from ");
  17. public static GUIContent poseLabel = EditorGUIUtility.TrTextContent("Pose Source", "The end point on the device to read tracking data from");
  18. public static GUIContent trackingLabel = EditorGUIUtility.TrTextContent("Tracking Type", "Whether Rotation or Position, or Both are applied from the source pose");
  19. public static GUIContent updateLabel = EditorGUIUtility.TrTextContent("Update Type", "Whether the Tracked Pose Driver updates in update, and/or just before rendering");
  20. public static GUIContent relativeLabel = EditorGUIUtility.TrTextContent("Use Relative Transform", "When this is set, the Tracked Pose Driver will use the original position of the object as a reference. This option will be deprecated in future releases");
  21. public static GUIContent poseProviderLabel = EditorGUIUtility.TrTextContent("Use Pose Provider", " [Optional] when a PoseProvider object is attached here, the pose provider will be used as the data source, not the Device/Pose settings on the Tracked Pose Driver");
  22. public static readonly string poseProviderWarning = "This Tracked Pose Driver is using an external component as its Pose Source.";
  23. public static readonly string devicePropWarning = "The selected Pose Source is not valid, please pick a different pose";
  24. public static readonly string cameraWarning = "The Tracked Pose Driver is attached to a camera, but is not tracking the Center Eye / HMD Reference. This may cause tracking problems if this camera is intended to track the headset.";
  25. }
  26. SerializedProperty m_DeviceProp = null;
  27. SerializedProperty m_PoseLabelProp = null;
  28. SerializedProperty m_TrackingTypeProp = null;
  29. SerializedProperty m_UpdateTypeProp = null;
  30. SerializedProperty m_UseRelativeTransformProp = null;
  31. SerializedProperty m_PoseProviderProp = null;
  32. void OnEnable()
  33. {
  34. m_DeviceProp = this.serializedObject.FindProperty("m_Device");
  35. m_PoseLabelProp = this.serializedObject.FindProperty("m_PoseSource");
  36. m_TrackingTypeProp = this.serializedObject.FindProperty("m_TrackingType");
  37. m_UpdateTypeProp = this.serializedObject.FindProperty("m_UpdateType");
  38. m_UseRelativeTransformProp = this.serializedObject.FindProperty("m_UseRelativeTransform");
  39. m_PoseProviderProp = this.serializedObject.FindProperty("m_PoseProviderComponent");
  40. }
  41. public override void OnInspectorGUI()
  42. {
  43. TrackedPoseDriver tpd = target as TrackedPoseDriver;
  44. serializedObject.Update();
  45. if (m_PoseProviderProp.objectReferenceValue == null)
  46. {
  47. EditorGUILayout.PropertyField(m_DeviceProp, Styles.deviceLabel);
  48. int selectedIndex = -1;
  49. for (int i = 0; i < TrackedPoseDriverDataDescription.DeviceData[m_DeviceProp.enumValueIndex].Poses.Count; ++i)
  50. {
  51. if ((int)TrackedPoseDriverDataDescription.DeviceData[m_DeviceProp.enumValueIndex].Poses[i] == m_PoseLabelProp.enumValueIndex)
  52. {
  53. selectedIndex = i;
  54. break;
  55. }
  56. }
  57. Rect rect = EditorGUILayout.GetControlRect();
  58. EditorGUI.LabelField(rect, Styles.poseLabel);
  59. rect.xMin += EditorGUIUtility.labelWidth;
  60. if (selectedIndex != -1)
  61. {
  62. int index = EditorGUI.Popup(rect, selectedIndex, TrackedPoseDriverDataDescription.DeviceData[m_DeviceProp.enumValueIndex].PoseNames.ToArray());
  63. m_PoseLabelProp.enumValueIndex = (int)TrackedPoseDriverDataDescription.DeviceData[m_DeviceProp.enumValueIndex].Poses[index];
  64. if(tpd &&
  65. (m_DeviceProp.enumValueIndex == 0 &&
  66. (m_PoseLabelProp.enumValueIndex != (int)(TrackedPoseDriver.TrackedPose.Center)) &&
  67. (m_PoseLabelProp.enumValueIndex != (int)TrackedPoseDriver.TrackedPose.ColorCamera)))
  68. {
  69. Camera camera = tpd.GetComponent<Camera>();
  70. if(camera != null)
  71. {
  72. EditorGUILayout.HelpBox(Styles.cameraWarning, MessageType.Warning, true);
  73. }
  74. }
  75. }
  76. else
  77. {
  78. int index = EditorGUI.Popup(rect, 0, TrackedPoseDriverDataDescription.DeviceData[m_DeviceProp.enumValueIndex].PoseNames.ToArray());
  79. m_PoseLabelProp.enumValueIndex = (int)TrackedPoseDriverDataDescription.DeviceData[m_DeviceProp.enumValueIndex].Poses[index];
  80. EditorGUILayout.HelpBox(Styles.devicePropWarning, MessageType.Warning, true);
  81. }
  82. }
  83. else
  84. {
  85. EditorGUILayout.HelpBox(Styles.poseProviderWarning, MessageType.Info, true);
  86. }
  87. EditorGUILayout.PropertyField(m_TrackingTypeProp, Styles.trackingLabel);
  88. EditorGUILayout.PropertyField(m_UpdateTypeProp, Styles.updateLabel);
  89. EditorGUILayout.PropertyField(m_UseRelativeTransformProp, Styles.relativeLabel);
  90. EditorGUILayout.PropertyField(m_PoseProviderProp, Styles.poseProviderLabel);
  91. serializedObject.ApplyModifiedProperties();
  92. }
  93. }
  94. }
  95. #endif