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

  1. Static constructorIconCache's static constructor discovers all concrete (non-abstract, non-generic) IconCache subclasses via TypeCache.GetTypesDerivedFrom<IconCache>()

  2. Instantiation — Each discovered type is instantiated via Activator.CreateInstance and stored in a Dictionary<Type, IconCache>

  3. Clearing — All caches are cleared on AssemblyReloadEvents.beforeAssemblyReload, which destroys all cached Texture2D instances via DestroyImmediate to 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

Goal
How

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:

  1. Define an enum for your icon types

  2. Create a cache class extending IconCache<TCache, TEnum>

  3. Add a static accessor class nested in DrawGUI.Icon

Implementation Requirements

When subclassing IconCache<TCache, TEnum>, you MUST:

  1. Implement protected override Texture2D Create(TEnum iconType) — returns a new Texture2D for the given enum value

  2. Mark the class with [UsedImplicitly] — it is discovered via reflection, so static analysis may flag it as unused

  3. Ensure the class is non-abstract and non-generic — only concrete classes are discovered by TypeCache

You SHOULD NOT:

  • Override Clear() — it is sealed in IconCache<TCache, TEnum> and handles texture destruction automatically

  • Manually 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.

Member
Type
Access
Description

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.

Member
Type
Access
Description

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

circle-exclamation
circle-exclamation
circle-exclamation
circle-info

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

Last updated