AddressableAssetFold.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace XGame.Editor.Asset.Addressable
  6. {
  7. /// <summary>
  8. /// 文件夹
  9. /// </summary>
  10. public class AddressableAssetFold : AddressableAssetEntry
  11. {
  12. private readonly List<AddressableAssetEntry> childs;
  13. //private bool isFoldout;
  14. private bool isSort;
  15. public override EntryType EnType => EntryType.Fold;
  16. private event Action addChildrenEvent;
  17. public event Action AddChildrenEvent
  18. {
  19. add { addChildrenEvent += value; }
  20. remove { addChildrenEvent -= value; }
  21. }
  22. public AddressableAssetFold(string path) : base(path)
  23. {
  24. childs = new List<AddressableAssetEntry>();
  25. isSort = false;
  26. }
  27. public void AddChild(AddressableAssetEntry entry)
  28. {
  29. if (entry.Parent != null && entry.Parent.Equals(this))
  30. {
  31. Debug.LogError($"Add Child Repeat. Parent:{Path} Child:{entry.Path}");
  32. return;
  33. }
  34. childs.Add(entry);
  35. entry.Parent = this;
  36. isSort = true;
  37. }
  38. public void RemoveChild(AddressableAssetEntry entry)
  39. {
  40. if (childs.Contains(entry))
  41. {
  42. childs.Remove(entry);
  43. }
  44. entry.Parent = null;
  45. }
  46. public AddressableAssetEntry GetChild(string path)
  47. {
  48. return childs.Find((entry => entry.Path.Equals(path)));
  49. }
  50. public bool Contains(string path)
  51. {
  52. return childs.Find((entry => entry.Path.Equals(path))) != null;
  53. }
  54. /// <summary>
  55. /// 节点数量
  56. /// </summary>
  57. public int ChildCount => childs.Count;
  58. protected override void OnSetParent()
  59. {
  60. }
  61. protected override void OnGUI()
  62. {
  63. EditorGUILayout.BeginVertical();
  64. IsFoldout = EditorGUILayout.Foldout(IsFoldout, ShowName);
  65. if (IsFoldout)
  66. {
  67. if (addChildrenEvent != null)
  68. {
  69. addChildrenEvent.Invoke();
  70. addChildrenEvent = null;
  71. }
  72. if (isSort)
  73. {
  74. isSort = false;
  75. childs.Sort();
  76. }
  77. for (int i = 0; i < childs.Count;)
  78. {
  79. var child = childs[i];
  80. child.GUI();
  81. if (child.Parent != null)
  82. {
  83. i++;
  84. }
  85. }
  86. }
  87. EditorGUILayout.EndVertical();
  88. }
  89. protected override void OnClear()
  90. {
  91. foreach (var child in childs)
  92. {
  93. child.Clear();
  94. }
  95. childs.Clear();
  96. addChildrenEvent = null;
  97. isSort = false;
  98. }
  99. }
  100. }