PokeInteractableEditor.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using UnityEditor;
  2. using UnityEngine;
  3. using Rokid.UXR.Interaction;
  4. namespace Rokid.UXR.Editor
  5. {
  6. [CustomEditor(typeof(PokeInteractable))]
  7. public class PokeInteractableEditor : UnityEditor.Editor
  8. {
  9. private PokeInteractable _interactable;
  10. private SerializedProperty _proximityFieldProperty;
  11. private SerializedProperty _surfaceProperty;
  12. private static readonly float DRAW_RADIUS = 0.02f;
  13. private void Awake()
  14. {
  15. _interactable = target as PokeInteractable;
  16. _proximityFieldProperty = serializedObject.FindProperty("_proximityField");
  17. _surfaceProperty = serializedObject.FindProperty("_surface");
  18. }
  19. public void OnSceneGUI()
  20. {
  21. Handles.color = EditorConstants.PRIMARY_COLOR;
  22. PlaneSurface plane = _surfaceProperty.objectReferenceValue as PlaneSurface;
  23. if (plane == null)
  24. {
  25. // TODO support non-planar surfaces for this gizmo?
  26. return;
  27. }
  28. Transform triggerPlaneTransform = plane.transform;
  29. IProximityField proximityField = _proximityFieldProperty.objectReferenceValue as IProximityField;
  30. if (triggerPlaneTransform == null
  31. || proximityField == null)
  32. {
  33. return;
  34. }
  35. Vector3 touchPoint = triggerPlaneTransform.position - triggerPlaneTransform.forward * _interactable.MaxDistance;
  36. Vector3 proximalPoint = proximityField.ComputeClosestPoint(touchPoint);
  37. Handles.DrawSolidDisc(touchPoint, triggerPlaneTransform.forward, DRAW_RADIUS);
  38. #if UNITY_2020_2_OR_NEWER
  39. Handles.DrawLine(touchPoint, proximalPoint, EditorConstants.LINE_THICKNESS);
  40. Handles.DrawLine(proximalPoint - triggerPlaneTransform.right * DRAW_RADIUS,
  41. proximalPoint + triggerPlaneTransform.right * DRAW_RADIUS, EditorConstants.LINE_THICKNESS);
  42. Handles.DrawLine(proximalPoint - triggerPlaneTransform.up * DRAW_RADIUS,
  43. proximalPoint + triggerPlaneTransform.up * DRAW_RADIUS, EditorConstants.LINE_THICKNESS);
  44. #else
  45. Handles.DrawLine(touchPoint, proximalPoint);
  46. Handles.DrawLine(proximalPoint - triggerPlaneTransform.right * DRAW_RADIUS,
  47. proximalPoint + triggerPlaneTransform.right * DRAW_RADIUS);
  48. Handles.DrawLine(proximalPoint - triggerPlaneTransform.up * DRAW_RADIUS,
  49. proximalPoint + triggerPlaneTransform.up * DRAW_RADIUS);
  50. #endif
  51. }
  52. }
  53. }