83 lines
2.6 KiB
C#
83 lines
2.6 KiB
C#
using UnityEditor;
|
|
using UnityEditor.Animations;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
public class SimpleCustomEditor : EditorWindow
|
|
{
|
|
[MenuItem("Window/UI Toolkit/MyCustomEditor")]
|
|
public static void ShowExample()
|
|
{
|
|
SimpleCustomEditor wnd = GetWindow<SimpleCustomEditor>();
|
|
wnd.titleContent = new GUIContent("MyCustomEditor");
|
|
}
|
|
|
|
[SerializeField]
|
|
private VisualTreeAsset m_UXMLTree = default;
|
|
|
|
private int m_ClickCount = 0;
|
|
|
|
private const string m_ButtonPrefix = "button";
|
|
|
|
public void CreateGUI()
|
|
{
|
|
// Each editor window contains a root VisualElement object
|
|
VisualElement root = rootVisualElement;
|
|
|
|
// VisualElements objects can contain other VisualElement following a tree hierarchy.
|
|
Label label = new Label("These controls were created using C# code.");
|
|
root.Add(label);
|
|
|
|
Button button = new Button();
|
|
button.name = "button3";
|
|
button.text = "This is button3.";
|
|
root.Add(button);
|
|
|
|
Toggle toggle = new Toggle();
|
|
toggle.name = "toggle3";
|
|
toggle.label = "Number?";
|
|
root.Add(toggle);
|
|
|
|
// Instantiate UXML created automatically which is set as the default VisualTreeAsset.
|
|
root.Add(m_UXMLTree.Instantiate());
|
|
|
|
// Import UXML created manually.
|
|
var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/UXML/Editor Simple/SimpleCustomEditor_uxml.uxml");
|
|
VisualElement labelFromUXML_uxml = visualTree.Instantiate();
|
|
root.Add(labelFromUXML_uxml);
|
|
|
|
//Call the event handler
|
|
SetupButtonHandler();
|
|
}
|
|
|
|
//Functions as the event handlers for your button click and number counts
|
|
private void SetupButtonHandler()
|
|
{
|
|
VisualElement root = rootVisualElement;
|
|
|
|
var buttons = root.Query<Button>();
|
|
buttons.ForEach(RegisterHandler);
|
|
}
|
|
|
|
private void RegisterHandler(Button button)
|
|
{
|
|
button.RegisterCallback<ClickEvent>(PrintClickMessage);
|
|
}
|
|
|
|
private void PrintClickMessage(ClickEvent evt)
|
|
{
|
|
VisualElement root = rootVisualElement;
|
|
|
|
++m_ClickCount;
|
|
|
|
//Because of the names we gave the buttons and toggles, we can use the
|
|
//button name to find the toggle name.
|
|
Button button = evt.currentTarget as Button;
|
|
string buttonNumber = button.name.Substring(m_ButtonPrefix.Length);
|
|
string toggleName = "toggle" + buttonNumber;
|
|
Toggle toggle = root.Q<Toggle>(toggleName);
|
|
|
|
Debug.Log("Button was clicked!" +
|
|
(toggle.value ? " Count: " + m_ClickCount : ""));
|
|
}
|
|
} |