Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- /// <summary>
- /// Base Component Class - Allows declaration of any component with a listener interface
- /// </summary>
- /// <typeparam name="TListenerInterface"></typeparam>
- public class ComponentBase<TListenerInterface> : MonoBehaviour
- {
- public ComponentBase(Scope broadcastLevel = Scope.Self, bool cacheListeners = true, bool lazyLoad = true)
- {
- this.cacheListeners = cacheListeners;
- if (cacheListeners && !lazyLoad) {
- var load = listeners;
- }
- switch (broadcastLevel)
- {
- case Scope.Self:
- getComponentInScope = GetComponents<TListenerInterface>;
- break;
- case Scope.SelfAndProgeny:
- getComponentInScope = GetComponentsInChildren<TListenerInterface>;
- break;
- case Scope.Children:
- getComponentInScope = GetComponentsInDirectChildren<TListenerInterface>;
- break;
- case Scope.SelfAndChildren:
- getComponentInScope = GetComponentsInSelfAndDirectChildren<TListenerInterface>;
- break;
- case Scope.SelfAndAncestors:
- getComponentInScope = GetComponentsInParent<TListenerInterface>;
- break;
- case Scope.Parent:
- getComponentInScope = GetComponentsInDirectParent<TListenerInterface>;
- break;
- case Scope.SelfAndParent:
- getComponentInScope = GetComponentsInSelfAndDirectParent<TListenerInterface>;
- break;
- case Scope.SelfProgenyAndAncestors:
- getComponentInScope = GetComponentsInSelfProgenyAndAncestors<TListenerInterface>;
- break;
- case Scope.Scene:
- getComponentInScope = GetComponentsInScene<TListenerInterface>;
- break;
- default: throw new ArgumentException("Unknown scope: components can not be resolved");
- }
- }
- private bool cacheListeners;
- private TListenerInterface[] _listenerCache;
- protected TListenerInterface[] listeners
- {
- get
- {
- if (cacheListeners)
- return _listenerCache == null
- ? _listenerCache = getComponentInScope()
- : _listenerCache;
- else
- return getComponentInScope();
- }
- }
- private Func<TListenerInterface[]> getComponentInScope;
- public T[] GetComponentsInDirectChildren<T>()
- {
- return transform.Cast<Transform>().SelectMany(t => t.GetComponents<T>()).ToArray();
- }
- public T[] GetComponentsInSelfAndDirectChildren<T>()
- {
- return transform.Cast<Transform>().SelectMany(t => t.GetComponents<T>()).Concat(GetComponents<T>()).ToArray();
- }
- public T[] GetComponentsInDirectParent<T>()
- {
- return transform.parent.GetComponents<T>();
- }
- public T[] GetComponentsInSelfAndDirectParent<T>()
- {
- return transform.parent.GetComponents<T>().Concat(GetComponents<T>()).ToArray();
- }
- public T[] GetComponentsInScene<T>()
- {
- var roots = UnityEngine.SceneManagement.SceneManager.GetActiveScene().GetRootGameObjects();
- return roots.SelectMany(x => x.GetComponentsInChildren<T>()).ToArray();
- }
- public T[] GetComponentsInSelfProgenyAndAncestors<T>()
- {
- return GetComponentsInChildren<T>().Union(GetComponentsInParent<T>()).ToArray();
- }
- }
- public enum Scope
- {
- Self,
- Children,
- SelfAndChildren,
- SelfAndProgeny,
- Parent,
- SelfAndParent,
- SelfAndAncestors,
- SelfProgenyAndAncestors,
- Scene
- }
Add Comment
Please, Sign In to add comment