0.6.3: Fixed issue, where toolbar items didn't accept mouse input in 2021 or newer versions

This commit is contained in:
Anders Ejlersen 2021-06-21 10:01:49 +02:00
parent 17c55cd03c
commit 99c83ea6d3
9 changed files with 186 additions and 73 deletions

View file

@ -15,22 +15,89 @@ namespace Game.NavigationTool.Editor.Toolbar
{
internal static class ToolbarUtility
{
private static readonly Assembly ASSEMBLY = typeof(UnityEditor.Editor).Assembly;
#if UNITY_2021_1_OR_NEWER
private static ScriptableObject CURRENT_TOOLBAR;
private static VisualElement CURRENT_PARENT_LEFT;
private static VisualElement CURRENT_PARENT_RIGHT;
private static int CURRENT_INSTANCE_ID = -1;
#endif
#if UNITY_2021_1_OR_NEWER
public static void OnUpdate(Action<VisualElement, VisualElement> callback)
{
if (CURRENT_TOOLBAR == null)
CURRENT_TOOLBAR = GetToolbarObject();
if (CURRENT_TOOLBAR != null && CURRENT_PARENT_LEFT != null && CURRENT_TOOLBAR.GetInstanceID() != CURRENT_INSTANCE_ID)
{
CURRENT_PARENT_LEFT.RemoveFromHierarchy();
CURRENT_PARENT_LEFT = null;
CURRENT_PARENT_RIGHT.RemoveFromHierarchy();
CURRENT_PARENT_RIGHT = null;
CURRENT_INSTANCE_ID = CURRENT_TOOLBAR.GetInstanceID();
}
if (CURRENT_TOOLBAR == null || CURRENT_PARENT_LEFT != null)
return;
CURRENT_PARENT_LEFT?.RemoveFromHierarchy();
CURRENT_PARENT_RIGHT?.RemoveFromHierarchy();
FieldInfo root = CURRENT_TOOLBAR.GetType().GetField("m_Root", BindingFlags.NonPublic | BindingFlags.Instance);
object rawRoot = root?.GetValue(CURRENT_TOOLBAR);
var mRoot = rawRoot as VisualElement;
CURRENT_PARENT_LEFT = CreateParent(mRoot, "ToolbarZoneLeftAlign", true);
CURRENT_PARENT_RIGHT = CreateParent(mRoot, "ToolbarZoneRightAlign", false);
callback?.Invoke(CURRENT_PARENT_LEFT, CURRENT_PARENT_RIGHT);
}
private static VisualElement CreateParent(VisualElement root, string query, bool isLeft)
{
VisualElement parent = root.Q(query);
var result = new VisualElement
{
style = {
flexGrow = 1,
flexDirection = FlexDirection.Row
}
};
if (isLeft)
{
result.Add(new VisualElement
{
style = {
flexGrow = 1
}
});
}
parent.Add(result);
return result;
}
#else
public static void AddGuiListener(Action action)
{
Assembly assembly = typeof(UnityEditor.Editor).Assembly;
Type typeToolbar = assembly.GetType("UnityEditor.Toolbar");
Object[] toolbars = Resources.FindObjectsOfTypeAll(typeToolbar);
ScriptableObject so = toolbars.Length > 0 ? (ScriptableObject)toolbars[0] : null;
ScriptableObject so = GetToolbarObject();
if (so == null)
return;
Type typeGuiView = assembly.GetType("UnityEditor.GUIView");
Type typeGuiView = ASSEMBLY.GetType("UnityEditor.GUIView");
#if UNITY_2020_1_OR_NEWER
Type typeWindowBackend = assembly.GetType("UnityEditor.IWindowBackend");
Type typeWindowBackend = ASSEMBLY.GetType("UnityEditor.IWindowBackend");
PropertyInfo piWindowBackend = typeGuiView.GetProperty("windowBackend", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
PropertyInfo piVisualTree = typeWindowBackend.GetProperty("visualTree", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (piWindowBackend == null)
return;
#else
PropertyInfo piVisualTree = typeGuiView.GetProperty("visualTree", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
#endif
@ -44,7 +111,7 @@ namespace Game.NavigationTool.Editor.Toolbar
return;
#if UNITY_2020_1_OR_NEWER
var windowBackend = piWindowBackend.GetValue(so);
object windowBackend = piWindowBackend.GetValue(so);
var visualTree = (VisualElement)piVisualTree.GetValue(windowBackend, null);
#else
var visualTree = (VisualElement)piVisualTree.GetValue(so, null);
@ -56,13 +123,21 @@ namespace Game.NavigationTool.Editor.Toolbar
handler += action;
fiImguiContainer.SetValue(container, handler);
}
#endif
public static IToolbarDrawer[] GetAllDrawers()
private static ScriptableObject GetToolbarObject()
{
Type typeToolbar = ASSEMBLY.GetType("UnityEditor.Toolbar");
Object[] toolbars = Resources.FindObjectsOfTypeAll(typeToolbar);
return toolbars.Length > 0 ? (ScriptableObject)toolbars[0] : null;
}
public static AbstractToolbarDrawer[] GetAllDrawers()
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
Type iType = typeof(IToolbarDrawer);
Type iType = typeof(AbstractToolbarDrawer);
var list = new List<IToolbarDrawer>(8);
var list = new List<AbstractToolbarDrawer>(8);
for (var i = 0; i < assemblies.Length; i++)
{
@ -74,7 +149,7 @@ namespace Game.NavigationTool.Editor.Toolbar
Type type = types[j];
if (!type.IsAbstract && iType.IsAssignableFrom(type))
list.Add((IToolbarDrawer)FormatterServices.GetUninitializedObject(type));
list.Add((AbstractToolbarDrawer)FormatterServices.GetUninitializedObject(type));
}
}