Hello,
I would like to propose a way to define my own Attributes for VisualElements that I have little to no control of (i.e. stock VisualElements, and those that may be created by content creators).
The problem
Currently if I want to extend the functionality of a VisualElement I would have to create my own VisualElement which inherits from the original with the new functionality. This increases the complexity of creating UI as there will be a lot of duplicate VisualElements with near similar names and functionality.
The Solution
I propose a similar the Custom Property Drawer, in which you define the valid VisualElement type and the name of the Attribute.
[CustomUXMLAttribute(Typeof(UnityEngine.UIElements.Label), "format")]
The class to which the attribute is donned upon, will handle the same functions as UxmlTraits for the VisualElement (in the above example label). I suspect this should inherit from a given class/interface which defines the required function calls.
[CustomUXMLAttribute(Typeof(UnityEngine.UIElements.Label), "format")]
public new class FormatCustomUxmlTraits : CustomUxmlTraits
{
UxmlStringAttributeDescription AttributeDescription => new UxmlStringAttributeDescription { name = "format" };
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
var value = AttributeDescription.GetValueFromBag(bag, cc);
if(value == "toLower")
(UnityEngine.UIElements.Label)ve.text = (UnityEngine.UIElements.Label)ve.text.ToLower();
}
}
}
Here is an example of a CustomUXMLAttribute which mutates the text string of a label depending on what the value is set to.
The UXML would be defined:
<engine:Label text="TeXt" format = "toLower"/>
I hope you enjoy and consider my proposal. I have purposefully tried to make the example as simple as possible, I am aware the user could just define the text as lowercase. The use case, which lead me to this feature request would be more advanced which would include fetching and processing data which would then mutate values of VisualElements.
Maybe the init of CustomUxmlTraits
could just pass the value into it, as the name is already defined in the attribute parameters, I just wanted to keep the whole system familiar to the current coding style
Thank you in advance