EditorCommon.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #if UNITY_EDITOR
  2. #if UNITY_2019_1_OR_NEWER
  3. #define UI_USE_FOLDOUT_HEADER_2019
  4. #endif
  5. using UnityEditor;
  6. using UnityEngine;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System;
  10. using System.Linq.Expressions;
  11. namespace VLB
  12. {
  13. public class EditorCommon : Editor
  14. {
  15. protected virtual void OnEnable()
  16. {
  17. FoldableHeader.OnEnable();
  18. }
  19. public override void OnInspectorGUI()
  20. {
  21. serializedObject.Update();
  22. #if UNITY_2019_3_OR_NEWER
  23. // no vertical space in 2019.3 looks better
  24. #else
  25. EditorGUILayout.Separator();
  26. #endif
  27. }
  28. public static void DrawLineSeparator()
  29. {
  30. DrawLineSeparator(Color.grey, 1, 10);
  31. }
  32. static void DrawLineSeparator(Color color, int thickness = 2, int padding = 10)
  33. {
  34. Rect r = EditorGUILayout.GetControlRect(GUILayout.Height(padding + thickness));
  35. r.x = 0;
  36. r.width = EditorGUIUtility.currentViewWidth;
  37. r.y += padding / 2;
  38. r.height = thickness;
  39. EditorGUI.DrawRect(r, color);
  40. }
  41. protected SerializedProperty FindProperty<T, TValue>(Expression<Func<T, TValue>> expr)
  42. {
  43. Debug.Assert(serializedObject != null);
  44. var prop = serializedObject.FindProperty(ReflectionUtils.GetFieldPath(expr));
  45. Debug.Assert(prop != null, string.Format("Failed to find SerializedProperty for expression '{0}'", expr));
  46. return prop;
  47. }
  48. protected void ButtonOpenConfig(bool miniButton = true)
  49. {
  50. bool buttonClicked = false;
  51. if (miniButton) buttonClicked = GUILayout.Button(EditorStrings.Common.ButtonOpenGlobalConfig, EditorStyles.miniButton);
  52. else buttonClicked = GUILayout.Button(EditorStrings.Common.ButtonOpenGlobalConfig);
  53. if (buttonClicked)
  54. Config.EditorSelectInstance();
  55. }
  56. protected bool GlobalToggle(ref bool boolean, GUIContent content, string saveString)
  57. {
  58. EditorGUI.BeginChangeCheck();
  59. boolean = EditorGUILayout.Toggle(content, boolean);
  60. if (EditorGUI.EndChangeCheck())
  61. {
  62. EditorPrefs.SetBool(saveString, boolean);
  63. SceneView.RepaintAll();
  64. return true;
  65. }
  66. return false;
  67. }
  68. }
  69. }
  70. #endif // UNITY_EDITOR