IconFoldoutGroupAttributeDrawer.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. string path = ValueResolver.GetForString(this.Property, this.Attribute.iconPath, null).GetValue();
  24. Texture2D image = EditorGUIUtility.FindTexture(path);
  25. if(image == null)
  26. {
  27. image = new Texture2D(1, 1);
  28. var data = System.IO.File.ReadAllBytes(path);
  29. if(data != null) image.LoadImage(data);
  30. image.Apply();
  31. }
  32. label = new GUIContent(text);
  33. var styleCollapse = new GUIStyle(EditorStyles.foldout);
  34. Rect rect = EditorGUILayout.GetControlRect();
  35. GUI.DrawTexture(new Rect(rect.x, rect.y + 2, 14, 14), image);
  36. rect.x += 18;
  37. rect.width -= 18;
  38. this.isExpanded.Value = SirenixEditorGUI.Foldout(rect, this.isExpanded.Value, label, styleCollapse);
  39. SirenixEditorGUI.EndBoxHeader();
  40. if (SirenixEditorGUI.BeginFadeGroup(this, this.isExpanded.Value))
  41. {
  42. for (int i = 0; i < this.Property.Children.Count; i++)
  43. {
  44. this.Property.Children[i].Draw();
  45. }
  46. }
  47. SirenixEditorGUI.EndFadeGroup();
  48. SirenixEditorGUI.EndBox();
  49. }
  50. }
  51. }