using UnityEditor; using UnityEngine; using UnityEngine.UIElements; namespace Attributes.Editor { [CustomPropertyDrawer(typeof(RequireAttribute))] public class RequireAttributeDrawer : PropertyDrawer { private readonly string HELPBOX_CONTENT = "Property has to be set!"; public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { bool isNull = !property.objectReferenceValue; if (isNull) position.height = EditorGUIUtility.singleLineHeight; EditorGUI.PropertyField(position, property, label); if (isNull) { position.y += position.height + EditorGUIUtility.standardVerticalSpacing; position.height = EditorStyles.helpBox.CalcHeight(new GUIContent(HELPBOX_CONTENT), EditorGUIUtility.currentViewWidth) + 10.0f; EditorGUI.HelpBox(position, HELPBOX_CONTENT, MessageType.Error); } } public override float GetPropertyHeight(SerializedProperty property, GUIContent label) { if (property.objectReferenceValue) return EditorGUIUtility.singleLineHeight; return EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing + EditorStyles.helpBox.CalcHeight(new GUIContent(HELPBOX_CONTENT), EditorGUIUtility.currentViewWidth) + 10.0f; } } }