Question by
PedroBitencourt · Dec 26, 2020 at 02:00 AM ·
editoreditor-scriptingpropertiespropertydrawer
Change the color of a variable field if it is empty
I recently saw this post on facebook and i did the same because it would be useful to me, but here it doesn't work...
His code:
Expeted result:
My code:
#region Libraries
using System;
using UnityEditor;
using UnityEngine;
#endregion
[CustomPropertyDrawer(typeof(WarnIfEmpty))]
public class FieldCantBeNull : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
WarnIfEmpty warn = attribute as WarnIfEmpty;
if (property.objectReferenceValue == null)
{
GUI.color = warn.color;
EditorGUI.PropertyField(position, property, label);
GUI.color = Color.white;
}
else
{
EditorGUI.PropertyField(position, property, label);
}
}
}
[AttributeUsage(AttributeTargets.Field)]
public class WarnIfEmpty : PropertyAttribute
{
public Color color = Color.red;
}
My unexpeted result:
What am I doing wrong?
Comment
Your answer
Follow this Question
Related Questions
Property Drawer ArgumentException 1 Answer
How to change the text of EditorGUILayout.TextField 2 Answers
How to create Generics Property Type Property Drawer 0 Answers
Pressing play button in editor makes changes to project (marking it with "*" for unsaved changes) 0 Answers
Setting GameObject properties in editor when public script fields are changed 2 Answers