No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SearcherItem.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using JetBrains.Annotations;
  4. using UnityEngine;
  5. namespace UnityEditor.Searcher
  6. {
  7. [PublicAPI]
  8. [Serializable]
  9. public class SearcherItem
  10. {
  11. [SerializeField] int m_Id;
  12. [SerializeField] List<int> m_ChildrenIds;
  13. [SerializeField] string m_Name;
  14. [SerializeField] string m_Help;
  15. [SerializeField] string[] m_Synonyms;
  16. [SerializeField] Texture2D m_Icon;
  17. [SerializeField] bool m_CollapseEmptyIcon = true;
  18. public int Id => m_Id;
  19. public virtual string Name => m_Name;
  20. public string Path { get; private set; }
  21. public string Help
  22. {
  23. get => m_Help;
  24. set => m_Help = value;
  25. }
  26. public string[] Synonyms { get { return m_Synonyms; } set { m_Synonyms = value; } }
  27. public int Depth => Parent?.Depth + 1 ?? 0;
  28. public object UserData { get; set; }
  29. public Texture2D Icon { get => m_Icon; set { m_Icon = value; } }
  30. public bool CollapseEmptyIcon { get => m_CollapseEmptyIcon; set { m_CollapseEmptyIcon = value; } }
  31. public SearcherItem Parent { get; private set; }
  32. public SearcherDatabaseBase Database { get; private set; }
  33. // the backing field gets serialized otherwise and triggers a "Serialization depth limit 7 exceeded" warning
  34. [field:NonSerialized]
  35. public List<SearcherItem> Children { get; private set; }
  36. public bool HasChildren => Children.Count > 0;
  37. public SearcherItem(string name, string help = "", List<SearcherItem> children = null, object userData = null, Texture2D icon = null, bool collapseEmptyIcon = true)
  38. {
  39. m_Id = -1;
  40. Parent = null;
  41. Database = null;
  42. m_Name = name;
  43. m_Help = help;
  44. m_Icon = icon;
  45. UserData = userData;
  46. m_CollapseEmptyIcon = collapseEmptyIcon;
  47. Children = new List<SearcherItem>();
  48. if (children == null)
  49. return;
  50. Children = children;
  51. foreach (var child in children)
  52. child.OverwriteParent(this);
  53. }
  54. public void AddChild(SearcherItem child)
  55. {
  56. if (child == null)
  57. throw new ArgumentNullException(nameof(child));
  58. if (Database != null)
  59. throw new InvalidOperationException(
  60. "Cannot add more children to an item that was already used in a database.");
  61. if (Children == null)
  62. Children = new List<SearcherItem>();
  63. Children.Add(child);
  64. child.OverwriteParent(this);
  65. }
  66. internal void OverwriteId(int newId)
  67. {
  68. m_Id = newId;
  69. }
  70. void OverwriteParent(SearcherItem newParent)
  71. {
  72. Parent = newParent;
  73. }
  74. internal void OverwriteDatabase(SearcherDatabaseBase newDatabase)
  75. {
  76. Database = newDatabase;
  77. }
  78. internal void OverwriteChildrenIds(List<int> childrenIds)
  79. {
  80. m_ChildrenIds = childrenIds;
  81. }
  82. internal void GeneratePath()
  83. {
  84. if (Parent != null)
  85. Path = Parent.Path + " ";
  86. else
  87. Path = string.Empty;
  88. Path += Name;
  89. }
  90. internal void ReInitAfterLoadFromFile()
  91. {
  92. if (Children == null)
  93. Children = new List<SearcherItem>();
  94. foreach (var id in m_ChildrenIds)
  95. {
  96. var child = Database.ItemList[id];
  97. Children.Add(child);
  98. child.OverwriteParent(this);
  99. }
  100. GeneratePath();
  101. }
  102. public override string ToString()
  103. {
  104. return $"{nameof(Id)}: {Id}, {nameof(Name)}: {Name}, {nameof(Depth)}: {Depth}";
  105. }
  106. }
  107. }