Search Unity

Dynamically Added UI Doesn't Get Affected by Layout Groups and Content Size Fitters

Discussion in 'UGUI & TextMesh Pro' started by IcyRagemora, Sep 16, 2015.

  1. IcyRagemora

    IcyRagemora

    Joined:
    Sep 16, 2015
    Posts:
    2
    I'm trying to add a panel prefab to a content panel during runtime, but it doesn't seem to get affected by the layout groups and content size fitters. If I place some panel prefabs in the content panel and then run the app, the panel prefabs are layout properly.

    The screenshots show the setup I have for the layout.

    Here is my code:
    GameObjectchatMessagePanelClone = Instantiate(chatMessagePanel) asGameObject;
    chatMessagePanelClone.transform.SetParent(this.gameObject.transform, false);
    Image[] images = chatMessagePanelClone.transform.GetComponentsInChildren<Image>();
    ImagebadgeIconImage = images[0];
    foreach(Imageimageinimages)
    {
    if(image.transform.parent == chatMessagePanelClone.transform)
    {
    badgeIconImage = image;
    }
    }
    SpritebadgeIconSprite;
    AccountDatabase.GetAccountImageBasedOnUserName(refsender, outbadgeIconSprite);
    badgeIconImage.sprite = badgeIconSprite;
    TextuserName = chatMessagePanelClone.GetComponentInChildren<Text>();
    userName.text = message;
    //Settheparentofthemessagepaneltothechatpanelthatactsasthecontentpanel
    //forchat
    LayoutRebuilder.MarkLayoutForRebuild(chatMessagePanel.GetComponent<RectTransform>());

    Am I missing something? My layout for dynamically added UI seems to be messed up for iOS platform.
     

    Attached Files:

  2. IcyRagemora

    IcyRagemora

    Joined:
    Sep 16, 2015
    Posts:
    2
    I figured it out! You need to use this: LayoutRebuilder.ForceRebuildLayoutImmediate(this.transformasRectTransform);
    That way the layout will be applied immediately to the dynamically add UI before becoming visible to the user.
     
    BAIZOR and greengremline like this.
  3. greengremline

    greengremline

    Joined:
    Sep 16, 2015
    Posts:
    183
    Oh man thank you so much! You have no idea how long I've spent trying to get a similar issue in my project to work, searching Layout information on the net did not bring up LayoutRebuilder at all (or at least the way I was searching). Much appreciated!