Search Unity

[Plugin] Free Uxml to C# wrappers generator + logical templates

Discussion in 'UIElements' started by Kamyker, Sep 12, 2019.

  1. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    59
    After discussion in uxml-could-be-easily-replaced-with-C# I've created simple tool to generate C# wrapper classes for uxml files.
    https://github.com/kamyker/UnityUxmlToCsharp

    Advance example from the bottom of readme:
    Page.uxml
    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <UXML
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.     xmlns="UnityEngine.UIElements"
    5.     xsi:noNamespaceSchemaLocation="../UIElementsSchema/UIElements.xsd"
    6.     xsi:schemaLocation="UnityEngine.UIElements ../UIElementsSchema/UnityEngine.UIElements.xsd">
    7.     <Button style="--csName: TestButton1;"/>
    8.     <Button style="--csName: TestButton2;"/>
    9.     <Button style="--csName: TestButton3;"/>
    10. </UXML>
    PageConverted.cs is generated from uxml above.

    PageConvertedExtended.cs
    Code (CSharp):
    1. public class PageConvertedExtended : PageConverted
    2. {
    3.     private void PageConvertedExtended() //PageConverted constructor is called before this
    4.     {
    5.         TestButton1.clickable.clicked += () => Debug.Log("button1 clicked");
    6.         TestButton2.clickable.clicked += () => Debug.Log("button2 clicked");
    7.         TestButton3.clickable.clicked += () => Debug.Log("button3 clicked");
    8.      
    9.         TestButton1.RegisterCallback<GeometryChangedEvent>(OnWindowResized);
    10.     }
    11.  
    12.     private void OnWindowResized(GeometryChangedEvent window)
    13.     {
    14.         //using TestButton1, TestButton2, TestButton3 here
    15.     }
    16. }
    TriplePages.uxml
    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <UXML
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.     xmlns="UnityEngine.UIElements"
    5.     xsi:noNamespaceSchemaLocation="../UIElementsSchema/UIElements.xsd"
    6.     xsi:schemaLocation="UnityEngine.UIElements ../UIElementsSchema/UnityEngine.UIElements.xsd">
    7.     <Box style="--csTemplate: PageConvertedExtended;">
    8.     <Box style="--csTemplate: PageConvertedExtended;">
    9.     <Box style="--csTemplate: PageConvertedExtended;">
    10. </UXML>
    This uxml is converted to TriplePagesConverted class. It also uses something similar to uxml templates but with logic included.

    TestWindow.cs
    Code (CSharp):
    1. public class TestWindow : EditorWindow
    2. {
    3.     private void OnEnable()
    4.     {
    5.         var root = rootVisualElement;
    6.         root.Clear();
    7.         root.AddChildrenOf(new TriplePagesConverted());
    8.     }
    9. }
     
    Last edited: Sep 12, 2019