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 Create a custom Pseudo Class

    using System.Collections.Generic;
    using SoyWar.UIToolkit;
    using SoyWar.UIToolkit.PseudoClasses;
    using UnityEngine;
    using UnityEngine.UIElements;
    
    public class CustomPseudoClass : PseudoClass
    {
        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
        private static void Init()
        {
            // Adds to the list of usable pseudo-classes, which can be called by name in the Selector Parser and Selector Builder.
            PseudoClass.Add<CustomPseudoClass>("custom");
        }
    
        // Checks if the VisualElement is valid according to the rules of this pseudo-class
        public override bool Check(ICollection<VisualElement> roots, VisualElement visualElement, UIToolkitComponent.TypeSearch searchFrom)
        {
            // Example: The VisualElement is only valid if it is a TextField and if it contains the same content as the "Value" variable.
            return visualElement is TextField textField && textField.value == Value;
        }
    
        // Add Event to check validity though the pseudo class manipulator.
        protected override void AddManipulatorCallback(PseudoClassManipulator manipulator)
        {
            // Example: Checks the validity of the VisualElement each time the KeyUpEvent is called
            manipulator.AddEventCallback<KeyUpEvent>(() =>
            {
                // Updates the validity of the VisualElement through the rules of this pseudo class
                manipulator.CheckUpdate();
                
                // Equivalent to
                manipulator.Check = Check(manipulator);
                
                // Or
                manipulator.Check = Check(manipulator.Roots, manipulator.target, manipulator.SearchFrom);
            });
        }
    
    
        // Checks the validity of the pseudo-class when creating the selector
        protected override bool Validate()
        {
            //Example: The pseudo-class is only valid if it has a value
            return Value != null;
        }
    }