Search Unity

Help Wanted Custom C# VisualElement children question.

Discussion in 'UI Toolkit' started by Ashfid, Sep 24, 2021.

  1. Ashfid

    Ashfid

    Joined:
    Jun 8, 2019
    Posts:
    16
    Hey all. So, I have a doubt on custom C# Visual Element that adds child when created. Is it not possible for the child element to accept another child element when this custom visual element is dropped in UI Builder? It is grayed out, but can you override that somehow?

    I am trying to create tab container that takes tab buttons that should be under TabButtonGroup child element and other visual elements under Content child element that is placed according to order of tab buttons.
     
  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    475
    I am going to reference a slightly undocumented feature called the contentContainer.

    Basically, overriding the contentContainer property from your custom element class and return the element to act as the parent of anything added with the "Add()" method, including what comes from UXML and thus the UI Builder.

    This is how something like the ScrollView is actually implemented.
     
  3. Ashfid

    Ashfid

    Joined:
    Jun 8, 2019
    Posts:
    16
    That worked beautifully! However, my tab panel element works this way:
    Tab Container
    - TabButtonGroup (this is the contentContainer now to add buttons)
    - TabContents (I can’t add to this because contentContainer takes one element of course)

    Do you suggest atomizing those 2 children and then add it on UI Builder (TabButtonGroup and TabContents are separate components then) or any other way?
     
  4. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    475
    I am not sure if this is what you are asking but you should use use this.hierarchy.Add() to bypass the contentContainer mechanism.
     
unityunity