Files
gpd-924-en/EditorCustomization/Assets/Scripts/Attributes/Editor/RequireAttributeDrawer.cs
T
david a6dbfb224f add scriptable object examples
add editor window example
add attribute + drawer example
2025-06-13 18:07:18 +02:00

35 lines
1.4 KiB
C#

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;
}
}
}