IconFoldoutGroupAttributeDrawer.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Sirenix.OdinInspector.Editor;
  2. using Sirenix.OdinInspector.Editor.ValueResolvers;
  3. using Sirenix.Utilities.Editor;
  4. using UnityEngine;
  5. using UnityEditor;
  6. using KairoEngine.Core;
  7. namespace KairoEngine.Core.Editor
  8. {
  9. public class IconFoldoutGroupAttributeDrawer : OdinGroupDrawer<IconFoldoutGroupAttribute>
  10. {
  11. private LocalPersistentContext<bool> isExpanded;
  12. protected override void Initialize()
  13. {
  14. this.isExpanded = this.GetPersistentValue<bool>(
  15. "IconFoldoutGroupAttributeDrawer.isExpanded",
  16. GeneralDrawerConfig.Instance.ExpandFoldoutByDefault);
  17. }
  18. protected override void DrawPropertyLayout(GUIContent label)
  19. {
  20. SirenixEditorGUI.BeginBox();
  21. SirenixEditorGUI.BeginBoxHeader();
  22. string text = ValueResolver.GetForString(this.Property, this.Attribute.text, null).GetValue();
  23. Texture2D image = EditorGUIUtility.FindTexture(this.Attribute.iconPath);
  24. if(image == null)
  25. {
  26. image = new Texture2D(1, 1);
  27. var data = System.IO.File.ReadAllBytes(this.Attribute.iconPath);
  28. if(data != null) image.LoadImage(data);
  29. image.Apply();
  30. }
  31. label = new GUIContent(text);
  32. var styleCollapse = new GUIStyle(EditorStyles.foldout);
  33. Rect rect = EditorGUILayout.GetControlRect();
  34. GUI.DrawTexture(new Rect(rect.x, rect.y + 2, 14, 14), image);
  35. rect.x += 18;
  36. rect.width -= 18;
  37. this.isExpanded.Value = SirenixEditorGUI.Foldout(rect, this.isExpanded.Value, label, styleCollapse);
  38. SirenixEditorGUI.EndBoxHeader();
  39. if (SirenixEditorGUI.BeginFadeGroup(this, this.isExpanded.Value))
  40. {
  41. for (int i = 0; i < this.Property.Children.Count; i++)
  42. {
  43. this.Property.Children[i].Draw();
  44. }
  45. }
  46. SirenixEditorGUI.EndFadeGroup();
  47. SirenixEditorGUI.EndBox();
  48. }
  49. }
  50. }