Skip to content

Instantly share code, notes, and snippets.

@mminer
Last active February 27, 2023 16:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mminer/f36cea7306b3bc6ffbc2b980ad099fdd to your computer and use it in GitHub Desktop.
Save mminer/f36cea7306b3bc6ffbc2b980ad099fdd to your computer and use it in GitHub Desktop.
Adds a UI element to the Unity 2022.1 toolbar using reflection.
var toolbarType = Assembly
.GetCallingAssembly()
.GetType("UnityEditor.Toolbar")!;
var toolbar = toolbarType
.GetField("get", BindingFlags.Public | BindingFlags.Static)!
.GetValue(null);
var toolbarRootVisualElement = toolbarType
.GetField("m_Root", BindingFlags.Instance | BindingFlags.NonPublic)!
.GetValue(toolbar) as VisualElement;
var rightToolbar = toolbarRootVisualElement.Q("ToolbarZoneRightAlign");
rightToolbar.Add(new EditorToolbarButton(() =>
{
Debug.Log("Clicked!");
}) { text = "Click Me" });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment