Skip to content

Instantly share code, notes, and snippets.

@petersvp
Created January 18, 2023 07:35
Show Gist options
  • Select an option

  • Save petersvp/251f9128b5102a8b8a102337725ae220 to your computer and use it in GitHub Desktop.

Select an option

Save petersvp/251f9128b5102a8b8a102337725ae220 to your computer and use it in GitHub Desktop.

Revisions

  1. petersvp created this gist Jan 18, 2023.
    24 changes: 24 additions & 0 deletions NaNFieldProperty.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    #if UNITY_EDITOR
    /// <summary>
    /// Float field with NAN button so you can use optional floats in your Unity components
    /// </summary>
    public sealed class NaNField : PropertyAttribute {}

    [CustomPropertyDrawer(typeof(NaNField))]
    public sealed class NaNFloatField : PropertyDrawer
    {
    const int buttonWidth = 40;
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
    var fieldpos = position;
    fieldpos.width -= buttonWidth;
    float f = property.floatValue;
    f = EditorGUI.FloatField(fieldpos, label, property.floatValue);
    property.floatValue = f;
    var buttonpos = position;
    buttonpos.x = position.width - buttonWidth + 18;
    buttonpos.width = buttonWidth;
    if (GUI.Button(buttonpos, "NaN")) property.floatValue = float.NaN;
    }
    }
    #endif