38 lines
1.2 KiB
C#
Executable File
38 lines
1.2 KiB
C#
Executable File
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<UIDocument>();
|
|
|
|
_button = uiDocument.rootVisualElement.Q("button") as Button;
|
|
_toggle = uiDocument.rootVisualElement.Q("toggle") as Toggle;
|
|
|
|
_button.RegisterCallback<ClickEvent>(PrintClickMessage);
|
|
|
|
var _inputFields = uiDocument.rootVisualElement.Q("input-message");
|
|
_inputFields.RegisterCallback<ChangeEvent<string>>(InputMessage);
|
|
}
|
|
|
|
private void OnDisable() {
|
|
_button.UnregisterCallback<ClickEvent>(PrintClickMessage);
|
|
}
|
|
|
|
private void PrintClickMessage(ClickEvent evt) {
|
|
++_clickCount;
|
|
|
|
Debug.Log($"{"button"} was clicked!" +
|
|
(_toggle.value ? " Count: " + _clickCount : ""));
|
|
}
|
|
|
|
public static void InputMessage(ChangeEvent<string> evt) {
|
|
Debug.Log($"{evt.newValue} -> {evt.target}");
|
|
}
|
|
} |