Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GUI UI Toolkit Plus - Code Gen, New Elements and Other

Discussion in 'Tools In Progress' started by ErnestSurys, Sep 1, 2022.

  1. ErnestSurys

    ErnestSurys

    Joined:
    Jan 18, 2018
    Posts:
    94
    Hi, I want to share with you one of my libraries:

    UI Toolkit Plus
    at GitHub

    Description
    Bunch of UI Tooklit generic features that can be resused across multiple projects.

    Features
    UXML C# Script generation
    You can generate a partial C# class from the UXML file.

    Given the following UXML file "MainMenuView.uxml":

    Code (UXML):
    1. <ui:VisualElement>
    2.    <ui:Label name="title" />
    3. </ui:VisualElement>
    4. <ui:VisualElement name="menu">
    5.    <ui:Button name="confirm-button" />
    6. </ui:VisualElement>
    The tool will generate C# script "MainMenuView.gen.cs":
    - this script will be regenerated automatically each time corresponding uxml file changes.
    - this is an opt-in feature by design, C# files will be generated only for for files that you chose.
    - tool picks up relevant assembly and generates appropriate namespace for generated cs file.
    - more documentation here
    Code (CSharp):
    1. partial class MainMenuView
    2. {
    3.    private Label title;
    4.    private VisualElement menu;
    5.    private Button confirmButton;
    6.  
    7.    private void AssignQueryResults(VisualElement root)
    8.    {
    9.        title = root.Q<Label>("title");
    10.        menu = root.Q<VisualElement>("menu");
    11.        confirmButton = root.Q<Button>("confirm-button");
    12.    }
    13. }
    QAttribute
    This is alternative way to retrieve references from Visutal Tree Asset,

    Where standard VisualElement query looks like this:
    Code (CSharp):
    1. public class ExampleEditorWindow
    2. {
    3.    ObjectField objField;
    4.    ListView listView;
    5.    Label label;
    6.  
    7.    void OnEnable()
    8.    {
    9.      objField = rootVisualElement.Q<ObjectField>("objField");
    10.      listView = rootVisualElement.Q<ListView>("listView");
    11.      label = rootVisualElement.Q<Label>("label");
    12.    }
    13. }
    With QAttribute it looks like this:

    Code (CSharp):
    1. public class ExampleEditorWindow
    2. {
    3.    [Q("objField")] ObjectField objField;
    4.    [Q("listView")] ListView listView;
    5.    [Q("label")] Label label;
    6.  
    7.    void OnEnable()
    8.    {
    9.      rootVisualElement.AssignQueryResults(this);
    10.    }
    11. }
    QAttribute marks the class member as a query target, and the AssignQueryResults extension method assigns appropriate UQuery results to those members.

    Copy Field Declarations (UI builder extension)
    Select visual element in your UI Builder hierarchy and use UI Builder/Copy Field Declarations shortcut (Alt + C). This will copy C# field declarations for the selected visual element and its children.

    Copied C# field declarations:

    Code (CSharp):
    1.    [Q("root")]
    2.  
    3.    private VisualElement root;
    4.    [Q("child")]
    5.    private VisualElement child;
    ReorderableManipulator

    Manipulator class that allows you to drag and drop visual elements to reorder them.

    TabGroup and Tab

    TabGroup, Tab, and TabDropdown are new elements for any tab-related functionality.

    Installation
    Get GitHub repo URL and follow these instructions.
    Feedback
    Let me know if you found anything useful in here or if you wish something was more polished. :)

     
  2. ErnestSurys

    ErnestSurys

    Joined:
    Jan 18, 2018
    Posts:
    94
    1.7.0 - Style Sheet Exporter

    I added a new Editor window available for Unity 2022.1 and newer.
    Style Sheet Exporter Window allows you to export built-in style sheets into your project.
    I often found myself asking the question "How did Unity do it" when I was writing USS selectors. Now I know and so can you :)

     
    DragonCoder likes this.
  3. ErnestSurys

    ErnestSurys

    Joined:
    Jan 18, 2018
    Posts:
    94
    3.0.0 - Code Generation QoL



    Added
    - UXML Code Generation:
    - Option to override namespace for generated C# files from UXML importer header.
    - Option to generate a second file of a generated partial class.
    - Project-wide settings for code generation.
    - support for pascal case and camel case styles
    - support for prefix and suffix for code identifiers
    - Custom icon for .gen.cs files.

    Changed
    - generate c# script context action moved to dropdown menu in UXML Importer header.
    - UXML attribute rename: `code-gen-prefix` -> `gen-cs-private-field-prefix`