Skip to content

Instantly share code, notes, and snippets.

@petersvp
Created January 18, 2023 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petersvp/251f9128b5102a8b8a102337725ae220 to your computer and use it in GitHub Desktop.
Save petersvp/251f9128b5102a8b8a102337725ae220 to your computer and use it in GitHub Desktop.
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment