Skip to content

Instantly share code, notes, and snippets.

@takazerker
Created February 13, 2023 14:54
Show Gist options
  • Save takazerker/351b44104f00641e6d8f409bd8970459 to your computer and use it in GitHub Desktop.
Save takazerker/351b44104f00641e6d8f409bd8970459 to your computer and use it in GitHub Desktop.
Adds addressables shortcuts to the toolbar
using System.Reflection;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor;
using UnityEditor.Toolbars;
static class AddressableTools
{
static readonly System.Type ToolbarType = typeof(EditorGUI).Assembly.GetType("UnityEditor.Toolbar");
static readonly FieldInfo get = ToolbarType.GetField(nameof(get), BindingFlags.Static | BindingFlags.Public);
static readonly FieldInfo m_Root = ToolbarType.GetField(nameof(m_Root), BindingFlags.Instance | BindingFlags.NonPublic);
[InitializeOnLoadMethod]
static void OnLoad()
{
EditorApplication.update += OnUpdate;
}
static void OnUpdate()
{
var toolbar = get.GetValue(null);
if (toolbar != null)
{
var root = m_Root.GetValue(toolbar) as VisualElement;
var rightToolbar = root.Query<VisualElement>("ToolbarZoneRightAlign").First();
if (rightToolbar != null)
{
var dropdown = new EditorToolbarDropdown("Addressables", OnDropdownClick);
rightToolbar.Add(dropdown);
EditorApplication.update -= OnUpdate;
}
}
}
static void OnDropdownClick()
{
var menu = new GenericMenu();
void AddMenu(string s)
{
var menuName = s;
menu.AddItem(new GUIContent(menuName), false, () =>
{
EditorApplication.ExecuteMenuItem("Window/Asset Management/Addressables/" + menuName);
});
}
AddMenu("Groups");
AddMenu("Settings");
AddMenu("Profiles");
AddMenu("Event Viewer");
AddMenu("Analyze");
AddMenu("Hosting");
menu.ShowAsContext();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment