TreeNode.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #if UNITY_2019_4_OR_NEWER
  2. using System.IO;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5. using UnityEditor;
  6. using UnityEngine;
  7. using UnityEditor.UIElements;
  8. using UnityEngine.UIElements;
  9. namespace YooAsset.Editor
  10. {
  11. public class TreeNode
  12. {
  13. /// <summary>
  14. /// 子节点集合
  15. /// </summary>
  16. public List<TreeNode> Children = new List<TreeNode>(10);
  17. /// <summary>
  18. /// 父节点
  19. /// </summary>
  20. public TreeNode Parent { get; set; }
  21. /// <summary>
  22. /// 用户数据
  23. /// </summary>
  24. public object UserData { get; set; }
  25. /// <summary>
  26. /// 是否展开
  27. /// </summary>
  28. public bool IsExpanded { get; set; } = false;
  29. public TreeNode(object userData)
  30. {
  31. UserData = userData;
  32. }
  33. /// <summary>
  34. /// 添加子节点
  35. /// </summary>
  36. public void AddChild(TreeNode child)
  37. {
  38. child.Parent = this;
  39. Children.Add(child);
  40. }
  41. /// <summary>
  42. /// 清理所有子节点
  43. /// </summary>
  44. public void ClearChildren()
  45. {
  46. foreach(var child in Children)
  47. {
  48. child.Parent = null;
  49. }
  50. Children.Clear();
  51. }
  52. /// <summary>
  53. /// 计算节点的深度
  54. /// </summary>
  55. public int GetDepth()
  56. {
  57. int depth = 0;
  58. TreeNode current = this;
  59. while (current.Parent != null)
  60. {
  61. depth++;
  62. current = current.Parent;
  63. }
  64. return depth;
  65. }
  66. }
  67. }
  68. #endif