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 OnGUIInjectorRemarks
Unity editor windows internally use a GUIView → IWindowBackend → VisualElement (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
Retrieves the
windowBackendfrom the targetScriptableObject(which is aGUIViewsubclass)Gets the
visualTreefrom theIWindowBackendAccesses the first child element — an
IMGUIContainerReads the
m_OnGUIHandlerdelegate 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
Inject OnGUI callback
OnGUIInjector.Inject(editorWindow, MyOnGUI)
Callback signature
Action (no parameters)
Target parameter type
ScriptableObject (must be a GUIView-based editor window)
Fields
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.
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
Target must be a GUIView-derived ScriptableObject The targetEditor parameter accepts ScriptableObject but internally casts it as a GUIView. Passing a non-GUIView ScriptableObject will throw a NullReferenceException when accessing windowBackend.
Unity internals dependency Relies on three internal Unity types/members: GUIView.windowBackend, IWindowBackend.visualTree, and IMGUIContainer.m_OnGUIHandler. Any of these may change between Unity versions.
Visual tree structure assumption Assumes the first child (visualTree[0]) is an IMGUIContainer. If Unity changes its visual tree layout, the injection will fail with an InvalidCastException.
Examples
Injecting a Status Bar Callback
See Also
LowLevel Overview — subsystem overview
EditorHeaderItemInjector — similar injection for header items
ParagonEntertainmentBanner — uses OnGUIInjector for status bar drawing
Last updated