NestedComponentUtilities.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Photon.Pun
  5. {
  6. public static class NestedComponentUtilities
  7. {
  8. public static T EnsureRootComponentExists<T, NestedT>(this Transform transform)
  9. where T : Component
  10. where NestedT : Component
  11. {
  12. var root = GetParentComponent<NestedT>(transform);
  13. if (root)
  14. {
  15. var comp = root.GetComponent<T>();
  16. if (comp)
  17. return comp;
  18. return root.gameObject.AddComponent<T>();
  19. }
  20. return null;
  21. }
  22. #region GetComponent Replacements
  23. // Recycled collections
  24. private static Queue<Transform> nodesQueue = new Queue<Transform>();
  25. public static Dictionary<System.Type, ICollection> searchLists = new Dictionary<System.Type, ICollection>();
  26. private static Stack<Transform> nodeStack = new Stack<Transform>();
  27. /// <summary>
  28. /// Find T on supplied transform or any parent. Unlike GetComponentInParent, GameObjects do not need to be active to be found.
  29. /// </summary>
  30. public static T GetParentComponent<T>(this Transform t)
  31. where T : Component
  32. {
  33. T found = t.GetComponent<T>();
  34. if (found)
  35. return found;
  36. var par = t.parent;
  37. while (par)
  38. {
  39. found = par.GetComponent<T>();
  40. if (found)
  41. return found;
  42. par = par.parent;
  43. }
  44. return null;
  45. }
  46. /// <summary>
  47. /// Returns all T found between the child transform and its root. Order in List from child to parent, with the root/parent most being last.
  48. /// </summary>
  49. /// <param name="t"></param>
  50. /// <returns></returns>
  51. public static void GetNestedComponentsInParents<T>(this Transform t, List<T> list)
  52. where T : Component
  53. {
  54. list.Clear();
  55. while (t != null)
  56. {
  57. T obj = t.GetComponent<T>();
  58. if (obj)
  59. list.Add(obj);
  60. t = t.parent;
  61. }
  62. }
  63. public static T GetNestedComponentInChildren<T, NestedT>(this Transform t, bool includeInactive)
  64. where T : class
  65. where NestedT : class
  66. {
  67. // Look for the most obvious check first on the root.
  68. var found = t.GetComponent<T>();
  69. if (!ReferenceEquals(found, null))
  70. return found;
  71. // No root found, start testing layer by layer - root is the first layer. Add to queue.
  72. nodesQueue.Clear();
  73. nodesQueue.Enqueue(t);
  74. while (nodesQueue.Count > 0)
  75. {
  76. var node = nodesQueue.Dequeue();
  77. for (int c = 0, ccnt = node.childCount; c < ccnt; ++c)
  78. {
  79. var child = node.GetChild(c);
  80. // Ignore branches that are not active
  81. if (!includeInactive && !child.gameObject.activeSelf)
  82. continue;
  83. // Hit a nested node - don't search this node
  84. if (!ReferenceEquals(child.GetComponent<NestedT>(), null))
  85. continue;
  86. // see if what we are looking for is on this node
  87. found = child.GetComponent<T>();
  88. // Return if we found what we are looking for
  89. if (!ReferenceEquals(found, null))
  90. return found;
  91. // Add node to queue for next depth pass since nothing was found on this layer.
  92. nodesQueue.Enqueue(child);
  93. }
  94. }
  95. return found;
  96. }
  97. /// <summary>
  98. /// Same as GetComponentInParent, but will always include inactive objects in search.
  99. /// </summary>
  100. /// <typeparam name="T"></typeparam>
  101. /// <typeparam name="DontRecurseOnT"></typeparam>
  102. /// <param name="t"></param>
  103. /// <returns></returns>
  104. public static T GetNestedComponentInParent<T, NestedT>(this Transform t)
  105. where T : class
  106. where NestedT : class
  107. {
  108. T found = null;
  109. Transform node = t;
  110. do
  111. {
  112. found = node.GetComponent<T>();
  113. if (!ReferenceEquals(found, null))
  114. return found;
  115. // stop search on node with PV
  116. if (!ReferenceEquals(node.GetComponent<NestedT>(), null))
  117. return null;
  118. node = node.parent;
  119. }
  120. while (!ReferenceEquals(node, null));
  121. return null;
  122. }
  123. /// <summary>
  124. /// UNTESTED
  125. /// </summary>
  126. /// <typeparam name="T"></typeparam>
  127. /// <typeparam name="StopSearchOnT"></typeparam>
  128. /// <param name="t"></param>
  129. /// <returns></returns>
  130. public static T GetNestedComponentInParents<T, NestedT>(this Transform t)
  131. where T : class
  132. where NestedT : class
  133. {
  134. // First try root
  135. var found = t.GetComponent<T>();
  136. if (!ReferenceEquals(found, null))
  137. return found;
  138. /// Get the reverse list of transforms climbing for start up to netobject
  139. var par = t.parent;
  140. while (!ReferenceEquals(par, null))
  141. {
  142. found = par.GetComponent<T>();
  143. if (!ReferenceEquals(found, null))
  144. return found;
  145. /// Stop climbing at the NetObj (this is how we detect nesting
  146. if (!ReferenceEquals(par.GetComponent<NestedT>(), null))
  147. return null;
  148. par = par.parent;
  149. };
  150. return null;
  151. }
  152. /// <summary>
  153. /// Finds components of type T on supplied transform, and every parent above that node, inclusively stopping on node StopSearchOnT component.
  154. /// </summary>
  155. /// <typeparam name="T"></typeparam>
  156. /// <typeparam name="StopSearchOnT"></typeparam>
  157. /// <param name="t"></param>
  158. /// <param name="list"></param>
  159. /// <returns></returns>
  160. public static void GetNestedComponentsInParents<T, NestedT>(this Transform t, List<T> list)
  161. where T : class
  162. where NestedT : class
  163. {
  164. // Get components on the starting node - this is a given.
  165. t.GetComponents(list);
  166. // If the starting node has the stop component, we are done.
  167. if (!ReferenceEquals(t.GetComponent<NestedT>(), null))
  168. return;
  169. var tnode = t.parent;
  170. // If there is no parent, we are done.
  171. if (ReferenceEquals(tnode, null))
  172. return;
  173. nodeStack.Clear();
  174. while (true)
  175. {
  176. // add new parent to stack
  177. nodeStack.Push(tnode);
  178. // if this node has the Stop, we are done recursing up.
  179. if (!ReferenceEquals(tnode.GetComponent<NestedT>(), null))
  180. break;
  181. // Get the next parent node and add it to the stack
  182. tnode = tnode.parent;
  183. // Stop recursing up if the parent is null
  184. if (ReferenceEquals(tnode, null))
  185. break;
  186. }
  187. if (nodeStack.Count == 0)
  188. return;
  189. System.Type type = typeof(T);
  190. // Acquire the right searchlist from our pool
  191. List<T> searchList;
  192. if (!searchLists.ContainsKey(type))
  193. {
  194. searchList = new List<T>();
  195. searchLists.Add(type, searchList);
  196. }
  197. else
  198. {
  199. searchList = searchLists[type] as List<T>;
  200. }
  201. // Reverse iterate the nodes found. This produces a GetComponentInParent that starts from the parent Stop down to the provided transform
  202. while (nodeStack.Count > 0)
  203. {
  204. var node = nodeStack.Pop();
  205. node.GetComponents(searchList);
  206. list.AddRange(searchList);
  207. }
  208. }
  209. /// <summary>
  210. /// Same as GetComponentsInChildren, but will not recurse into children with component of the DontRecurseOnT type. This allows nesting of PhotonViews/NetObjects to be respected.
  211. /// </summary>
  212. /// <typeparam name="T"></typeparam>
  213. /// <param name="t"></param>
  214. /// <param name="list">Pass null and a reused list will be used. Consume immediately.</param>
  215. public static List<T> GetNestedComponentsInChildren<T, NestedT>(this Transform t, List<T> list, bool includeInactive = true)
  216. where T : class
  217. where NestedT : class
  218. {
  219. System.Type type = typeof(T);
  220. // Temp lists are also recycled. Get/Create a reusable List of this type.
  221. List<T> searchList;
  222. if (!searchLists.ContainsKey(type))
  223. searchLists.Add(type, searchList = new List<T>());
  224. else
  225. searchList = searchLists[type] as List<T>;
  226. nodesQueue.Clear();
  227. if (list == null)
  228. list = new List<T>();
  229. // Get components on starting transform - no exceptions
  230. t.GetComponents(list);
  231. // Add first layer of children to the queue for next layer processing.
  232. for (int i = 0, cnt = t.childCount; i < cnt; ++i)
  233. {
  234. var child = t.GetChild(i);
  235. // Ignore inactive nodes (optional)
  236. if (!includeInactive && !child.gameObject.activeSelf)
  237. continue;
  238. // ignore nested DontRecurseOnT
  239. if (!ReferenceEquals(child.GetComponent<NestedT>(), null))
  240. continue;
  241. nodesQueue.Enqueue(child);
  242. }
  243. // Recurse node layers
  244. while (nodesQueue.Count > 0)
  245. {
  246. var node = nodesQueue.Dequeue();
  247. // Add found components on this gameobject node
  248. node.GetComponents(searchList);
  249. list.AddRange(searchList);
  250. // Add children to the queue for next layer processing.
  251. for (int i = 0, cnt = node.childCount; i < cnt; ++i)
  252. {
  253. var child = node.GetChild(i);
  254. // Ignore inactive nodes (optional)
  255. if (!includeInactive && !child.gameObject.activeSelf)
  256. continue;
  257. // ignore nested NestedT
  258. if (!ReferenceEquals(child.GetComponent<NestedT>(), null))
  259. continue;
  260. nodesQueue.Enqueue(child);
  261. }
  262. }
  263. return list;
  264. }
  265. /// <summary>
  266. /// Same as GetComponentsInChildren, but will not recurse into children with component of the DontRecurseOnT type. This allows nesting of PhotonViews/NetObjects to be respected.
  267. /// </summary>
  268. /// <typeparam name="T"></typeparam>
  269. /// <param name="t"></param>
  270. /// <param name="list">Pass null and a reused list will be used. Consume immediately.</param>
  271. public static List<T> GetNestedComponentsInChildren<T>(this Transform t, List<T> list, bool includeInactive = true, params System.Type[] stopOn)
  272. where T : class
  273. {
  274. System.Type type = typeof(T);
  275. // Temp lists are also recycled. Get/Create a reusable List of this type.
  276. List<T> searchList;
  277. if (!searchLists.ContainsKey(type))
  278. searchLists.Add(type, searchList = new List<T>());
  279. else
  280. searchList = searchLists[type] as List<T>;
  281. nodesQueue.Clear();
  282. // Get components on starting transform - no exceptions
  283. t.GetComponents(list);
  284. // Add first layer of children to the queue for next layer processing.
  285. for (int i = 0, cnt = t.childCount; i < cnt; ++i)
  286. {
  287. var child = t.GetChild(i);
  288. // Ignore inactive nodes (optional)
  289. if (!includeInactive && !child.gameObject.activeSelf)
  290. continue;
  291. // ignore nested DontRecurseOnT
  292. bool stopRecurse = false;
  293. for (int s = 0, scnt = stopOn.Length; s < scnt; ++s)
  294. {
  295. if (!ReferenceEquals(child.GetComponent(stopOn[s]), null))
  296. {
  297. stopRecurse = true;
  298. break;
  299. }
  300. }
  301. if (stopRecurse)
  302. continue;
  303. nodesQueue.Enqueue(child);
  304. }
  305. // Recurse node layers
  306. while (nodesQueue.Count > 0)
  307. {
  308. var node = nodesQueue.Dequeue();
  309. // Add found components on this gameobject node
  310. node.GetComponents(searchList);
  311. list.AddRange(searchList);
  312. // Add children to the queue for next layer processing.
  313. for (int i = 0, cnt = node.childCount; i < cnt; ++i)
  314. {
  315. var child = node.GetChild(i);
  316. // Ignore inactive nodes (optional)
  317. if (!includeInactive && !child.gameObject.activeSelf)
  318. continue;
  319. // ignore nested NestedT
  320. bool stopRecurse = false;
  321. for (int s = 0, scnt = stopOn.Length; s < scnt; ++s)
  322. {
  323. if (!ReferenceEquals(child.GetComponent(stopOn[s]), null))
  324. {
  325. stopRecurse = true;
  326. break;
  327. }
  328. }
  329. if (stopRecurse)
  330. continue;
  331. nodesQueue.Enqueue(child);
  332. }
  333. }
  334. return list;
  335. }
  336. /// <summary>
  337. /// Same as GetComponentsInChildren, but will not recurse into children with component of the NestedT type. This allows nesting of PhotonViews/NetObjects to be respected.
  338. /// </summary>
  339. /// <typeparam name="T">Cast found components to this type. Typically Component, but any other class/interface will work as long as they are assignable from SearchT.</typeparam>
  340. /// <typeparam name="SearchT">Find components of this class or interface type.</typeparam>
  341. /// <typeparam name="DontRecurseOnT"></typeparam>
  342. /// <param name="t"></param>
  343. /// <param name="includeInactive"></param>
  344. /// <param name="list"></param>
  345. /// <returns></returns>
  346. public static void GetNestedComponentsInChildren<T, SearchT, NestedT>(this Transform t, bool includeInactive, List<T> list)
  347. where T : class
  348. where SearchT : class
  349. {
  350. list.Clear();
  351. // If this is inactive, nothing will be found. Give up now if we are restricted to active.
  352. if (!includeInactive && !t.gameObject.activeSelf)
  353. return;
  354. System.Type searchType = typeof(SearchT);
  355. // Temp lists are also recycled. Get/Create a reusable List of this type.
  356. List<SearchT> searchList;
  357. if (!searchLists.ContainsKey(searchType))
  358. searchLists.Add(searchType, searchList = new List<SearchT>());
  359. else
  360. searchList = searchLists[searchType] as List<SearchT>;
  361. // Recurse child nodes one layer at a time. Using a Queue allows this to happen without a lot of work.
  362. nodesQueue.Clear();
  363. nodesQueue.Enqueue(t);
  364. while (nodesQueue.Count > 0)
  365. {
  366. var node = nodesQueue.Dequeue();
  367. // Add found components on this gameobject node
  368. searchList.Clear();
  369. node.GetComponents(searchList);
  370. foreach (var comp in searchList)
  371. {
  372. var casted = comp as T;
  373. if (!ReferenceEquals(casted, null))
  374. list.Add(casted);
  375. }
  376. // Add children to the queue for next layer processing.
  377. for (int i = 0, cnt = node.childCount; i < cnt; ++i)
  378. {
  379. var child = node.GetChild(i);
  380. // Ignore inactive nodes (optional)
  381. if (!includeInactive && !child.gameObject.activeSelf)
  382. continue;
  383. // ignore nested DontRecurseOnT
  384. if (!ReferenceEquals(child.GetComponent<NestedT>(), null))
  385. continue;
  386. nodesQueue.Enqueue(child);
  387. }
  388. }
  389. }
  390. #endregion
  391. }
  392. }