Search Unity

Is it possible to create an uxml template that can accept child elements like ScrollView?

Discussion in 'UI Toolkit' started by mutsuyuki, Oct 1, 2021.

  1. mutsuyuki

    mutsuyuki

    Joined:
    Apr 16, 2015
    Posts:
    19
    I often want to create a uxml template for a container and put arbitrary child elements in it.
    In other words, I want to create custom template like ScrollView.

    The following is an example.

    The following is pseudo-code, so it just shows the concept.
    Is it possible to include content on the parent element side like this?

    -- child template --
    Code (Container.uxml):
    1.  
    2. <VisualElement>
    3.     <Label text="header text"/>
    4.  
    5.     <Slot />   <=== replace by parent (three Label elements will be inserted)
    6.  
    7.     <Label text="footer text"/>
    8. </VisualElement>
    9.  
    -- parent template--
    Code (Root.uxml):
    1.  
    2. <Template src="Container.uxml" name="Container" />
    3. <Instance name="page1" template="Container">
    4.     <Label text="awesome contents 1" />
    5.     <Label text="awesome contents 2" />
    6.     <Label text="awesome contents 3" />
    7. </Instance>
    8.  
     
    Last edited: Oct 1, 2021
  2. HugoBD-Unity

    HugoBD-Unity

    Unity Technologies

    Joined:
    May 14, 2018
    Posts:
    499
    Hi @mutsuyuki !

    I am not sure if it's what you are looking for, but have you looked at the "contentContainer". The documentation is pretty poor, but in short, it allows to specify another VisualElement into which child elements are added.