CinemachineImpulseDefinitionPropertyDrawer.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. namespace Cinemachine.Editor
  5. {
  6. [CustomPropertyDrawer(typeof(CinemachineImpulseDefinitionPropertyAttribute))]
  7. internal sealed class CinemachineImpulseDefinitionPropertyDrawer : PropertyDrawer
  8. {
  9. const int vSpace = 2;
  10. float HeaderHeight { get { return EditorGUIUtility.singleLineHeight * 1.5f; } }
  11. float DrawHeader(Rect rect, string text)
  12. {
  13. float delta = HeaderHeight - EditorGUIUtility.singleLineHeight;
  14. rect.y += delta; rect.height -= delta;
  15. EditorGUI.LabelField(rect, new GUIContent(text), EditorStyles.boldLabel);
  16. return HeaderHeight;
  17. }
  18. string HeaderText(SerializedProperty property)
  19. {
  20. var attrs = property.serializedObject.targetObject.GetType()
  21. .GetCustomAttributes(typeof(HeaderAttribute), false);
  22. if (attrs != null && attrs.Length > 0)
  23. return ((HeaderAttribute)attrs[0]).header;
  24. return null;
  25. }
  26. List<string> mHideProperties = new List<string>();
  27. public override float GetPropertyHeight(SerializedProperty prop, GUIContent label)
  28. {
  29. CinemachineImpulseDefinition myClass = null; // to access name strings
  30. SignalSourceAsset asset = null;
  31. float height = 0;
  32. mHideProperties.Clear();
  33. string prefix = prop.name;
  34. prop.NextVisible(true); // Skip outer foldout
  35. do
  36. {
  37. if (!prop.propertyPath.StartsWith(prefix))
  38. break;
  39. string header = HeaderText(prop);
  40. if (header != null)
  41. height += HeaderHeight + vSpace;
  42. // Do we hide this property?
  43. bool hide = false;
  44. if (prop.name == SerializedPropertyHelper.PropertyName(() => myClass.m_RawSignal))
  45. asset = prop.objectReferenceValue as SignalSourceAsset;
  46. if (prop.name == SerializedPropertyHelper.PropertyName(() => myClass.m_RepeatMode))
  47. hide = asset == null || asset.SignalDuration <= 0;
  48. else if (prop.name == SerializedPropertyHelper.PropertyName(() => myClass.m_Randomize))
  49. hide = asset == null || asset.SignalDuration > 0;
  50. if (hide)
  51. mHideProperties.Add(prop.name);
  52. else
  53. height += EditorGUI.GetPropertyHeight(prop, false) + vSpace;
  54. } while (prop.NextVisible(prop.isExpanded));
  55. return height;
  56. }
  57. public override void OnGUI(Rect rect, SerializedProperty prop, GUIContent label)
  58. {
  59. string prefix = prop.name;
  60. prop.NextVisible(true); // Skip outer foldout
  61. do
  62. {
  63. if (!prop.propertyPath.StartsWith(prefix))
  64. break;
  65. string header = HeaderText(prop);
  66. if (header != null)
  67. {
  68. rect.height = HeaderHeight;
  69. DrawHeader(rect, header);
  70. rect.y += HeaderHeight + vSpace;
  71. }
  72. if (mHideProperties.Contains(prop.name))
  73. continue;
  74. rect.height = EditorGUI.GetPropertyHeight(prop, false);
  75. EditorGUI.PropertyField(rect, prop);
  76. rect.y += rect.height + vSpace;
  77. } while (prop.NextVisible(prop.isExpanded));
  78. }
  79. }
  80. }