ErnSur/UI-Toolkit-Plus

Binding script automatic generation

Closed this issue · 5 comments

@ErnSur hi , thanks for this useful ui toolkit extension.

try this out

I add script automatic generation feature and I think this way faster than alt+c copy and paste .

If you think this great I will optimize the code then pull request.

Right Click any UXML files , and click UIToolkit->GenerateBinding .

If generated before , it also scan all scripts in project , find the right file (By file name like : UXML File Name+"Binding.cs") and update.

image
image
Bing the data

Hi @Shaun-Fong, great to see someone using this lib and even contributing.

This feature is something I thought about adding at the beginning but scraped it for a less complex solution- in the form of QAttribute.
Now that I got back to this topic, I did some thinking and R&D and I think that it still might be an interesting addition to the library, although it would require a more complex implementation than what I can see in your fork.

If you are interested I can write down the specifications for this feature and explain the reasoning behind it. We could work on it together, just let me know if you are interested in this form of collaboration.

If you are interested I can write down the specifications for this feature and explain the reasoning behind it. We could work on it together, just let me know if you are interested in this form of collaboration.

of course! that sounds great!

Query assignment generation feature

Purpose:

automate referencing named elements from UXML so that the user doesn't have to copy and paste names from VisualTreeAsset and write query calls each time the element name is modified or added.

Code generation

  • Taking a UXML file as input we can generate a new C# script.
  • For each UXML tag with a name attribute we would generate a new field and assignment in the c# script.
  • Generated C# class would also contain a method that assigns query results to all defined fields.

Given the following UXML:

<ui:VisualElement>
    <ui:Label name="title" />
</ui:VisualElement>
<ui:VisualElement name="menu">
    <ui:Button name="confirm-button" />
</ui:VisualElement>

We generate something like:

partial class CodeGenExample
{
    private Label title;
    private VisualElement menu;
    private Button confirmButton;

    private void AssignQueryResults(VisualElement root)
    {
        title = root.Q<Label>("title");
        menu = root.Q<VisualElement>("menu");
        confirmButton = root.Q<Button>("confirm-button");
    }
}

Specifications of the generated class

  • partial class
  • private fields
  • protected "Assign" method

Open Questions:

  • Should we add an option to format field names to your liking? Like adding a prefix to generated field names.
  • should the generated cs file always be in the same dir as UXML or should the user be able to relocate generated script?
    • if so how to ensure that the regenerated script overrides the old one?
    • if we want to allow relocation of this file we need to track it somehow for the autogeneration feature.
  • Option to Toggle automatic generation for a UXML file
    • save toggle value to AssetImporter.userdata?
  • How to set the appropriate namespace for the generated script (✅ done)
    • get it from asmdef.rootnamespace > asmdef name > project namespace > no namespace

Added to develop