Skip to content

Instantly share code, notes, and snippets.

@beardordie
Last active July 16, 2023 04:10
Show Gist options
  • Save beardordie/a93845b04e8fc7e63706024bf6a352f8 to your computer and use it in GitHub Desktop.
Save beardordie/a93845b04e8fc7e63706024bf6a352f8 to your computer and use it in GitHub Desktop.
Unity Static References class for reducing GetComponent and Find calls for object references throughout a project
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Static References helper using Dictionaries
///
/// Why? Eliminate calls to GetComponent and FindObjectOfType and other such calls for objects that you often need to reference.
/// Cache them once here, then re-use that reference project-wide
/// Singletons won't need this as they store their own static reference
///
/// "TypeList" is for components whose types are unique in a scene but aren't singletons.
/// "StringList" is for using a string key when multiple objects of that type may exist in a scene
/// "IntList" is for using an int or enum key when multiple objects of that type may exist in a scene
///
/// Get Reference Usage: (in Start) m_GameManager = References.Get<GameManager>();
/// Get Reference Usage: (in Start) m_CurrentWeapon = References.Get<Weapon>("MachineGun");
/// Get Reference Usage: (in Start) m_CurrentEnemy = References.Get<Enemy>(EnemyTypes.BigBoss);
///
/// Set Reference Usage: (in Awake) References.Set<GameManager>(this);
/// Set Reference Usage: (in Awake) References.Set<Weapon>("MachineGun",this);
/// Set Reference Usage: (in Awake) References.Set<Enemy>(EnemyTypes.BigBoss,this);
///
/// Remove Reference Usage: References.Remove<GameManager>();
/// Remove Reference Usage: References.Remove("MachineGun");
/// Remove Reference Usage: References.Remove(EnemyTypes.BigBoss);
///
/// License: CC0 as is no warranties; Author: Beard or Die
/// </summary>
public static class References
{
public static Dictionary<System.Type, object> TypeList = new Dictionary<System.Type, object>();
public static Dictionary<string, object> StringList = new Dictionary<string, object>();
public static Dictionary<int, object> IntList = new Dictionary<int, object>();
/// <summary>
/// Gets the unique object of the specific type that has already been set.
/// If none found, uses FindObjectOfType to try and find it in the scene, then cache and use that reference
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static T Get<T>()
{
if (TypeList.ContainsKey(typeof(T)))
{
// Found existing reference
return (T)TypeList[typeof(T)];
}
// No such reference found. Return null (default)
return default;
}
/// <summary>
/// Gets a cached reference using the specified string as the key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="typeKey"></param>
/// <returns></returns>
public static T Get<T>(string typeKey)
{
if (StringList.ContainsKey(typeKey))
{
return (T)StringList[typeKey];
}
return default;
}
/// <summary>
/// Gets a cached reference using the specified int/enum as the key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="enumKey"></param>
/// <returns></returns>
public static T Get<T>(int enumKey)
{
if (IntList.ContainsKey(enumKey))
{
return (T)IntList[enumKey];
}
return default;
}
/// <summary>
/// Caches the reference and uses its type as the key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="reference"></param>
public static void Set<T>(object reference)
{
if (!TypeList.ContainsKey(typeof(T)))
{
TypeList.Add(typeof(T), reference);
}
}
/// <summary>
/// Caches a reference to an object using a string as the key
/// </summary>
/// <param name="stringKey"></param>
/// <param name="objectReference"></param>
public static void Set(string stringKey, object objectReference)
{
if (!StringList.ContainsKey(stringKey))
{
StringList.Add(stringKey, objectReference);
}
}
/// <summary>
/// Caches a reference to an object using an int/enum as the key
/// </summary>
/// <param name="enumKey"></param>
/// <param name="objectReference"></param>
public static void Set(int enumKey, object objectReference)
{
if (!IntList.ContainsKey(enumKey))
{
IntList.Add(enumKey, objectReference);
}
}
/// <summary>
/// Removed a reference if one exists
/// </summary>
/// <typeparam name="T"></typeparam>
public static void Remove<T>()
{
if (TypeList.ContainsKey(typeof(T)))
{
TypeList.Remove(typeof(T));
}
}
/// <summary>
/// Removes a reference if one exists using a string as the key
/// </summary>
/// <param name="stringKey"></param>
public static void Remove(string stringKey)
{
if (StringList.ContainsKey(stringKey)) StringList.Remove(stringKey);
}
/// <summary>
/// Removes a reference if one exists using an int/enum as the key
/// </summary>
/// <param name="enumKey"></param>
public static void Remove(int enumKey)
{
if (IntList.ContainsKey(enumKey)) IntList.Remove(enumKey);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment