Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Kyllthor · May 29 at 06:29 PM · uitoolkit

(UI Toolkit) How to create a ListView ?


I cannot display the VisualElements of the list, I succeed in displaying only Labels.


I failed to display a VisualElements which contains several elements, nothing works so far.


 private ListView LoadDataTable()
 {
     //####################### if i change that with list<Label> its works ##################
     List<VisualElement> items = new List<VisualElement>();
     for (int i = 1; i <= 100; i++)
     {
         VisualElement box = new VisualElement();
         box.tooltip = "NEED HELP";
         box.style.width = 4000;
         box.style.flexDirection = FlexDirection.Row;
         for (int col = 0; col < 10; col++)
         {
             Label lbl = new Label();
             lbl.text = "*****[" + (col * 1000000) + "]*****";
             //lbl.style.width = 50;
             box.Add(lbl);
         }
         items.Add(box);
     }
     //#############################################################################
 
     Func<VisualElement> makeItem = () => new VisualElement();
 
     //!!!!!!!!!!!!!!!!!!!!!!!!!! this doesn't works !!!!!!!!!!!!!!!!!!!
     Action<VisualElement, int> bindItem = (e, i) => e = items[i];
     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
     // Provide the list view with an explict height for every row
     // so it can calculate how many items to actually display
     const int itemHeight = 30;
 
     var listView = new ListView(items, itemHeight, makeItem, bindItem);
 
     listView.showAlternatingRowBackgrounds = AlternatingRowBackground.All;
     listView.showBorder = true;
     listView.selectionType = SelectionType.Multiple;
     listView.onItemsChosen += objects => Debug.Log(objects);
     listView.style.flexGrow = 1.0f;
 
     return listView;
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew-lukasik · 5 days ago


You got some things all wrong here. This is how you do it properly:


 void OnEnable ()
 {
     // lets (pretend to) load the high score data:
     var highScoreData = new List<HighScoreEntry>();
     for( int i=0 ; i<100 ; i++ )
     {
         var entry = new HighScoreEntry{
             name = $"Turbo Lechita #{i}" ,
             score = Random.Range( 0 , 1000000 )
         };
         highScoreData.Add( entry );
     }
 
     // lets bind ui:
     var root = GetComponent<UIDocument>().rootVisualElement;
     {
         ListView highScoreView = LoadDataTable( highScoreData );
         root.Add( highScoreView );
     }
 }
 
 ListView LoadDataTable ( List<HighScoreEntry> data )
 {
     var listView = new ListView();
     {
         listView.itemsSource = data;
         listView.itemHeight = 80;
         listView.makeItem = onMakeItem;
         listView.bindItem = onBindItem;
         listView.onItemsChosen += onItemsChosen;
         listView.selectionType = SelectionType.Multiple;
         listView.style.flexGrow = 1.0f;
     }
     return listView;
 
     VisualElement onMakeItem ()
     {
         var box = new VisualElement();
         {
             var label = new Label();
             label.name = "name-field";
             box.Add( label );
         }
         {
             var label = new Label();
             label.name = "score-field";
             box.Add( label );
         }
         return box;
     }
     void onBindItem ( VisualElement root , int i )
     {
         var entry = data[i];
         {
             var nameField = root.Q<Label>("name-field");
             nameField.text = entry.name;
         }
         {
             var scoreField = root.Q<Label>("score-field");
             scoreField.text = $"$$$ {entry.score} $$$";
         }
     }
     void onItemsChosen ( IEnumerable<object> objects )
     {
         Debug.Log( $"{nameof(onItemsChosen)}()" );
         foreach( HighScoreEntry entry in objects )
             Debug.Log( $"\t ( {entry.name} : {entry.score} ) selected" );
     }
 }
 
 public struct HighScoreEntry
 {
     public string name;
     public int score;
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

186 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to put downloaded images into UIButton using UIToolkit 0 Answers

How can you use UQuery to access child nodes? 1 Answer

The "Event Trigger" Component makes more process to the game that use a collider and use this in the script when I use "onMouseDown" function? 0 Answers

Extend ui toolkit label control 1 Answer

UI Builder is missing several standard library options 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges