- Home /
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.
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
Answer by Bunny83 · Jun 20, 2013 at 10:24 AM
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.
@$$anonymous$$aT. Bunny83 told you. You need to use reflection to use those internal stuff.
This was a bit huge.
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), typeof(float) };
$$anonymous$$ethodInfo doFloatField$$anonymous$$ethod = editorGUIType.Get$$anonymous$$ethod("DoFloatField", BindingFlags.NonPublic | BindingFlags.Static, null, argumentTypes, null)
Right, "DoFloatField" has two overloads with different parameters ^^. If you don't specify the parameter types it doesn't know which method you're looking for.
Another way would be to use "Get$$anonymous$$ethods" ins$$anonymous$$d to get an array of all methods that match the given binding flags. Then you just have to iterate through that array to find the method you want. Of course besides checking the methods name you would need to check it's parameters as well.
In the end it's probably not shorter / easier than what @$$anonymous$$ikilo did. At least you don't need the "RecycledTextEditor" type^^.
Of course to invoke that method you need to pass an instance of that class, but you can find one in "s_RecycledEditor" which is a static internal field of the EditorGUI class. That field is also used by "FloatField"
Answer by MaT227 · Sep 01, 2015 at 03:01 PM
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.
Obviously, if this utility is called on a window requiring mouse mouve, it will burn the CPU.
A little bit of cache would be necessary before using it.
Of course, assigning all those variables every time is a bad idea but this is for the example :) I'll add an edit to the answer.
Answer by KeithKong · Dec 11, 2017 at 01:01 AM
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);
}
This will calculate your drag sensitivity, where 'value' is the int value you are displaying. The formula comes from Unity's ParticleSystem source code. float dragSensitity = $$anonymous$$athf.$$anonymous$$ax(1, $$anonymous$$athf.Pow($$anonymous$$athf.Abs((float)value), 0.5f) * 0.03f);