Skip to content

MenuController base class for the UI Toolkit #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Pituivan opened this issue May 29, 2025 · 1 comment
Open

MenuController base class for the UI Toolkit #69

Pituivan opened this issue May 29, 2025 · 1 comment

Comments

@Pituivan
Copy link

Hello, I want to propose adding a system that I have implemented in previous projects of mine that use the Unity UI Toolkit.

It consists of a base class MenuController which is linked to a UXLM document or a Visual Element (the view). When inheriting from it, allows you to add a new attribute I've defined called QueryFromView applicable to fields of type VisualElement (or subclasses) so that field is automatically initialized to an specific element from the view.

Image

You can query by name, type, or both.

It counts with explanatory error and warning logs for every possible misuse of the system.


Depending on the project, I use two different variants:

Variant 1: EditorWindow and controller are independent

When using this option, whenever you inherit from MenuController you have to inherit the base constructor in your class, like this:

Image

You initialize your controller this way (unless you've added extra arguments to your constructor), where visualTreeAsset represents your view as a UXML document:

Image

Variant 2: EditorWindow is the controller

When opting for using this variant, you don't have to worry about constructors or inicialization. However, you have to worry about dragging the UXML file that represents your view from your EditorWindow script to the Menu UI field in the inspector.

Image

Note that this variant does not allow you to use as a view a VisualElement that is not represented by a UXML file.


Does it sound useful for your repository? In case you like it, which variant should I implement? Please let me know what you think about it.

PS: Don't mind the namespaces and spanish comments. I used screenshots from a time I shared my system in a Spanish-speaking community.

@unitycoder
Copy link
Member

i havent really used ui toolkit so cannot comment on the usage.. but feel free to post either or both.
(unless anyone else has comments)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants