Files

35 lines
1.2 KiB
C#

using UnityEditor;
using UnityEngine;
namespace Attributes.Editor
{
[CustomPropertyDrawer(typeof(RequiredAttribute))]
public class RequiredAttributeDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
bool isNull = property.objectReferenceValue == null;
if (isNull) position.height = EditorGUIUtility.singleLineHeight;
// draw property itself
EditorGUI.PropertyField(position, property, label);
// draw error if property is null
if (!isNull) return;
position.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
position.height += 10.0f;
EditorGUI.HelpBox(position, "Property has to be set!", MessageType.Error);
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (property.objectReferenceValue == null)
{
return EditorGUIUtility.singleLineHeight * 2 + EditorGUIUtility.standardVerticalSpacing + 10.0f;
}
return EditorGUIUtility.singleLineHeight;
}
}
}