using UnityEngine;
using UnityEngine.UIElements;
[UxmlElement]
public partial class TestVisualElement : VisualElement
{
[UxmlAttribute]
public string TestString { get; set; }
public TestVisualElement()
{
Debug.Log(TestString);
}
}
The value is applied after the element is constructed, there’s no way we could do it before. You can use the property setter method to know when its set.
When I use this custom element directly in UI Document it works fine but if I make a template from it and use this template inside the document it returns null again
Thats normal, it resets values in the UI Builder.
You should adjust your code to expect a null string or change the default value for _actionName.
Maybe change it to private string _actionName = "";
I’m not sure I understand how private string _actionName = ""; should help.
I want to configure different strings for each instance of KeyBindingTemplate but it resets all of them to value of _actionName.
The UI Builder will set all the attributes to their default values. The default value for ActionName is currently null. Instead of setting it to “” you would be better checking if the value is null or empty in Initialize and not calling InputSystem.actions.FindAction if it is.
I mean, yeah, there will be no error if I check for null or empty but property is initialized with configured in UIBuilder string and after that gets reseted to default value. So at the end all instances reseted to whatever _actionName is.
This is what I have in Builder:
It’s properly initialized at start
After that it gets reseted without calling property setter
I think it’s a bug or I don’t know something about templates. My uxml structure looks like this:
Everything is fine but if I give SettingsWindow.uxml template some name all properties in custom controls inside it will fail to initialize.
Without name:
With name:
What could be the reason for this behavior?