DrawGUI.Icon
Icon caching infrastructure for the Paragon Editor. Defines the abstract IconCache base class and its generic IconCache<TCache, TEnum> subclass, which together provide a type-safe, auto-clearing icon cache system. Concrete icon providers (Paragon, Sirenix, Unity) extend this infrastructure in the Icons/ subsystem.
Definition
Namespace: Paragon.Editor Assembly: Paragon.Editor.dll
public static partial class DrawGUI
{
public static partial class Icon
{
abstract class IconCache { ... }
abstract class IconCache<TCache, TEnum> : IconCache where TCache : IconCache<TCache, TEnum>
where TEnum : Enum { ... }
}
}Remarks
DrawGUI.Icon is a static partial class that serves as the namespace for all icon-related functionality. This core file defines the caching infrastructure. The concrete icon providers (Paragon, Sirenix, Unity) are defined in separate files under Icons/ and add nested static classes to DrawGUI.Icon.
IconCache Architecture
The caching system uses the Curiously Recurring Template Pattern (CRTP) to provide type-safe static access to cache instances:
Auto-Discovery and Lifecycle
Static constructor —
IconCache's static constructor discovers all concrete (non-abstract, non-generic)IconCachesubclasses viaTypeCache.GetTypesDerivedFrom<IconCache>()Instantiation — Each discovered type is instantiated via
Activator.CreateInstanceand stored in aDictionary<Type, IconCache>Clearing — All caches are cleared on
AssemblyReloadEvents.beforeAssemblyReload, which destroys all cachedTexture2Dinstances viaDestroyImmediateto prevent memory leaks
Lazy Loading
IconCache<TCache, TEnum>.Get(TEnum) checks the dictionary for a cached texture. On cache miss, it calls the abstract Create(TEnum) method (implemented by each provider) and stores the result. Stale/destroyed textures (null check) trigger re-creation.
Quick Lookup
Get a Paragon icon
DrawGUI.Icon.Paragon.Checkmark or DrawGUI.Icon.Paragon.Get(ParagonIconType.CHECKMARK)
Create icon with custom color
DrawGUI.Icon.Paragon.Create(ParagonIconType.CHECKMARK, Color.red, 32, 32)
Extend with new provider
Subclass IconCache<TCache, TEnum> with your enum (see Extension Points)
Extension Points
Creating a Custom Icon Provider
To add a new icon source to the DrawGUI.Icon system:
Define an enum for your icon types
Create a cache class extending
IconCache<TCache, TEnum>Add a static accessor class nested in
DrawGUI.Icon
Implementation Requirements
When subclassing IconCache<TCache, TEnum>, you MUST:
Implement
protected override Texture2D Create(TEnum iconType)— returns a newTexture2Dfor the given enum valueMark the class with
[UsedImplicitly]— it is discovered via reflection, so static analysis may flag it as unusedEnsure the class is non-abstract and non-generic — only concrete classes are discovered by
TypeCache
You SHOULD NOT:
Override
Clear()— it issealedinIconCache<TCache, TEnum>and handles texture destruction automaticallyManually register the cache — discovery is automatic via the static constructor
Nested Classes
IconCache (abstract)
Base class for all icon caches. Manages auto-discovery and global clearing.
icon_size
int
protected const
Default icon size: 64 pixels
caches
Dictionary<Type, IconCache>
private static readonly
Global registry of all cache instances
GetCache<T>()
static method
protected
Retrieves a typed cache instance from the registry
ClearCaches()
static method
private
Clears all caches (called on assembly reload)
Clear()
abstract method
protected
Implemented by subclasses to destroy cached textures
IconCache<TCache, TEnum> (abstract generic)
Typed cache that maps enum values to Texture2D instances. Uses CRTP (TCache references the concrete subclass) for static Get() access.
iconCache
Dictionary<TEnum, Texture2D>
private readonly
Maps enum values to cached textures
Create(TEnum)
abstract method
protected
Creates a Texture2D for the given icon type
Get(TEnum)
static method
internal
Returns cached texture or creates/caches on miss
Clear()
sealed override
protected
Destroys all cached textures and clears the dictionary
Common Pitfalls
Textures are destroyed on assembly reload All cached textures are destroyed via DestroyImmediate when assemblies reload. Any Texture2D references held externally will become null. Always re-fetch icons via Get() rather than caching the Texture2D reference long-term.
CRTP constraint is required The TCache type parameter must reference the concrete class itself (CRTP). For example, class Cache : IconCache<Cache, MyEnum>. If TCache does not match the declaring class, GetCache<TCache>() will fail with a KeyNotFoundException.
IconCache classes are internal Both IconCache and IconCache<TCache, TEnum> are abstract class without explicit access modifiers (defaulting to internal within the namespace). They are accessible within the Paragon.Editor assembly but not from external assemblies.
Null textures trigger re-creation Get() checks if the cached Texture2D is not null before returning it. If Unity destroys the texture (e.g., during domain reload), the next Get() call will re-create it via Create().
Examples
Using Icons in Editor GUI
See Also
DrawGUI — core drawing methods
DrawGUI.Style — style presets
DrawGUI.Scope — layout scopes
DrawGUI Overview — system overview
Last updated