Steps
Add an UI Document via the Hierarchy tab
Add a Template on to UI Document
Add a UI Toolkit Element via the Hierarchy tab
Link UI Document to the UI Toolkit Component (if it has not been automatically assigned)
Enter the selector wanted in the selector field
Enable events you need
Assign listeners to each events
Result
Before | After |
---|
| |
using SoyWar.UIToolkit;
using SoyWar.UIToolkit.Elements;
using UnityEngine;
using UnityEngine.UIElements;
public class MyComponent : MonoBehaviour
{
[SerializeField] private UIDocument _document;
[SerializeField] private LabelComponent _labelComponent;
[SerializeField] private TextFieldComponent _textFieldComponent;
public void Start()
{
// Create Selectors via the Selector Builder
Selector labelSelector = Selector.Builder(className: "element").Descendants<Label>().Build();
Selector textFieldSelector = Selector.Builder("header").Descendants<TextField>().Build();
// Or via the Selector Parser
Selector labelSelector = Selector.Parse(".element Label");
Selector textFieldSelector = Selector.Parse("#header TextField");
// Link the UIDocument to the components
_labelComponent.Document = _document;
_textFieldComponent.Document = _document;
// Link the created selectors to the components
_labelComponent.Selector = labelSelector;
_textFieldComponent.Selector = textFieldSelector;
// Use an Unity Event (like OnValueChangedEvent) to assign a Listener
_textFieldComponent.OnValueChangedEvent.AddListener(value => _labelComponent.SetText(value));
}
}