Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class MonoBehaviourWrapper : MonoBehaviour {
- #region Fields
- static Dictionary<GameObject, Dictionary<Type, Component>> components;
- public bool debug;
- bool thisComponentWasDestroyed;
- /// Override this property to change whether or not this component and its game object should be cached on awake (default is true).
- protected virtual bool CacheOnAwake { get { return true; } }
- #endregion
- #region Unity Magic
- protected virtual void Awake() {
- if (components == null) { components = new Dictionary<GameObject, Dictionary<Type, Component>>(); }
- if (CacheOnAwake) { Cache(this.GetType(), this); }
- }
- protected virtual void OnDestroy() {
- if (thisComponentWasDestroyed) {
- Component[] wrappers = GetComponents(typeof(MonoBehaviourWrapper));
- if ( !(wrappers.Length == 1 && wrappers[0] == this) ) { return; }
- }
- components.Remove(gameObject);
- }
- #endregion
- #region Methods
- public void Cache<T>(Component component) where T : Component { Cache(typeof(T), component); }
- public void Cache(Type type, Component component) {
- Dictionary<Type, Component> cache;
- if (components.TryGetValue(gameObject, out cache)) {
- if (cache.ContainsKey(type))
- cache[type] = component;
- else
- cache.Add(type, component);
- }
- else {
- components.Add(gameObject, new Dictionary<Type, Component>() { { type, component } });
- }
- }
- public T Add<T>() where T : Component { return (T)Add(typeof(T)); }
- public Component Add(Type type) {
- Component component = gameObject.AddComponent(type);
- Dictionary<Type, Component> cache;
- if (components.TryGetValue(gameObject, out cache))
- cache.Add(type, component);
- else
- components.Add(gameObject, new Dictionary<Type, Component>() { { type, component } });
- return component;
- }
- public bool Remove<T>() where T : Component { return Remove(typeof(T)); }
- public bool Remove(Type type) {
- Component component = null;
- Dictionary<Type, Component> cache;
- if (components.TryGetValue(gameObject, out cache)) {
- if (cache.TryGetValue(type, out component))
- cache.Remove(type);
- }
- else {
- components.Add(gameObject, new Dictionary<Type, Component>());
- }
- if (component == null) { component = GetComponent(type); }
- if (component != null) {
- thisComponentWasDestroyed = component == this;
- Destroy(component);
- return true;
- }
- else {
- return false;
- }
- }
- /// Uses GetComponent to find the component, and then caches the result. Subsequent
- /// calls will return the cached version. Returns null if the component does not exist.
- public T Get<T>() where T : Component { return (T)Get(typeof(T)); }
- public Component Get(Type type) {
- Component component;
- Dictionary<Type, Component> cache;
- if (components.TryGetValue(gameObject, out cache)) {
- if (cache.TryGetValue(type, out component)) {
- if (component == null) { cache.Remove(type); }
- return component;
- }
- }
- else {
- cache = new Dictionary<Type, Component>();
- components.Add(gameObject, cache);
- }
- component = GetComponent(type);
- if (component != null)
- cache.Add(type, component);
- return component;
- }
- /// Uses GetComponent to check whether or not this GameObject has a particular component,
- /// and caches any non-null result in the process.
- public bool Has<T>() where T : Component { return Has(typeof(T)); }
- public bool Has(Type type) {
- Component component;
- Dictionary<Type, Component> cache;
- if (components.TryGetValue(gameObject, out cache)) {
- if (cache.TryGetValue(type, out component)) {
- if (component == null) {
- cache.Remove(type);
- return false;
- }
- return true;
- }
- }
- else {
- cache = new Dictionary<Type, Component>();
- components.Add(gameObject, cache);
- }
- component = GetComponent(type);
- if (component != null) {
- cache.Add(type, component);
- return true;
- }
- return false;
- }
- public string DumpCachedComponents() {
- Dictionary<Type, Component> cache;
- if (!components.TryGetValue(gameObject, out cache)) { return ""; }
- System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
- stringBuilder.Append(string.Concat(gameObject.name, " components = {"));
- if (cache.Count == 0) { stringBuilder.Append(" }"); }
- int i = 1;
- foreach (KeyValuePair<Type, Component> entry in cache) {
- stringBuilder.Append(string.Concat("\n\t", entry.Key.ToString(), " => ", entry.Value.ToString()));
- if (i < cache.Count)
- stringBuilder.Append(", ");
- else
- stringBuilder.Append("\n}");
- i++;
- }
- return stringBuilder.ToString();
- }
- public static string DumpAllCachedComponents() {
- System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
- stringBuilder.Append("components = {");
- if (components.Count == 0) { stringBuilder.Append(" }"); }
- int i = 1;
- foreach (KeyValuePair<GameObject, Dictionary<Type, Component>> cache in components) {
- stringBuilder.Append(string.Concat("\n\t", cache.Key.name, " => {"));
- if (cache.Value.Count == 0) { stringBuilder.Append(" }"); }
- int j = 1;
- foreach (KeyValuePair<Type, Component> component in cache.Value) {
- stringBuilder.Append(string.Concat("\n\t\t", component.Key.ToString(), " => ", component.Value.ToString()));
- if (j < cache.Value.Count)
- stringBuilder.Append(',');
- else
- stringBuilder.Append("\n\t}");
- j++;
- }
- if (i < components.Count)
- stringBuilder.Append(',');
- else
- stringBuilder.Append("\n}");
- i++;
- }
- return stringBuilder.ToString();
- }
- #endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement