Class InterfaceUtil
Provides methods for discovering and retrieving Unity UnityEngine.MonoBehaviour components by interface type. Supports scene-wide searches, single-object searches, and type-discovery queries backed by a thread-safe reflection cache, so that the assembly scan is only performed once per interface type per session.
Inherited Members
Namespace: Scylla.Core.Util
Assembly: ScyllaCore.dll
Syntax
public static class InterfaceUtil
Remarks
Choosing a search method:
- GetComponents<T>() - fast for one-off searches; iterates all scene MonoBehaviours once.
- FindComponentsOfType<T>() - more efficient when the same interface is searched repeatedly, because implementing types are discovered once and cached.
- GetComponents<T>(GameObject) and GetComponent<T>(GameObject) - O(1) relative to scene size; searches only components on a single object.
Performance Warning: Scene-wide searches always issue at least one Object.FindObjectsByType
call, which is expensive. Call these methods only during initialization and cache the results.
The reflection cache is populated on first use and can be invalidated via ClearCache() if new assemblies are loaded at runtime.
Methods
ClearCache()
Removes all entries from the internal interface-to-implementing-types cache. After this call, the next call to GetInterfaceTypes(Type) or any scene/object search method will re-scan all loaded assemblies from scratch.
Declaration
public static void ClearCache()
Remarks
Call this when a new assembly is loaded at runtime (e.g. via Addressables code packages) and you need to discover newly registered implementing types. Under normal circumstances the cache does not need to be cleared.
FindComponentsOfType<T>()
Returns all components in the loaded scene(s) that implement interface T,
using cached reflection data from GetInterfaceTypes<T>() to find every concrete
implementing type first. This is more efficient than GetComponents<T>() when the
same interface is searched multiple times, since the type discovery is cached. Inactive GameObjects
are excluded. This is a convenience overload that forwards to
FindComponentsOfType<T>(bool) with includeInactive = false.
Declaration
public static T[] FindComponentsOfType<T>()
Returns
| Type | Description |
|---|---|
| T[] | An array of all scene components implementing |
Type Parameters
| Name | Description |
|---|---|
| T | The interface type to search for. |
Remarks
Performance Warning: This method issues one Object.FindObjectsByType call per
concrete implementing type. Cache results where possible.
FindComponentsOfType<T>(bool)
Returns all components in the loaded scene(s) that implement interface T,
using cached reflection data from GetInterfaceTypes(Type) to enumerate all
concrete implementing types. For each implementing type that is a UnityEngine.Component,
performs a FindObjectsByType call and accumulates deduplicated results.
Declaration
public static T[] FindComponentsOfType<T>(bool includeInactive)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | includeInactive | When |
Returns
| Type | Description |
|---|---|
| T[] | A deduplicated array of all found components implementing |
Type Parameters
| Name | Description |
|---|---|
| T | The interface type to search for. |
GetComponent<T>(GameObject)
Returns the first component attached to gameObject that implements
interface T. Uses cached type reflection from
GetInterfaceTypes(Type) and forwards to GetComponent(Type) for each
known implementing concrete type until a match is found.
Declaration
public static T GetComponent<T>(GameObject gameObject)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject | The GameObject to search. When |
Returns
| Type | Description |
|---|---|
| T | The first matching component cast to |
Type Parameters
| Name | Description |
|---|---|
| T | The interface type to search for. |
GetComponents<T>()
Returns all active UnityEngine.MonoBehaviour instances currently in the loaded scene(s) that
implement interface T. Inactive GameObjects are excluded. This is a
convenience overload that forwards to GetComponents<T>(bool) with
includeInactive = false.
Declaration
public static T[] GetComponents<T>()
Returns
| Type | Description |
|---|---|
| T[] | An array containing every active UnityEngine.MonoBehaviour that implements |
Type Parameters
| Name | Description |
|---|---|
| T | The interface type to search for. Must be an interface, but this is not enforced at compile time. |
Remarks
Performance Warning: This method calls Object.FindObjectsByType<MonoBehaviour>,
which scans every UnityEngine.MonoBehaviour in the scene. It is intended for one-shot
initialization only - cache the result rather than calling per-frame.
Consider using FindComponentsOfType<T>() instead if the same interface will be searched repeatedly, as that overload benefits from cached type reflection data.
GetComponents<T>(bool)
Returns all UnityEngine.MonoBehaviour instances currently in the loaded scene(s) that implement
interface T. The search iterates every UnityEngine.MonoBehaviour in the
scene and filters by interface assignability using the is T pattern match.
Declaration
public static T[] GetComponents<T>(bool includeInactive)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | includeInactive | When |
Returns
| Type | Description |
|---|---|
| T[] | An array containing every UnityEngine.MonoBehaviour that implements |
Type Parameters
| Name | Description |
|---|---|
| T | The interface type to search for. |
Remarks
Performance Warning: This method calls Object.FindObjectsByType<MonoBehaviour>,
which visits all MonoBehaviours in the scene. Do not call per-frame; cache results where possible.
GetComponents<T>(GameObject)
Returns all UnityEngine.MonoBehaviour components attached to gameObject
that implement interface T. Unlike the scene-wide overload, this only
examines components on the given object and incurs no scene scan cost.
Declaration
public static T[] GetComponents<T>(GameObject gameObject)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject | The GameObject whose components are searched. When |
Returns
| Type | Description |
|---|---|
| T[] | An array of all matching components on |
Type Parameters
| Name | Description |
|---|---|
| T | The interface type to search for. |
GetInterfaceTypes(Type)
Returns all concrete (non-interface) types in every assembly loaded into the current
AppDomain that implement interfaceType. The search uses
IsAssignableFrom(Type) and skips types that are themselves interfaces. Results are
stored in Scylla.Core.Util.InterfaceUtil._interfaceTypesCache under the lock Scylla.Core.Util.InterfaceUtil._lock so that concurrent
callers never duplicate the scan. Assemblies that throw ReflectionTypeLoadException
are handled gracefully by using the partial type list they provide.
Declaration
public static Type[] GetInterfaceTypes(Type interfaceType)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | interfaceType | The interface Type to discover implementations for.
Must be non- |
Returns
| Type | Description |
|---|---|
| Type[] | An array of all concrete types that implement |
GetInterfaceTypes<T>()
Returns all concrete (non-interface) types in the current AppDomain that implement
interface T. Results are cached in Scylla.Core.Util.InterfaceUtil._interfaceTypesCache after
the first call so subsequent lookups for the same interface type are O(1) dictionary reads.
This is a convenience overload that delegates to GetInterfaceTypes(Type).
Declaration
public static Type[] GetInterfaceTypes<T>()
Returns
| Type | Description |
|---|---|
| Type[] | An array of all concrete types that implement |
Type Parameters
| Name | Description |
|---|---|
| T | The interface type to discover implementations for. |
HasComponent<T>(GameObject)
Returns true if gameObject has at least one component that implements
interface T. This is a convenience wrapper around
GetComponent<T>(GameObject) that avoids the need to receive the component itself.
Declaration
public static bool HasComponent<T>(GameObject gameObject)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject | The GameObject to inspect. When |
Returns
| Type | Description |
|---|---|
| bool |
|
Type Parameters
| Name | Description |
|---|---|
| T | The interface type to check for. |
TryGetComponent<T>(GameObject, out T)
Attempts to retrieve the first component attached to gameObject that
implements interface T. Delegates to GetComponent<T>(GameObject).
Declaration
public static bool TryGetComponent<T>(GameObject gameObject, out T component)
Parameters
| Type | Name | Description |
|---|---|---|
| GameObject | gameObject | The GameObject to search. When |
| T | component | When this method returns |
Returns
| Type | Description |
|---|---|
| bool |
|
Type Parameters
| Name | Description |
|---|---|
| T | The interface type to search for. |