Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Beginner in UI ToolKit - Sample ListView

Discussion in 'Editor & General Support' started by Malavia, Mar 23, 2021.

  1. Malavia

    Malavia

    Joined:
    Aug 28, 2019
    Posts:
    10
    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.


    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.UIElements;
    4. using System.Collections.Generic;
    5. using System;
    6.  
    7.  
    8. public class hip : EditorWindow
    9. {
    10.     [MenuItem("testo/hip")]
    11.     public static void ShowExample()
    12.     {
    13.         hip wnd = GetWindow<hip>();
    14.         wnd.titleContent = new GUIContent("hip");
    15.     }
    16.  
    17.     public void CreateGUI()
    18.     {
    19.         var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/hip.uxml");
    20.         VisualElement labelFromUXML = visualTree.Instantiate();
    21.         rootVisualElement.Add(labelFromUXML);
    22.  
    23.         createListView();
    24.     }
    25.  
    26.     void createListView()
    27.     {
    28.  
    29.         // Create some list of data, here simply numbers in interval [1, 1000]
    30.         const int itemCount = 1000;
    31.         var items = new List<string>(itemCount);
    32.         for (int i = 1; i <= itemCount; i++)
    33.             items.Add(i.ToString());
    34.  
    35.         Func<VisualElement> makeItem = () => new Label();
    36.         Action<VisualElement, int> bindItem = (e, i) => (e as Label).text = items[i];
    37.  
    38.         ListView listView = rootVisualElement.Q<ListView>(className: "the-uxml-listview");
    39.         //var listView = rootVisualElement.Q<ListView>();
    40.         listView.makeItem = makeItem;
    41.         listView.bindItem = bindItem;
    42.         listView.itemsSource = items;
    43.         listView.selectionType = SelectionType.Multiple;
    44.        
    45.         // Callback invoked when the user double clicks an item
    46.         listView.onItemsChosen += Debug.Log;
    47.  
    48.         // Callback invoked when the user changes the selection inside the ListView
    49.         listView.onSelectionChange += Debug.Log;
    50.  
    51.     }
    52. }
    Code (CSharp):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <engine:UXML
    3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4.     xmlns:engine="UnityEngine.UIElements"
    5.     xmlns:editor="UnityEditor.UIElements"
    6.     xsi:noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd"
    7. >
    8.     <engine:Label text="Hello World! From UXML" />
    9.     <engine:ListView class="the-uxml-listview" />
    10. </engine:UXML>
    Code (CSharp):
    1. Label {
    2.     font-size: 20px;
    3.     -unity-font-style: bold;
    4.     color: rgb(68, 138, 255);
    5. }
    6. .the-uxml-listview {
    7.     /* Provide the list view with an explict height for every row
    8.     so it can calculate how many items to actually display */
    9.     --unity-item-height: 16;
    10.     height: 200px;
    11. }
     
  2. Malavia

    Malavia

    Joined:
    Aug 28, 2019
    Posts:
    10
    Sorry, Close the post, I move it.