/// ---------------------------------------------
/// Senses Pack for Behavior Designer Pro
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.BehaviorDesigner.AddOns.SensesPack.Runtime.Utility
{
using System;
using System.Collections.Generic;
using UnityEngine;
///
/// Caches the components for quick lookup.
///
public static class CacheUtility
{
private static Dictionary> s_GameObjectComponentMap = new Dictionary>();
private static Dictionary> s_GameObjectParentComponentMap = new Dictionary>();
private static Dictionary> s_GameObjectComponentsMap = new Dictionary>();
///
/// Returns a cached component reference for the specified type.
///
/// The GameObject (or child GameObject) to get the component reference of.
/// The cached component reference.
public static T GetCachedComponent(this GameObject gameObject)
{
Dictionary typeComponentMap;
// Return the cached component if it exists.
if (s_GameObjectComponentMap.TryGetValue(gameObject, out typeComponentMap)) {
object targetObject;
if (typeComponentMap.TryGetValue(typeof(T), out targetObject)) {
return (T)targetObject;
}
} else {
// The cached component doesn't exist for the specified type.
typeComponentMap = new Dictionary();
s_GameObjectComponentMap.Add(gameObject, typeComponentMap);
}
// Find the component reference and cache the results.
var targetComponent = gameObject.GetComponent();
typeComponentMap.Add(typeof(T), targetComponent);
return targetComponent;
}
///
/// Returns a cached parent component reference for the specified type.
///
/// The GameObject (or child GameObject) to get the component reference of.
/// The cached component reference.
public static T GetCachedParentComponent(this GameObject gameObject)
{
Dictionary typeComponentMap;
// Return the cached component if it exists.
if (s_GameObjectParentComponentMap.TryGetValue(gameObject, out typeComponentMap)) {
object targetObject;
if (typeComponentMap.TryGetValue(typeof(T), out targetObject)) {
return (T)targetObject;
}
} else {
// The cached component doesn't exist for the specified type.
typeComponentMap = new Dictionary();
s_GameObjectParentComponentMap.Add(gameObject, typeComponentMap);
}
// Find the component reference and cache the results.
var targetComponent = gameObject.GetComponentInParent();
typeComponentMap.Add(typeof(T), targetComponent);
return targetComponent;
}
///
/// Returns a cached component reference for the specified type.
///
/// The GameObject (or child GameObject) to get the component reference of.
/// The cached components reference.
public static T[] GetCachedComponentsInChildren(this GameObject gameObject)
{
Dictionary typeComponentsMap;
// Return the cached component if it exists.
if (s_GameObjectComponentsMap.TryGetValue(gameObject, out typeComponentsMap)) {
object[] targetObjects;
if (typeComponentsMap.TryGetValue(typeof(T), out targetObjects)) {
return targetObjects as T[];
}
} else {
// The cached component doesn't exist for the specified type.
typeComponentsMap = new Dictionary();
s_GameObjectComponentsMap.Add(gameObject, typeComponentsMap);
}
// Find the component reference and cache the results.
var targetComponents = gameObject.GetComponentsInChildren();
typeComponentsMap.Add(typeof(T), targetComponents as object[]);
return targetComponents;
}
///
/// Clears the cache.
///
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
public static void ClearCache()
{
s_GameObjectComponentMap.Clear();
s_GameObjectParentComponentMap.Clear();
s_GameObjectComponentsMap.Clear();
}
}
}