Search Unity

ContentContainer not applying in custom visualElement

Discussion in 'UI Toolkit' started by hvacek22, Feb 25, 2021.

  1. hvacek22

    hvacek22

    Joined:
    Apr 24, 2018
    Posts:
    28
    Hello, I have my own Element called SmallModal
    Code (CSharp):
    1. public class SmallModal : VisualElement
    2. {
    3.     public new class UxmlFactory : UxmlFactory<SmallModal> { }
    4.    
    5.     public SmallModal()
    6.     {
    7.         Init();
    8.     }
    9.  
    10.     private void Init()
    11.     {
    12.         var asset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/_Project/Components/Core/Modals/SmallModal/SmallModal.uxml");
    13.         asset.CloneTree(this);
    14.     }
    15. }
    16.  
    next I need to use it as a container for content, I have applied content-container="true", but it doesnt work
    upload_2021-2-25_18-32-25.png

    CloseButton is OK, it is in the SmallModal.uxml, but If I use it as an own component with content -

    upload_2021-2-25_18-33-27.png

    result:
    upload_2021-2-25_18-33-56.png

    What Am I doing wrong please?
     
  2. hvacek22

    hvacek22

    Joined:
    Apr 24, 2018
    Posts:
    28
    Any help please? The content-container attribute is not applied. The inserted elements are displaying on bad parent
     
  3. Midiphony-panda

    Midiphony-panda

    Joined:
    Feb 10, 2020
    Posts:
    243
    The "modalPanelContainer" is indeed the content-container of the SmallModal.uxml template.

    However, I think the issue is that nothing overrides the contentContainer of your SmallModal custom control instance.

    On C# side, at Init, try to set the contentContainer property to the cloned VisualElement added to your SmallModal custom control. I am curious to know if the contentContainer properties will chain properly, because :
    • the contentContainer of the SmallModal custom constrol instance would be the SmallModal.uxml template instance
    • the contentContainer of the SmallModal.uxml template instance is "modalPanelContainer"

    If it doesn't work out of the box, you might need to set the contentContainer explicitly to your "modalPanelContainer" VisualElement, that you can query with .Q("modalPanelContainer").