Hi,
I would to make a listView, but I can’t add Label like in the example.
I mean, the numbers doesn’t show in the window and in the debugger only Hello world.
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using System.Collections.Generic;
using System;
public class hip : EditorWindow
{
[MenuItem("testo/hip")]
public static void ShowExample()
{
hip wnd = GetWindow<hip>();
wnd.titleContent = new GUIContent("hip");
}
public void CreateGUI()
{
var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/hip.uxml");
VisualElement labelFromUXML = visualTree.Instantiate();
rootVisualElement.Add(labelFromUXML);
createListView();
}
void createListView()
{
// Create some list of data, here simply numbers in interval [1, 1000]
const int itemCount = 1000;
var items = new List<string>(itemCount);
for (int i = 1; i <= itemCount; i++)
items.Add(i.ToString());
Func<VisualElement> makeItem = () => new Label();
Action<VisualElement, int> bindItem = (e, i) => (e as Label).text = items[i];
ListView listView = rootVisualElement.Q<ListView>(className: "the-uxml-listview");
//var listView = rootVisualElement.Q<ListView>();
listView.makeItem = makeItem;
listView.bindItem = bindItem;
listView.itemsSource = items;
listView.selectionType = SelectionType.Multiple;
// Callback invoked when the user double clicks an item
listView.onItemsChosen += Debug.Log;
// Callback invoked when the user changes the selection inside the ListView
listView.onSelectionChange += Debug.Log;
}
}
<?xml version="1.0" encoding="utf-8"?>
<engine:UXML
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:engine="UnityEngine.UIElements"
xmlns:editor="UnityEditor.UIElements"
xsi:noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd"
>
<engine:Label text="Hello World! From UXML" />
<engine:ListView class="the-uxml-listview" />
</engine:UXML>
Label {
font-size: 20px;
-unity-font-style: bold;
color: rgb(68, 138, 255);
}
.the-uxml-listview {
/* Provide the list view with an explict height for every row
so it can calculate how many items to actually display */
--unity-item-height: 16;
height: 200px;
}