EditorGUILayoutExtended.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Reflection;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace MPUIKIT.Editor
  6. {
  7. public class EditorGUILayoutExtended : UnityEditor.Editor
  8. {
  9. private static readonly Type editorGUIType = typeof(EditorGUI);
  10. private static readonly Type RecycledTextEditorType =
  11. Assembly.GetAssembly(editorGUIType).GetType("UnityEditor.EditorGUI+RecycledTextEditor");
  12. private static readonly Type[] argumentTypes =
  13. {
  14. RecycledTextEditorType, typeof(Rect), typeof(Rect), typeof(int), typeof(float), typeof(string),
  15. typeof(GUIStyle), typeof(bool)
  16. };
  17. private static readonly MethodInfo doFloatFieldMethod = editorGUIType.GetMethod("DoFloatField",
  18. BindingFlags.NonPublic | BindingFlags.Static, null, argumentTypes, null);
  19. private static readonly FieldInfo fieldInfo =
  20. editorGUIType.GetField("s_RecycledEditor", BindingFlags.NonPublic | BindingFlags.Static);
  21. private static readonly object recycledEditor = fieldInfo.GetValue(null);
  22. private static readonly GUIStyle style = EditorStyles.numberField;
  23. public static float FloatFieldExtended(Rect _position, float _value, Rect _dragHotZone)
  24. {
  25. int controlId = GUIUtility.GetControlID("EditorTextField".GetHashCode(), FocusType.Keyboard, _position);
  26. object[] parameters = {recycledEditor, _position, _dragHotZone, controlId, _value, "g7", style, true};
  27. return (float) doFloatFieldMethod.Invoke(null, parameters);
  28. }
  29. // public static float FloatField(GUIContent _content, float _value, float _inputBoxWidth, params GUILayoutOption[] _options)
  30. // {
  31. // Rect totalRect = EditorGUILayout.GetControlRect(_options);
  32. // float width;
  33. // if (_inputBoxWidth < 1) width = totalRect.width * Mathf.Clamp(_inputBoxWidth, 0.2f, 0.8f);
  34. // else width = Mathf.Clamp(_inputBoxWidth, totalRect.width * 0.2f, totalRect.width * 0.8f);
  35. // Rect labelRect = new Rect(totalRect.x, totalRect.y, totalRect.width - width - 8, totalRect.height);
  36. // Rect inputRect = new Rect(totalRect.x + totalRect.width - width, totalRect.y, width, totalRect.height);
  37. //
  38. // EditorGUI.LabelField(labelRect, _content);
  39. // return FloatFieldExtended(inputRect, _value, labelRect);
  40. // }
  41. public static float FloatField(GUIContent _content, float _value, float _labelwidth,
  42. params GUILayoutOption[] _options)
  43. {
  44. Rect totalRect = EditorGUILayout.GetControlRect(_options);
  45. // float width;
  46. // if (_labelwidth < 1) width = totalRect.width * Mathf.Clamp(_labelwidth, 0.2f, 0.8f);
  47. // else width = Mathf.Clamp(_labelwidth, totalRect.width * 0.2f, totalRect.width * 0.8f);
  48. Rect labelRect = new Rect(totalRect.x, totalRect.y, _labelwidth, totalRect.height);
  49. Rect inputRect = new Rect(totalRect.x + _labelwidth, totalRect.y, totalRect.width - _labelwidth,
  50. totalRect.height);
  51. // Rect labelRect = new Rect(totalRect.x, totalRect.y, totalRect.width - width - 8, totalRect.height);
  52. // Rect inputRect = new Rect(totalRect.x + totalRect.width - width, totalRect.y, width, totalRect.height);
  53. EditorGUI.LabelField(labelRect, _content);
  54. return FloatFieldExtended(inputRect, _value, labelRect);
  55. }
  56. }
  57. }