Skip to content

Instantly share code, notes, and snippets.

@keenanwoodall
Last active March 13, 2019 23:59
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 keenanwoodall/d198e8181706905e72f06b9ab2227278 to your computer and use it in GitHub Desktop.
Save keenanwoodall/d198e8181706905e72f06b9ab2227278 to your computer and use it in GitHub Desktop.
A helper class that lets you draw a float field with more control. You can define a rect where the value is drawn/set and another rect where you can drag to scroll the value.
using System;
using System.Reflection;
using UnityEngine;
using UnityEditor;
public static class CustomFloatField
{
private static readonly int Hint = "EditorTextField".GetHashCode ();
private static readonly Type EditorGUIType = typeof (EditorGUI);
private static readonly Type RecycledTextEditorType = Assembly.GetAssembly (EditorGUIType).GetType ("UnityEditor.EditorGUI+RecycledTextEditor");
private static readonly Type[] ArgumentTypes =
{
RecycledTextEditorType,
typeof (Rect),
typeof (Rect),
typeof (int),
typeof (float),
typeof (string),
typeof (GUIStyle),
typeof (bool)
};
private static readonly MethodInfo DoFloatFieldMethodInfo = EditorGUIType.GetMethod ("DoFloatField", BindingFlags.NonPublic | BindingFlags.Static, null, ArgumentTypes, null);
private static readonly FieldInfo FieldInfo = EditorGUIType.GetField ("s_RecycledEditor", BindingFlags.NonPublic | BindingFlags.Static);
private static readonly object RecycledEditor = FieldInfo.GetValue (null);
public static float Draw (Rect draw, Rect drag, float value, GUIStyle style)
{
var controlID = GUIUtility.GetControlID (Hint, FocusType.Keyboard, draw);
var parameters = new object[] { RecycledEditor, draw, drag, controlID, value, "g7", style, true };
return (float)DoFloatFieldMethodInfo.Invoke (null, parameters);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment