UI Toolkit ComponentUI Toolkit Component
UI Toolkit ComponentUI Toolkit Component
  • Docs
  • API
  • Changelog
  • Unity Asset Store
  • Bug report
  • Getting Started
  • Essentials
    • How To Import a Demo
    • How To Add a Style Sheet
    • How To Add an Event
    • List of Pseudo Classes
    • List of Events
  • Advanced
    • How To Get a VisualElement
    • How To Find a VisualElement
    • How To Check a VisualElement
    • How To Create a custom Pseudo Class
    • How To Create a custom UI Toolkit Component
    • How To Add a custom UI Toolkit GameObject
Generated by DocFX
(with DiscordFX + SingulinkFX)

Search Results for

    How To Add an Event

    • Interface
    • Code

    Steps

    1. Add an UI Document via the Hierarchy tab

      UI Document added from Hierarchy tab

    2. Add a Template on to UI Document

      Template added to UI Document

    3. Add a UI Toolkit Element via the Hierarchy tab

      UI Toolkit Element added from Hierarchy tab

    4. Link UI Document to the UI Toolkit Component (if it has not been automatically assigned)

      UI Document linked to the UI Toolkit Component

    5. Enter the selector wanted in the selector field

      Text selector filled

    6. Enable events you need

      Event enabled

    7. Assign listeners to each events

      Listener assigned to event

    Result

    BeforeAfter
    Result before the implementationResult after the implementation
    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));
        }
    }