Class CachedScriptableObject<T>
Abstract base class for ScriptableObjects that need to be cached and retrieved quickly by ID. Objects are cached not only by their concrete type but also by their base types up to CachedScriptableObject<T>. This allows for flexible retrieval by specific derived types or broader base types.
public abstract class CachedScriptableObject<T> : ScriptableObject where T : CachedScriptableObject<T>, ICachedObject
Type Parameters
TThe concrete type of the ScriptableObject inheriting from this class. Must also implement ICachedObject.
- Inheritance
-
ObjectScriptableObjectCachedScriptableObject<T>
- Derived
- Inherited Members
-
ScriptableObject.SetDirty()ScriptableObject.CreateInstance<T>()Object.GetEntityId()Object.GetInstanceID()Object.GetHashCode()Object.InstantiateAsync<T>(T)Object.InstantiateAsync<T>(T, Transform)Object.InstantiateAsync<T>(T, Vector3, Quaternion)Object.InstantiateAsync<T>(T, Transform, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion)Object.Instantiate(Object, Vector3, Quaternion, Transform)Object.Instantiate(Object)Object.Instantiate(Object, Scene)Object.Instantiate<T>(T, InstantiateParameters)Object.Instantiate<T>(T, Vector3, Quaternion, InstantiateParameters)Object.Instantiate(Object, Transform)Object.Instantiate<T>(T)Object.Instantiate<T>(T, Vector3, Quaternion)Object.Instantiate<T>(T, Vector3, Quaternion, Transform)Object.Instantiate<T>(T, Transform)Object.Destroy(Object)Object.DestroyImmediate(Object)Object.DontDestroyOnLoad(Object)Object.DestroyObject(Object)Object.FindObjectsOfType<T>()Object.FindObjectsByType<T>(FindObjectsSortMode)Object.FindObjectsByType<T>(FindObjectsInactive, FindObjectsSortMode)Object.FindObjectOfType<T>()Object.FindFirstObjectByType<T>()Object.FindAnyObjectByType<T>()Object.FindFirstObjectByType<T>(FindObjectsInactive)Object.FindAnyObjectByType<T>(FindObjectsInactive)Object.FindObjectsByType<T>()Object.FindObjectsByType<T>(FindObjectsInactive)Object.ToString()Object.nameObject.hideFlags
Properties
ID
The unique ID generated for this cached object instance. This ID is deterministic, meaning it will be the same across application runs for the same object's type name and provided unique object name (e.g., asset file name).
public int ID { get; }
Property Value
Methods
AddToCache(string)
Adds this ScriptableObject instance to the static cache.
It registers the object under its concrete type and all relevant base types up the inheritance chain
(until CachedScriptableObject<T> is reached).
The ID is generated deterministically based on the object's concrete type name and the provided objectName.
public void AddToCache(string objectName)
Parameters
objectNamestringA unique name for this specific object instance within its type, typically its asset file name. This is crucial for generating a consistent and unique ID.
GetCache<U>()
Returns the entire dictionary of cached objects for a specific type U.
This method assumes that U is the exact type stored as T
in the inner dictionary of the cache.
public static Dictionary<int, U> GetCache<U>() where U : T
Returns
- Dictionary<int, U>
A Dictionary<TKey, TValue> containing the cached objects of type
Uindexed by their IDs, or null if no cache is found for that type.
Type Parameters
UThe specific type of objects whose cache should be retrieved. In your usage, this type is expected to be the same as the
Tparameter of this class.
GetFirst<U>()
Retrieves the first cached object of type U found in the cache.
This method should be used cautiously, as the "first" object is not guaranteed to be consistent
across different runs or iterations if the underlying dictionary order changes.
It is best suited for scenarios where you expect only one instance of a given type,
or when the specific instance returned doesn't matter.
public static U GetFirst<U>() where U : T
Returns
- U
The first cached object of type
Uif any are found; otherwise, null.
Type Parameters
UThe specific type of the cached object to retrieve. Must inherit from
T.
Get<U>(HashSet<int>)
Retrieves a list of cached objects of type U corresponding to a given set of IDs.
public static List<ICachedObject> Get<U>(HashSet<int> ids) where U : T
Parameters
idsHashSet<int>A HashSet<T> of integer IDs to retrieve.
Returns
- List<ICachedObject>
A List<T> of cached objects of type
Uthat match the provided IDs. Returns an empty list if no IDs are provided, no cache for the type exists, or no objects match the IDs.
Type Parameters
UThe specific type of objects to retrieve. Must inherit from
T.
Get<U>(int)
Retrieves a cached object by its ID, explicitly cast to type U.
public static U Get<U>(int id) where U : T
Parameters
idintThe deterministic ID of the object to retrieve.
Returns
- U
The cached object of type
Uif found; otherwise, null.
Type Parameters
UThe specific type of the cached object to retrieve. Must inherit from
T.
OnLoad(string, string, int)
Virtual method called when a ScriptableObject is successfully added to the cache. Derived classes can override this method to add custom logging, initialization, or other specific logic that should occur upon caching.
public virtual void OnLoad(string typeName, string resourceName, int resourceID)
Parameters
typeNamestringThe name of the type (e.g., "RaceTemplate") under which the object is currently being cached.
resourceNamestringThe name of the ScriptableObject asset (e.g., "HumanRace").
resourceIDintThe deterministic ID generated for the resource instance.
OnUnload(string, string, int)
Virtual method called when a ScriptableObject is successfully removed from the cache. Derived classes can override this method to add custom logging, cleanup, or other specific logic that should occur upon uncaching.
public virtual void OnUnload(string typeName, string resourceName, int resourceID)
Parameters
typeNamestringThe name of the type (e.g., "RaceTemplate") from which the object is being removed from cache.
resourceNamestringThe name of the ScriptableObject asset (e.g., "HumanRace").
resourceIDintThe deterministic ID of the resource instance.
RemoveFromCache()
Removes this ScriptableObject instance from the static cache. It removes the object from its concrete type's cache and all relevant base types up the inheritance chain, mirroring the AddToCache(string) process.
public void RemoveFromCache()