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 Grid Layout Group equivalent in UI Toolkit

Discussion in 'UI Toolkit' started by Kan15hkSMT, Jul 29, 2020.

  1. Kan15hkSMT

    Kan15hkSMT

    Joined:
    Nov 13, 2019
    Posts:
    37
    I want to make a grid of buttons for runtime with X rows and Y columns that I populate from code.

    Any recommendations on how I can do that?

    I tried to look at scroll view but I don't think I can specify the number of rows and columns.
     
  2. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    225
    With flexbox there are some approaches as how to make grid, for example:
    https://css-tricks.com/dont-overthink-flexbox-grids/

    If your button sizes and container are always a fixed size than it is exceptionally easy, just set flex wrap mode for main container. If you need a bit more flexibility for proper UI scaling (mobile friendly) then some works need to be done..
     
    Kan15hkSMT likes this.
  3. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    515
    There is no built in solution to create a grid of elements as of today. All dynamic layout is done using flexbox from the css specification. Thanks Digika for the link!


    You can still manually place the elements using absolute positioning. Instead of setting dimensions in pixels, it is possible to assign a percentage by creating the length using
    Length.Percent(value);
     
  4. Seith

    Seith

    Joined:
    Nov 3, 2012
    Posts:
    755
    @SimonDufour Hi Simon, I believe UI Toolkit will be out of preview (so production-ready) in 2021.1 so I was wondering if it's going to include any grid layout at all?
     
  5. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    225
    Not the person you were referring to I can answer you that easily - it wont. and even if we assume it will at some point - there are many, many years before that will happen.
     
  6. Seith

    Seith

    Joined:
    Nov 3, 2012
    Posts:
    755
    Oh. And what is the reasoning behind your answer, may I ask?
     
  7. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    225
    This question about GRID layout came up multiple times and the answer was clear. Besides, they are using
    https://github.com/facebook/yoga
    and it is flexbox only
     
  8. SimonDufour

    SimonDufour

    Unity Technologies

    Joined:
    Jun 30, 2020
    Posts:
    515
    While I can clearly see why would the grid layout be beneficial to speed up some layout, nothing should prevent you to implement a grid without it.

    I would suggest that you do not wait and ask for a more specific question if you encounter any difficulty obtaining the results you want.
     
  9. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    225
    There is a difference between having an existing stack that has RFC and standardized feature set and reinventing the wheel. Yes, you can do a lot of stuff with flex, but grid wouldnt exist in a first place if flex covered all possible needs and was perfect.

    Besides that, GRID layout engine offers bunch of features flexbox simply cannot into and never will into by design. Is it better? That depends on your needs, but saying that you can have a drop-in replacement is disingenuous at best. I'm not saying it is critical to UTK, but it is a waste, because grid is no longer experimental, it wasnt experimental even 2-3 years ago. There was no reason to skip it when big minds at Unity had a hard thonk back then: "what layouts should we have as a base".
     
    PixelLifetime and mikevargas like this.
  10. mikevargas

    mikevargas

    Joined:
    Aug 17, 2019
    Posts:
    22
    I'm curious to hear if you come up with a solution that doesn't require reinventing the wheel, @Digika. I am uncertain why Unity chose this particular UI framework as the basis for UI Elements/Toolkit, but I imagine it was a project of someone who happened to be familiar with yoga/Flex (but not css-grid for some reason?). It's completely infeasible for me to develop my own custom UI controls from scratch whenever I need to implement a simple Editor feature for my team (e.g. any involving a grid). It turns a 1-week task into a several week task (or more). And it's further complicated by the fact that the docs don't seem to have had significant updates in years.
     
  11. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    225
    All I'm saying is that they played on the safe side. CSS group been advocating for GRID last few years but people stuck on less intuitive flexbox with the same superstitions - "it is more mature, more stable". Grid was "born stable" because it was built on the foundation of flex, just addresses its shortcomings.
     
  12. mshem

    mshem

    Joined:
    Nov 30, 2016
    Posts:
    4
    if i need to, ill probably end up writing a custom visualelement adaptation of this:
     
  13. BillyWM

    BillyWM

    Joined:
    Dec 29, 2018
    Posts:
    14
    Nothing should prevent you from making a grid with floats and clears either! That's why Bootstrap came around; part of a whole cottage industry of "grid systems" from the pre-flexbox era. It's a question of ease-of-use not of technically possible. That's why UI Toolkit exists at all

    CSS Grid has been widely supported since 2017. 5 years. 1/6th of the web's entire existence. Even IE 10 - TEN - supports the earlier version of the standard. Yes, flexbox is pretty powerful, but it's just regressive for Unity to set their sights that low.

    Plus UGUI's grid is one of the most widely-mocked, absolutely horrible parts of Unity. You'd think they'd want to redeem themselves :)
     
    Last edited: Jun 3, 2022
  14. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
    +1 for a grid layout :)
     
  15. MarcGFJ

    MarcGFJ

    Joined:
    May 16, 2019
    Posts:
    19
    Last edited: Jan 10, 2023