Utils.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using UnityEngine;
  2. #if UNITY_EDITOR
  3. using UnityEditor;
  4. #endif
  5. ///This file contains classes used to add custom attributes to fields that will
  6. ///cause them to be drawn differently in the Inspector, without the need for custom editors.
  7. /// <summary>
  8. /// Adds a [LabelOverride(string)] attribute that causes a public field drawn in the default
  9. /// Inspector to have a customized label, rather than Unity generating one from the name.
  10. /// </summary>
  11. public class LabelOverride : PropertyAttribute
  12. {
  13. /// <summary>
  14. /// String to override the default label with.
  15. /// </summary>
  16. public string label;
  17. /// <summary>
  18. /// Tooltip to add to the label, if set.
  19. /// </summary>
  20. public string optTooltip;
  21. /// <summary>
  22. /// Constructor. Called by the [LabelOverride(string)] tag with the params inside the parenthesis.
  23. /// </summary>
  24. /// <param name="label">String to override the default label with.</param>
  25. /// <param name="tooltip">Tooltip to add to the label, if set. </param>
  26. public LabelOverride(string label,string tooltip="")
  27. {
  28. this.label = label;
  29. this.optTooltip = tooltip;
  30. }
  31. #if UNITY_EDITOR
  32. /// <summary>
  33. /// Custom property drawer for fields with a [LabelOverride(string)] attribute.
  34. /// The label on the drawer will be set to the label value in the parameter instead of the default one.
  35. /// </summary>
  36. [CustomPropertyDrawer( typeof(LabelOverride) )]
  37. public class ThisPropertyDrawer : PropertyDrawer
  38. {
  39. public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
  40. {
  41. var propertyAttribute = this.attribute as LabelOverride;
  42. label.text = propertyAttribute.label;
  43. label.tooltip = propertyAttribute.optTooltip;
  44. EditorGUI.PropertyField( position , property , label );
  45. }
  46. }
  47. #endif
  48. }
  49. /// <summary>
  50. /// Adds a [ReadOnly(string)] attribute that will cause tagged fields to be drawn
  51. /// with ReadOnlyDrawer in the Inspector, preventing them from being edited.
  52. /// Used by ZEDManager to draw the status texts ("Version, Engine FPS, etc.")
  53. /// </summary>
  54. public class ReadOnlyAttribute : PropertyAttribute
  55. {
  56. /// <summary>
  57. /// String to override the default label with.
  58. /// </summary>
  59. public string label;
  60. /// <summary>
  61. /// Constructor. Called by the [ReadOnly(string)] tag with parameter in the parenthesis.
  62. /// </summary>
  63. /// <param name="label"></param>
  64. public ReadOnlyAttribute(string label)
  65. {
  66. this.label = label;
  67. }
  68. }
  69. #if UNITY_EDITOR
  70. /// <summary>
  71. /// Custom property drawer for fields with a [ReadOnly(string)] attribute
  72. /// that displays an uneditable text field in the Inspector.
  73. /// </summary>
  74. [CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
  75. public class ReadOnlyDrawer : PropertyDrawer
  76. {
  77. public override float GetPropertyHeight(SerializedProperty property,
  78. GUIContent label)
  79. {
  80. return EditorGUI.GetPropertyHeight(property, label, true);
  81. }
  82. public override void OnGUI(Rect position,
  83. SerializedProperty property,
  84. GUIContent label)
  85. {
  86. GUI.enabled = false;
  87. var propertyAttribute = this.attribute as ReadOnlyAttribute;
  88. label.text = propertyAttribute.label;
  89. EditorGUI.PropertyField(position, property, label, true);
  90. GUI.enabled = true;
  91. }
  92. }
  93. #endif