Created
January 18, 2023 07:35
-
-
Save petersvp/251f9128b5102a8b8a102337725ae220 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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