OnGUIInjector

Static utility that injects custom OnGUI callbacks into Unity editor windows by accessing their internal IMGUIContainer via reflection. Enables drawing custom GUI content in editor windows without subclassing them.

Definition

Namespace: Paragon.Editor.LowLevel Assembly: Paragon.Editor.dll

public static class OnGUIInjector

Remarks

Unity editor windows internally use a GUIViewIWindowBackendVisualElement (visual tree) hierarchy. The root element of the visual tree is an IMGUIContainer with a private m_OnGUIHandler delegate field. This class injects a callback by appending to that delegate.

Injection Chain

  1. Retrieves the windowBackend from the target ScriptableObject (which is a GUIView subclass)

  2. Gets the visualTree from the IWindowBackend

  3. Accesses the first child element — an IMGUIContainer

  4. Reads the m_OnGUIHandler delegate and appends the callback (with deduplication)

Deduplication

The injector removes the callback before adding it (-= then +=), preventing duplicate registrations if Inject is called multiple times with the same callback.

Quick Lookup

Goal
How

Inject OnGUI callback

OnGUIInjector.Inject(editorWindow, MyOnGUI)

Callback signature

Action (no parameters)

Target parameter type

ScriptableObject (must be a GUIView-based editor window)

Fields

Field
Type
Access
Description

windowBackend

PropertyInfo

private static readonly

Reflected GUIView.windowBackend property

viewVisualTree

PropertyInfo

private static readonly

Reflected IWindowBackend.visualTree property

imguiContainerOnGUI

FieldInfo

private static readonly

Reflected IMGUIContainer.m_OnGUIHandler field

Methods

Inject

Injects a custom OnGUI callback into an editor window's draw loop.

Parameter
Type
Description

targetEditor

ScriptableObject

The editor window to inject into (must be a GUIView-derived type such as AppStatusBar)

onGUICallback

Action

The callback to invoke during the window's OnGUI

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation

Examples

Injecting a Status Bar Callback

See Also

Last updated