HighlightNullAttribute.cs 914 B

123456789101112131415161718192021222324252627282930313233
  1. using UnityEngine;
  2. #if UNITY_EDITOR
  3. using UnityEditor;
  4. #endif
  5. namespace VLB
  6. {
  7. /// <summary>
  8. /// Highlight in red in inspector in not set
  9. /// </summary>
  10. public sealed class HighlightNullAttribute : PropertyAttribute { }
  11. #if UNITY_EDITOR
  12. [CustomPropertyDrawer(typeof(HighlightNullAttribute))]
  13. public class HighlightNullDrawer : PropertyDrawer
  14. {
  15. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  16. {
  17. if (property.propertyType != SerializedPropertyType.ObjectReference)
  18. {
  19. EditorGUI.LabelField(position, label.text, "Only valid for object references");
  20. return;
  21. }
  22. if (property.objectReferenceValue == null)
  23. EditorGUI.DrawRect(position, Color.red);
  24. EditorGUI.ObjectField(position, property, label);
  25. }
  26. }
  27. #endif
  28. }