This package is part of UnityMvvmToolkit.
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; }
}
.
βββ src
β βββ UnityUxmlGenerator
β βββ UnityUxmlGenerator.UnityPackage
β ...
β βββ UnityUxmlGenerator.dll # Auto-generated
β
βββ UnityUxmlGenerator.sln
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
.
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.
You may contribute in several ways like creating new features, fixing bugs or improving documentation and examples.
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
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.
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.
Give a β if this project helped you!
Usage is provided under the MIT License.