Skip to content

Instantly share code, notes, and snippets.

@jean-moreno
Last active November 29, 2022 13:49
Show Gist options
  • Save jean-moreno/129d3d571d6b897818175ce0c37874eb to your computer and use it in GitHub Desktop.
Save jean-moreno/129d3d571d6b897818175ce0c37874eb to your computer and use it in GitHub Desktop.
Hack Unity Console GUI
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
public class ConsoleHijacker
{
static readonly Type editorWindowDelegate = typeof(EditorWindow).Assembly.GetType("UnityEditor.HostView+EditorWindowDelegate");
static readonly Font monospaceFont = Font.CreateDynamicFontFromOSFont("Source Code Pro", 12);
static object internalOnGuiDelegate;
static bool useCustomFont;
[MenuItem("Tools/Hack Console Window")]
static void HackConsole()
{
if (internalOnGuiDelegate != null)
{
return;
}
// fetch types/fields
var consoleWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.ConsoleWindow", true);
var parentField = consoleWindowType.GetField("m_Parent", BindingFlags.Instance | BindingFlags.NonPublic);
var onGuiField = typeof(EditorWindow).Assembly.GetType("UnityEditor.HostView").GetField("m_OnGUI", BindingFlags.Instance | BindingFlags.NonPublic);
// get values
var console = EditorWindow.GetWindow(consoleWindowType);
var hostView = parentField?.GetValue(console);
var onGui = onGuiField?.GetValue(hostView);
internalOnGuiDelegate = onGui;
if (internalOnGuiDelegate == null)
{
Debug.LogError("Couldn't hijack the console window!");
return;
}
// inject custom delegate
var dgt = Delegate.CreateDelegate(editorWindowDelegate, typeof(ConsoleHijacker).GetMethod(nameof(HijackedConsoleGUI), BindingFlags.Static | BindingFlags.NonPublic)!);
onGuiField.SetValue(hostView, dgt);
}
static void HijackedConsoleGUI()
{
// add buttons or anything else above the original GUI:
var builtInFont = GUI.skin.font;
useCustomFont = EditorGUILayout.Toggle("Use monospace font", useCustomFont);
GUI.skin.font = useCustomFont ? monospaceFont : builtInFont;
// draw the default internal console GUI
var delegateType = editorWindowDelegate;
dynamic converted = Convert.ChangeType(internalOnGuiDelegate, delegateType);
foreach (dynamic deleg in converted.GetInvocationList())
{
deleg.DynamicInvoke();
}
GUI.skin.font = builtInFont;
// add anything else below the original GUI:
GUILayout.Label("This console has been hijacked!");
}
}
@jean-moreno
Copy link
Author

I procrastinated a bit the other day and searched how to hack Unity's console GUI...
This script allows you to hack Unity's Console GUI to add your own buttons or other GUI components above or below the original one.
In this example I added a toggle to enable using a monospace font for the console (note: this is now an option in Unity 2021+).

Works in Unity 2020.3 but heavily untested, and you will have to reinject the custom method after each domain reload (although this can likely be automatized).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment