Search Unity

Feature Request VisualElement physical and logical hierarchies

Discussion in 'UI Toolkit' started by Hellfim, May 30, 2023.

  1. Hellfim

    Hellfim

    Joined:
    Sep 11, 2014
    Posts:
    123
    Hello! I've been working with UI Toolkit for quite a while now and believe it's a very handy tool (though obviously not finished yet).

    In my work I mostly found my self wanting to access element's physical hierarchy and not logical one. I understand that physical one is accessable through visualElement.hierarchy (alongside with visualElement.hierarchy.parent and so on) property and logical is default for childCount, parent and indexer properties.

    It's hard for me to imagine the reasoning why it was chosen to make logical hierarchy default and have an additional property for accessing physical one. It's more intuitive for the element to return childCount as actual child count rather than some truncated amount of 'logical' children. If I want that I can access contentContainer.childCount property to directly specify that I wan't to know the amount of children in the 'logical' container.

    So my feature request is to swap default hierarchy before it's too late.
    It would also be cool to know actual pros of having logical hierarchy as default one if this request is denied.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,927
    This would've had to be a decision made when UI toolkit was first started. It's way, way too late to make a change like this and completely wreck everyone's code bases.

    And I think the reason is for API reasons. Most Visual Elements - particularly the ones Unity provides - wrap around a number of child visual elements we shouldn't have to deal with. When we add/remove elements, they're going to the content container (this 'logical' heirarchy you speak of), and we generally only care about these specific elements. The rest is all fluff in between that should be invisible to the general use of these elements and their API's.

    I think Unity made the right choice here.

    It was my first instinct to reach to the heirarchy when starting out with UI toolkit, but this was leading to a lot of rigid code. The more I leaned into how Unity approached things, making visual elements that managed their own children with clean APIs, sped things up as I had a lot of reusable visual elements I could use anywhere.
     
  3. Hellfim

    Hellfim

    Joined:
    Sep 11, 2014
    Posts:
    123
    Thank you for the detailes response.
    Maybe it's just not that intuitive to the UI Toolkit newcommers such as myself. Hope I'll also understand this some day.