PrimitiveVectorDrawer.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace Unity.Mathematics.Editor
  5. {
  6. [CustomPropertyDrawer(typeof(bool2)), CustomPropertyDrawer(typeof(bool3)), CustomPropertyDrawer(typeof(bool4))]
  7. [CustomPropertyDrawer(typeof(double2)), CustomPropertyDrawer(typeof(double3)), CustomPropertyDrawer(typeof(double4))]
  8. [CustomPropertyDrawer(typeof(float2)), CustomPropertyDrawer(typeof(float3)), CustomPropertyDrawer(typeof(float4))]
  9. [CustomPropertyDrawer(typeof(int2)), CustomPropertyDrawer(typeof(int3)), CustomPropertyDrawer(typeof(int4))]
  10. [CustomPropertyDrawer(typeof(uint2)), CustomPropertyDrawer(typeof(uint3)), CustomPropertyDrawer(typeof(uint4))]
  11. [CustomPropertyDrawer(typeof(DoNotNormalizeAttribute))]
  12. class PrimitiveVectorDrawer : PropertyDrawer
  13. {
  14. static class Content
  15. {
  16. public static readonly string doNotNormalizeCompatibility = L10n.Tr(
  17. $"{typeof(DoNotNormalizeAttribute).Name} only works with {typeof(quaternion)} and primitive vector types."
  18. );
  19. public static readonly string doNotNormalizeTooltip =
  20. L10n.Tr("This value is not normalized, which may produce unexpected results.");
  21. public static readonly GUIContent[] labels2 = { new GUIContent("X"), new GUIContent("Y") };
  22. public static readonly GUIContent[] labels3 = { new GUIContent("X"), new GUIContent("Y"), new GUIContent("Z") };
  23. public static readonly GUIContent[] labels4 = { new GUIContent("X"), new GUIContent("Y"), new GUIContent("Z"), new GUIContent("W") };
  24. }
  25. public override bool CanCacheInspectorGUI(SerializedProperty property)
  26. {
  27. return false;
  28. }
  29. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  30. {
  31. var height = EditorGUIUtility.singleLineHeight;
  32. if (!EditorGUIUtility.wideMode)
  33. height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
  34. return height;
  35. }
  36. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  37. {
  38. var subLabels = Content.labels4;
  39. var startIter = "x";
  40. switch (property.type[property.type.Length - 1])
  41. {
  42. case '2':
  43. subLabels = Content.labels2;
  44. break;
  45. case '3':
  46. subLabels = Content.labels3;
  47. break;
  48. case '4':
  49. subLabels = Content.labels4;
  50. break;
  51. default:
  52. {
  53. if (property.type == nameof(quaternion))
  54. startIter = "value.x";
  55. else if (attribute is DoNotNormalizeAttribute)
  56. {
  57. EditorGUI.HelpBox(EditorGUI.PrefixLabel(position, label), Content.doNotNormalizeCompatibility, MessageType.None);
  58. return;
  59. }
  60. break;
  61. }
  62. }
  63. if (attribute is DoNotNormalizeAttribute && string.IsNullOrEmpty(label.tooltip))
  64. label.tooltip = Content.doNotNormalizeTooltip;
  65. EditorGUI.BeginProperty(position, label, property);
  66. EditorGUI.MultiPropertyField(position, subLabels, property.FindPropertyRelative(startIter), label);
  67. EditorGUI.EndProperty();
  68. }
  69. }
  70. }