Guest User

Untitled

a guest
Dec 11th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6.  
  7. /// <summary>
  8. /// Base Component Class - Allows declaration of any component with a listener interface
  9. /// </summary>
  10. /// <typeparam name="TListenerInterface"></typeparam>
  11. public class ComponentBase<TListenerInterface> : MonoBehaviour
  12. {
  13. public ComponentBase(Scope broadcastLevel = Scope.Self, bool cacheListeners = true, bool lazyLoad = true)
  14. {
  15. this.cacheListeners = cacheListeners;
  16. if (cacheListeners && !lazyLoad) {
  17. var load = listeners;
  18. }
  19. switch (broadcastLevel)
  20. {
  21. case Scope.Self:
  22. getComponentInScope = GetComponents<TListenerInterface>;
  23. break;
  24. case Scope.SelfAndProgeny:
  25. getComponentInScope = GetComponentsInChildren<TListenerInterface>;
  26. break;
  27. case Scope.Children:
  28. getComponentInScope = GetComponentsInDirectChildren<TListenerInterface>;
  29. break;
  30. case Scope.SelfAndChildren:
  31. getComponentInScope = GetComponentsInSelfAndDirectChildren<TListenerInterface>;
  32. break;
  33. case Scope.SelfAndAncestors:
  34. getComponentInScope = GetComponentsInParent<TListenerInterface>;
  35. break;
  36. case Scope.Parent:
  37. getComponentInScope = GetComponentsInDirectParent<TListenerInterface>;
  38. break;
  39. case Scope.SelfAndParent:
  40. getComponentInScope = GetComponentsInSelfAndDirectParent<TListenerInterface>;
  41. break;
  42. case Scope.SelfProgenyAndAncestors:
  43. getComponentInScope = GetComponentsInSelfProgenyAndAncestors<TListenerInterface>;
  44. break;
  45. case Scope.Scene:
  46. getComponentInScope = GetComponentsInScene<TListenerInterface>;
  47. break;
  48. default: throw new ArgumentException("Unknown scope: components can not be resolved");
  49. }
  50. }
  51. private bool cacheListeners;
  52. private TListenerInterface[] _listenerCache;
  53. protected TListenerInterface[] listeners
  54. {
  55. get
  56. {
  57. if (cacheListeners)
  58. return _listenerCache == null
  59. ? _listenerCache = getComponentInScope()
  60. : _listenerCache;
  61. else
  62. return getComponentInScope();
  63. }
  64. }
  65. private Func<TListenerInterface[]> getComponentInScope;
  66.  
  67. public T[] GetComponentsInDirectChildren<T>()
  68. {
  69. return transform.Cast<Transform>().SelectMany(t => t.GetComponents<T>()).ToArray();
  70. }
  71. public T[] GetComponentsInSelfAndDirectChildren<T>()
  72. {
  73. return transform.Cast<Transform>().SelectMany(t => t.GetComponents<T>()).Concat(GetComponents<T>()).ToArray();
  74. }
  75. public T[] GetComponentsInDirectParent<T>()
  76. {
  77. return transform.parent.GetComponents<T>();
  78. }
  79. public T[] GetComponentsInSelfAndDirectParent<T>()
  80. {
  81. return transform.parent.GetComponents<T>().Concat(GetComponents<T>()).ToArray();
  82. }
  83. public T[] GetComponentsInScene<T>()
  84. {
  85. var roots = UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects();
  86. return roots.SelectMany(x => x.GetComponentsInChildren<T>()).ToArray();
  87. }
  88. public T[] GetComponentsInSelfProgenyAndAncestors<T>()
  89. {
  90. return GetComponentsInChildren<T>().Union(GetComponentsInParent<T>()).ToArray();
  91. }
  92. }
  93.  
  94. public enum Scope
  95. {
  96. Self,
  97. Children,
  98. SelfAndChildren,
  99. SelfAndProgeny,
  100. Parent,
  101. SelfAndParent,
  102. SelfAndAncestors,
  103. SelfProgenyAndAncestors,
  104. Scene
  105. }
Add Comment
Please, Sign In to add comment