ShowIfDrawer.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace HQFPSWeapons
  4. {
  5. [CustomPropertyDrawer(typeof(ShowIf))]
  6. public class ShowIfDrawer : PropertyDrawer
  7. {
  8. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  9. {
  10. if(CanShow(property))
  11. {
  12. var indentedRect = position;
  13. float indentation = ((ShowIf)attribute).m_Indentation;
  14. position.x += indentation;
  15. position.width -= indentation;
  16. EditorGUI.PropertyField(position, property, label, true);
  17. }
  18. }
  19. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  20. {
  21. if (!CanShow(property))
  22. // HACK. if we don't use this, extra spacing height will be added by Unity.
  23. return -EditorGUIUtility.standardVerticalSpacing;
  24. else
  25. return EditorGUI.GetPropertyHeight(property, true);
  26. }
  27. private bool CanShow(SerializedProperty property)
  28. {
  29. var attr = (ShowIf)attribute;
  30. string parentPath = string.Empty;
  31. parentPath = property.GetParentPath();
  32. SerializedProperty conditionProperty = property.serializedObject.FindProperty(parentPath + (parentPath != string.Empty ? "." : "") + attr.m_PropertyName);
  33. if(conditionProperty != null)
  34. {
  35. if(conditionProperty.propertyType == SerializedPropertyType.Boolean)
  36. return attr.m_RequiredBool == conditionProperty.boolValue;
  37. else if(conditionProperty.propertyType == SerializedPropertyType.ObjectReference)
  38. return attr.m_RequiredBool == (conditionProperty.objectReferenceValue != null);
  39. else if(conditionProperty.propertyType == SerializedPropertyType.Integer)
  40. return attr.m_RequiredInt == conditionProperty.intValue;
  41. else if(conditionProperty.propertyType == SerializedPropertyType.Enum)
  42. return attr.m_RequiredInt == conditionProperty.intValue;
  43. else if(conditionProperty.propertyType == SerializedPropertyType.Float)
  44. return attr.m_RequiredFloat == conditionProperty.floatValue;
  45. else if(conditionProperty.propertyType == SerializedPropertyType.String)
  46. return attr.m_RequiredString == conditionProperty.stringValue;
  47. else if(conditionProperty.propertyType == SerializedPropertyType.Vector3)
  48. return attr.m_RequiredVector3 == conditionProperty.vector3Value;
  49. }
  50. return false;
  51. }
  52. }
  53. }