Search Unity

[RELEASED] UI Table

Discussion in 'Assets and Asset Store' started by bourriquet, Mar 14, 2019.

  1. bourriquet

    bourriquet

    Joined:
    Jul 17, 2012
    Posts:
    115
    If your XML is in Resources, you can access it with Resources.Load()
    Then you can convert the TextAsset into a StringReader.
    So that's:

    Code (CSharp):
    1.     public class XML2ListConverter : MonoBehaviour
    2.  
    3.         public List<MyClass> myList;
    4.  
    5.         void Start()
    6.         {
    7.             XmlSerializer serializer = new XmlSerializer(typeof(List<MyClass>));
    8.             TextAsset textAsset = Resources.Load("path_to_xml");
    9.             using (var reader = new StringReader(textAsset.text))
    10.             {
    11.                 myList = (List<MyClass>)serializer.Deserialize(reader);
    12.             }
    13.         }
    14.  
    15.     }
    Keep in mind the path_to_xml is relative to the containing Resources folder.
     
  2. BennyTan

    BennyTan

    Joined:
    Jan 23, 2014
    Posts:
    41
    Hi, before i buy this plugin, could i check, there seems to be a fixed list for the cell types. Is there a dropdown list option for the cell types?
     
  3. bourriquet

    bourriquet

    Joined:
    Jul 17, 2012
    Posts:
    115
    Hi @BennyTan
    Yes, there is a dropdown list cell type for enum fields.
    The cell types list is extendable. You can inherit the CellType class and create new cell type objects. They will automatically be added to the list of available types.
     
  4. Friction

    Friction

    Joined:
    Jan 6, 2015
    Posts:
    17
    Hello @bourriquet ,
    Thank you for this great asset.
    I want to add columns at runtime (via script), is it possible?
     
  5. bourriquet

    bourriquet

    Joined:
    Jul 17, 2012
    Posts:
    115
    Hi @Friction
    The code isn't really thought for that, but it still works. You have to add a small change though: in TableColumnInfo.cs, add a constructor as such:

    Code (CSharp):
    1. public TableColumnInfo(Table table, string fieldName, float width, bool useRelativeWidth, TableCell cellPrefab, string columnTitle)
    2. {
    3.     this.table = table;
    4.     this.fieldName = fieldName;
    5.     this.width = width;
    6.     this.useRelativeWidth = useRelativeWidth;
    7.     this.cellPrefab = cellPrefab;
    8.     this.columnTitle = columnTitle;
    9. }
    then, in your code, you can do:

    Code (CSharp):
    1. table.columns.Add(
    2.     new TableColumnInfo(table, fieldName, width, isRelativeWidth, cellPrefab, columnName));
    I'll think about a clean way to support that for the next update.
     
    Friction likes this.
  6. Friction

    Friction

    Joined:
    Jan 6, 2015
    Posts:
    17
    Hello again,

    Thank you for your kind reply. I've one more question.
    Can I add list elements to table as columns?
     
  7. bourriquet

    bourriquet

    Joined:
    Jul 17, 2012
    Posts:
    115
    @Friction
    There is currently no Cell that is adapted to list properties. Lists are by definition of variable size, which means it's unclear how it should be fitted in a column.
    You can, however, code your own custom Cell Type class to fit your needs in that regard.
     
    Friction likes this.
  8. bourriquet

    bourriquet

    Joined:
    Jul 17, 2012
    Posts:
    115
    @Friction
    Actually, if you just want a simple print out of the list as text in a Label Cell, that can be easily done.
    In LabelCell.cs, add

    Code (CSharp):
    1. using System.Linq;
    then replace the UpdateContent function by this one:

    Code (CSharp):
    1. public override void UpdateContent()
    2. {
    3.     if (property == null || property.IsEmpty)
    4.         return;
    5.     object o = property.GetValue(obj);
    6.     if (o == null)
    7.         label.text = "null";
    8.     else if (Table.IsCollection(o.GetType()))
    9.         label.text = string.Join(", ", (o as IEnumerable).OfType<object>().Select(obj => obj.ToString()).ToArray());
    10.     else
    11.         label.text = o.ToString();
    12. }
    That actually totally makes sense so I will add that in an update now.
     
    Friction likes this.
  9. dougdodd

    dougdodd

    Joined:
    Apr 18, 2014
    Posts:
    32
    How do I set cells using TextMeshPro assets? The dropdown list is Text Cell, Input Cell or Input Cell expandable. Can I make a TMP asset and add it to that list?
     
  10. bourriquet

    bourriquet

    Joined:
    Jul 17, 2012
    Posts:
    115
    Hi @dougdodd
    Extract Assets/RuntimeGUITable/TextMeshPro Addings.unitypackage in your project and there will be an additional TMP cell type in the list.
     
  11. vihist

    vihist

    Joined:
    Sep 7, 2017
    Posts:
    3
    The header is hidden when i scroll the table. How to show the header when scroll?
    Thx!
     
  12. bourriquet

    bourriquet

    Joined:
    Jul 17, 2012
    Posts:
    115
    Hi @vihist
    Sorry a scrollable table with fixed headers line feature is not yet available. I'll work on this for the next update.
    Thanks for the suggestion.
     
  13. misato

    misato

    Joined:
    Aug 5, 2012
    Posts:
    6
    Hi I am having a table with selectable rows but it seems that I can select everything (including headers) except for the last row of the table. What am I missing?
    It is a simple table with an image and some text per row. Also not scrollable, and always having 10 rows (so not even super big)

    EDIT: alright I found the issue but I am not sure this will break anything else:

    In the Table.cs script, at line 224 function HandleInputs you have this:

    Code (CSharp):
    1. if (Input.GetKeyUp(KeyCode.UpArrow))
    2.     selectedRow = Mathf.Max(selectedRow - 1, 0);
    3. else if (Input.GetKeyUp(KeyCode.DownArrow))
    4.     selectedRow = Mathf.Min(selectedRow + 1, elementCount-1);
    it should be changed to:

    Code (CSharp):
    1. if (Input.GetKeyUp(KeyCode.UpArrow))
    2.     selectedRow = Mathf.Max(selectedRow - 1, 1);
    3. else if (Input.GetKeyUp(KeyCode.DownArrow))
    4.     selectedRow = Mathf.Min(selectedRow + 1, elementCount);
    The reason for this change is that row 0 is the header of the table. I don't know if there is a way to disable headers. In that case it shouldbe checked here if there is header or not and change those numbers acordingly.
     
    Last edited: Dec 13, 2019
  14. bourriquet

    bourriquet

    Joined:
    Jul 17, 2012
    Posts:
    115
    Hi @misato
    Wow. Thanks for fixing it for me!
    There is no way to disable headers so it's a plain and simple bug. This has never been reported before, so I guess few people use the selectable feature.
    I'll upload the fix this week.
    Thanks again!
     
  15. misato

    misato

    Joined:
    Aug 5, 2012
    Posts:
    6
    Not a problem! :D At the end I needed to disable that code because in my case I use Rewired and then it got double presses. I wonder if there's a better way to check if you are using the standard Unity input or inControl/Rewired (I'm still fairly new to Unity). But in any case I'm happy to help!

    EDIT: I forgot to thank you for this wonderful asset!!! It really saved me a lot of time making a table for a leaderboard in my game.
     
    Last edited: Dec 20, 2019
  16. nekkoriv

    nekkoriv

    Joined:
    Jul 30, 2018
    Posts:
    1
    Hi Bourriquet, is there a way to have the collection properties be rows and the data be columns? Basically, are we able to make horizontal tables? That would be immensely useful in solving the problem I purchased this in hopes to solve.
     
  17. bourriquet

    bourriquet

    Joined:
    Jul 17, 2012
    Posts:
    115
    Hi @nekkoriv
    Unfortunately that is not currently possible. But that's a very good idea, so I'll be working on it. If your problem can wait for maybe a week (Unity's approval can take 2-3 days), I'll let you know.
     
  18. Jan85

    Jan85

    Joined:
    Aug 18, 2013
    Posts:
    2
    Hello Bourriquet,
    I got your UI Table and enjoy how simple it is to use it even for beginner programmer as myself.
    I managed to connect the table to a list that I am populating from SQLite database.

    What I am struggling with is reading which row was selected by the user (ideally reading the ID column or at least row index) and trigger click event so I can work with the information further in the application.

    Additionally I would also need the table to be scrollable with the header row locked on top always showing even after the table is scrolled down – someone else here was already asking for the same thing.

    In attachment is the code I use to fill the table. I need to read the “ID” when user click a line and trigger an event. And ideally have the first line clicked when table is loaded. Could you help me with that please?

    Thank you in advance
    Jan
     

    Attached Files:

  19. bourriquet

    bourriquet

    Joined:
    Jul 17, 2012
    Posts:
    115
    Hi @Jan85
    For reading the selected row, use Table.SelectedRow. The user can only select entire rows. If you want to know the column selected, you will have to work with Unity UI's selection system.
    Scrollable table with fixed header is not supported. That's a good idea (arguably more useful than the current scrollable option actually) but I don't think I can do it in a short time. In the meantime, you could workaround by duplicating the table:
    - one for the headers, populated with an empty collection and set non-scrollable
    - one for the data, with the real collection and set to scrollable, with the headers hidden in some way
    Let me know if you have further questions.
     
  20. bourriquet

    bourriquet

    Joined:
    Jul 17, 2012
    Posts:
    115
    Hi @nekkoriv
    The 1.6 version, that was just approved, contains a "Horizontal" option. Let me know if it fills your needs.
     
  21. Jan85

    Jan85

    Joined:
    Aug 18, 2013
    Posts:
    2

    Thank you I appreciate your help. I managed to get the ID from the table and update it on click. And for scrolling I am using table with header covered by text boxes with header names. It’s not optimal but works fine for me.
     
unityunity