using UnityEngine; using UnityEngine.UIElements; public class SimpleRuntimeUi : MonoBehaviour { private Button _button; private Toggle _toggle; private int _clickCount; //Add logic that interacts with the UI controls in the `OnEnable` methods private void OnEnable() { // The UXML is already instantiated by the UIDocument component var uiDocument = GetComponent(); _button = uiDocument.rootVisualElement.Q("button") as Button; _toggle = uiDocument.rootVisualElement.Q("toggle") as Toggle; _button.RegisterCallback(PrintClickMessage); var _inputFields = uiDocument.rootVisualElement.Q("input-message"); _inputFields.RegisterCallback>(InputMessage); } private void OnDisable() { _button.UnregisterCallback(PrintClickMessage); } private void PrintClickMessage(ClickEvent evt) { ++_clickCount; Debug.Log($"{"button"} was clicked!" + (_toggle.value ? " Count: " + _clickCount : "")); } public static void InputMessage(ChangeEvent evt) { Debug.Log($"{evt.newValue} -> {evt.target}"); } }