Skip to content

LibraStack/UnityUxmlGenerator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

UnityUxmlGenerator

This package is part of UnityMvvmToolkit.

πŸ“– Table of Contents

πŸ“ About

The UnityUxmlGenerator allows you to generate UxmlFactory and UxmlTraits using [UxmlElement] and [UxmlAttribute] attributes.

[UxmlElement]
public partial class CustomVisualElement : VisualElement
{
    [UxmlAttribute]
    private string CustomAttribute { get; set; }
}

🌡 Folder Structure

.
β”œβ”€β”€ src
β”‚   β”œβ”€β”€ UnityUxmlGenerator
β”‚   └── UnityUxmlGenerator.UnityPackage
β”‚       ...
β”‚       └── UnityUxmlGenerator.dll      # Auto-generated
β”‚
β”œβ”€β”€ UnityUxmlGenerator.sln

βš™οΈ Installation

You can install UnityUxmlGenerator in one of the following ways:

1. Install via package file
  • Download unityuxmlgenerator.tgz from releases
  • Open Window/Package Manager
  • Select Add package from tarball...
2. Install via Git URL

You can add https://github.com/LibraStack/UnityUxmlGenerator.git?path=src/UnityUxmlGenerator.UnityPackage/Assets/Plugins/UnityUxmlGenerator to the Package Manager.

If you want to set a target version, UnityUxmlGenerator uses the v*.*.* release tag, so you can specify a version like #v0.0.1. For example https://github.com/LibraStack/UnityUxmlGenerator.git?path=src/UnityUxmlGenerator.UnityPackage/Assets/Plugins/UnityUxmlGenerator#v0.0.1-preview1.

πŸ•ΉοΈ How To Use

To create a custom control, just add the [UxmlElement] attribute to the custom control class definition. The custom control class must be declared as a partial class and be inherited from VisualElement or one of its derived classes.

You can use the [UxmlAttribute] attribute to declare that a property is associated with a UXML attribute.

The following example creates a custom control with multiple attributes:

[UxmlElement]
public partial class CustomVisualElement : VisualElement
{
    [UxmlAttribute]
    private string CustomAttribute { get; set; }
    
    [UxmlAttribute("DefaultValue")]
    private string CustomAttributeWithDefaultValue { get; set; }
}
Generated code

CustomVisualElement.UxmlFactory.g.cs

partial class CustomVisualElement
{
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("UnityUxmlGenerator", "1.0.0.0")]
    public new class UxmlFactory : global::UnityEngine.UIElements.UxmlFactory<CustomVisualElement, UxmlTraits> 
    {
    }
}

CustomVisualElement.UxmlTraits.g.cs

partial class CustomVisualElement
{
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("UnityUxmlGenerator", "1.0.0.0")]
    public new class UxmlTraits : global::UnityEngine.UIElements.VisualElement.UxmlTraits
    {
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("UnityUxmlGenerator", "1.0.0.0")]
        private readonly global::UnityEngine.UIElements.UxmlStringAttributeDescription _customAttribute = new() 
            { name = "custom-attribute", defaultValue = "" };

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("UnityUxmlGenerator", "1.0.0.0")]
        private readonly global::UnityEngine.UIElements.UxmlStringAttributeDescription _customAttributeWithDefaultValue = new() 
            { name = "custom-attribute-with-default-value", defaultValue = "DefaultValue" };

        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("UnityUxmlGenerator", "1.0.0.0")]
        [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
        public override void Init(global::UnityEngine.UIElements.VisualElement visualElement, 
            global::UnityEngine.UIElements.IUxmlAttributes bag, 
            global::UnityEngine.UIElements.CreationContext context)
        {
            base.Init(visualElement, bag, context);

            var control = (CustomVisualElement) visualElement;
            control.CustomAttribute = _customAttribute.GetValueFromBag(bag, context);
            control.CustomAttributeWithDefaultValue = _customAttributeWithDefaultValue.GetValueFromBag(bag, context);
        }
    }
}

Note: For now, only string attributes are supported.

πŸ“‘ Contributing

You may contribute in several ways like creating new features, fixing bugs or improving documentation and examples.

Discussions

Use discussions to have conversations and post answers without opening issues.

Discussions is a place to:

  • Share ideas
  • Ask questions
  • Engage with other community members

Report a bug

If you find a bug in the source code, please create bug report.

Please browse existing issues to see whether a bug has previously been reported.

Request a feature

If you have an idea, or you're missing a capability that would make development easier, please submit feature request.

If a similar feature request already exists, don't forget to leave a "+1" or add additional information, such as your thoughts and vision about the feature.

Show your support

Give a ⭐ if this project helped you!

Buy Me A Coffee

βš–οΈ License

Usage is provided under the MIT License.

About

Generates "UxmlFactory" and "UxmlTraits" source code for a custom VisualElement

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages