Skip to content

Extension Methods to better build UI through code using Unity UIElements

License

Notifications You must be signed in to change notification settings

ltmx/Unity.UIToolkit.Extensions

Repository files navigation

Unity.UIToolkit.Extensions

Extension Library for Unity UIElements

GitHub package.json version GitHub repo size GitHub top language GitHub Made for Unity

Create UI using LINQ Syntax

Looks :

    public static TextField CreateInputTextField(this VisualElement container)
    {
        var row = container.Row();
        row.Image().USS("logo");
        
        var inputTextField = row.TextField()
            .Multiline(true)
            .Value(AthenaSettings.DefaultPrompt)
            .ToolTip("[Shift + Enter] to SEND\n[Esc] to STOP chat")
            .Placeholder("Type your message here")
            .HidePlaceholderOnFocus(true);
        return inputTextField;
    }

Install

Unity > Package Manager > Install via Git URL > paste https://github.com/ltmx/Unity.UIToolkit.Extensions.git

Dependencies (automaticaly installed)

Examples

Create Child Elements

element.Button();
element.TextField();
element.Label();

Styling

element.USS("my-style");
element.RemoveUSS("my-class");
element.ClearUSS("my-class");
element.WhereClassListContains("my-class");
element.FirstElementWithClass("my-class");

// of course everything links together
myButton.WhereClassListContains("unity-text-element").ClearUSS();

An example of how I create a Path Field

public PathField(string name) : base(name)
{
    var button = new ActionButton(OpenPathInExplorer)
        .USS("open-button")
        .FlexShrink(1)
        .FlexGrow(1)
        .AlignSelf(Align.Center)
        .Margin(0)
        .MarginRight(8)
        .Size(24)
        .BackgroundSize(BackgroundSizeType.Contain);
    Insert(1, button);
}