Slideable Float Field in Custom Editor

Hey,

I am creating a custom editor and wanted to include a FloatField.
This works fine but I wanted the float field to change by clicking and sliding it, as most default unity components do. As shown in the image bellow.

12197-slider.png

I wasn’t abel to achieve this and would appriciate it if someone could point me in the right direction.

Thank you for your time

The FloatField does this by default. The internal function DoFloatField has a parameter for the dragHotZone. The rect of this zone is automatically calculated from the PrefixLabel size of the FloatField. When you don’t display a label the dragHotZone will be emtpy.

Unfortunately the DoFloatField function is an internal function so you can’t call it manually unless you use reflection.

Here is a version of the FloatFieldInternal method of EditorGUI which allows you to define the position of the dragHotZone.

private float MyFloatFieldInternal(Rect position, Rect dragHotZone, float value, [DefaultValue("EditorStyles.numberField")]GUIStyle style)
{
	int controlID = GUIUtility.GetControlID("EditorTextField".GetHashCode(), FocusType.Keyboard, position);
	Type editorGUIType = typeof(EditorGUI);

	Type RecycledTextEditorType = Assembly.GetAssembly(editorGUIType).GetType("UnityEditor.EditorGUI+RecycledTextEditor");
	Type[] argumentTypes = new Type[] { RecycledTextEditorType, typeof(Rect), typeof(Rect), typeof(int), typeof(float), typeof(string), typeof(GUIStyle), typeof(bool) };
	MethodInfo doFloatFieldMethod	= editorGUIType.GetMethod("DoFloatField", BindingFlags.NonPublic | BindingFlags.Static, null, argumentTypes, null);

	FieldInfo fieldInfo = editorGUIType.GetField("s_RecycledEditor", BindingFlags.NonPublic | BindingFlags.Static);
	object recycledEditor = fieldInfo.GetValue(null);

	object[] parameters = new object[] { recycledEditor, position, dragHotZone, controlID, value, "g7", style, true };

	return (float)doFloatFieldMethod.Invoke(null, parameters);
}

As suggested, don’t forget to cache the data, you don’t need to assign the values everytime. This is purely for example purpose.

For anyone wanting to do the same for int fields, you have to pass the drag sensitivity float as well. You could probably reflect EditorGUI.CalculateIntDragSensitivity() but you wouldn’t be able to cache it so I decided to just pass 1f which seems to work fine. Here’s the cached implementation (you could probably cache the params array as well and just update the indexes but *shrug*):

		private static MethodInfo sIntFieldMethod = null;
		private static object sRecycledEditor = null;

		private int MyIntFieldInternal (Rect displayRect, Rect dragHotZone, int value, GUIStyle style)
		{
			if (sIntFieldMethod == null || sRecycledEditor == null) {
				Type editorGUIType = typeof(EditorGUI);

				Type RecycledTextEditorType = Assembly.GetAssembly (editorGUIType).GetType ("UnityEditor.EditorGUI+RecycledTextEditor");
				Type[] argumentTypes = new Type[] {
					RecycledTextEditorType,
					typeof(Rect),
					typeof(Rect),
					typeof(int),
					typeof(int),
					typeof(string),
					typeof(GUIStyle),
					typeof(bool),
					typeof(float)
				};
				sIntFieldMethod = editorGUIType.GetMethod ("DoIntField", BindingFlags.NonPublic | BindingFlags.Static, null, argumentTypes, null);

				FieldInfo fieldInfo = editorGUIType.GetField ("s_RecycledEditor", BindingFlags.NonPublic | BindingFlags.Static);
				sRecycledEditor = fieldInfo.GetValue (null);
			}

			int controlID = GUIUtility.GetControlID ("EditorTextField".GetHashCode (), FocusType.Keyboard, displayRect);
			object[] fieldParams = new object[] { sRecycledEditor, displayRect, dragHotZone, controlID, value, "#######0", style, true, 1f };

			return (int)sIntFieldMethod.Invoke(null, fieldParams);
		}