Skip to content

Unity 6.3 alternative approach #7

Description

@FrantisekHolubec

Hi, I saw your approach to Unity 6.3 new toolbar
And I just wanted to share my reflection-based alternative

Reflection-based factory

public static class MainToolbarElementFactory
{
	private static readonly ConstructorInfo CTOR;

	static MainToolbarElementFactory()
	{
		var type = Type.GetType("UnityEditor.Toolbars.MainToolbarCustom, UnityEditor");
		CTOR = type?.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, new[] { typeof(Func<VisualElement>) }, null);
	}

	public static MainToolbarElement Create(Func<VisualElement> createElement)
	{
		return CTOR == null ? null : CTOR.Invoke(new object[] { createElement }) as MainToolbarElement;
	}
}

Example of use

[MainToolbarElement("SampleButton", defaultDockPosition = MainToolbarDockPosition.Right)]
public static MainToolbarElement CreateSampleButton()
{
	return MainToolbarElementFactory.Create(() =>
	{
		var button = new Button(() => Debug.Log("Test"));
		return button;
	});
}

I don't really like the approach of creating an empty slate and then populating it, but that's just me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions