FoldableHeader.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.Generic;
  8. using System;
  9. namespace VLB
  10. {
  11. public static class FoldableHeader
  12. {
  13. public static void OnEnable()
  14. {
  15. ms_CurrentFoldableHeader = null;
  16. }
  17. public static bool Begin(UnityEngine.Object self, string label)
  18. {
  19. return Begin(self, new GUIContent(label));
  20. }
  21. public static bool Begin(UnityEngine.Object self, GUIContent label)
  22. {
  23. var uniqueString = label.text;
  24. if(self) uniqueString = self.ToString() + uniqueString;
  25. if (ms_StyleHeaderFoldable == null)
  26. {
  27. ms_StyleHeaderFoldable = new GUIStyle(EditorStyles.foldout);
  28. ms_StyleHeaderFoldable.fontStyle = FontStyle.Bold;
  29. }
  30. ms_CurrentFoldableHeader = uniqueString;
  31. bool folded = ms_FoldedHeaders.Contains(uniqueString);
  32. #if UI_USE_FOLDOUT_HEADER_2019
  33. folded = !EditorGUILayout.BeginFoldoutHeaderGroup(!folded, label);
  34. #elif UNITY_5_5_OR_NEWER
  35. folded = !EditorGUILayout.Foldout(!folded, label, toggleOnLabelClick: true, style: ms_StyleHeaderFoldable);
  36. #else
  37. folded = !EditorGUILayout.Foldout(!folded, label, ms_StyleHeaderFoldable);
  38. #endif
  39. if (folded) ms_FoldedHeaders.Add(uniqueString);
  40. else ms_FoldedHeaders.Remove(uniqueString);
  41. return !folded;
  42. }
  43. public static void End()
  44. {
  45. Debug.Assert(ms_CurrentFoldableHeader != null, "Trying to call FoldableHeader.End() but there is no header opened");
  46. ms_CurrentFoldableHeader = null;
  47. #if UI_USE_FOLDOUT_HEADER_2019
  48. EditorGUILayout.EndFoldoutHeaderGroup();
  49. #if UNITY_2019_3_OR_NEWER
  50. EditorGUILayout.Separator();
  51. #endif
  52. #else
  53. EditorCommon.DrawLineSeparator();
  54. #endif
  55. }
  56. static string ms_CurrentFoldableHeader = null;
  57. static HashSet<String> ms_FoldedHeaders = new HashSet<String>();
  58. static GUIStyle ms_StyleHeaderFoldable = null;
  59. }
  60. }
  61. #endif // UNITY_EDITOR