• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by T_Lavell · Feb 27, 2015 at 01:30 AM · uiinstantiateparentlayoutrecttransform

UI LayoutGroup does not organize dynamically instantiated buttons

I have a UI panel with a vertical layout group component and a content size fitter. When I hit play and instantiate the button prefabs, their positioning is not affected by the layout group component. I can see that they are being instantiated as expected, but the layout group doesn't seem motivated to organize them unless I manually resize the screen, or re-order the siblings while the game is paused.

Code sample:

 void InstantiateNametags()  
     {
         VerticalLayoutGroup layoutGroup = hud_left.GetComponentInChildren<VerticalLayoutGroup>(); // Finds the left-side Roster panel.
         layoutGroup.childAlignment = TextAnchor.UpperLeft; // Ensures the names will be left-aligned.
 
         foreach (GameObject fighter in combatantsRosterP1)
         {
             GameObject nametag = GameObject.Instantiate (nametagPrefab) as GameObject;
             nametag.transform.SetParent(layoutGroup.transform, false); // <-- HERE, I suspect, is the culprit.
             nametag.GetComponentInChildren<Text>().text = fighter.name;
             // Additional button functionality here
 
         }
         layoutGroup.gameObject.GetComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize; 
 
         // Code repeats for the right-side hud/roster.
 
     }

How can I gently remind the layout group to perform its function before the player sees disorganized buttons? Could this have to do with the fact that I am using transform.SetParent() rather than accessing the RectTransform component?

Setting the contentsizefitter verticalFit to preferred size (from unconstrained) after creating the buttons was an attempt to fix this issue, as the rect's height was being set to zero without content, but I'm not sure the content size fitter is part of the issue anymore...

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image T_Lavell · Feb 27, 2015 at 01:53 AM 0
Share

The parent 'Roster' gameObject is being instantiated during Start(), immediately before the buttons are being instantiated - I tried using

Invoke("InstantiateNametags", 0.1f);

rather than calling InstantiateNametags() right away, and this has "solved" the problem - though I can't help but feel that there may be a better solution. Shouldn't I be able to instantiate the layoutGroup gameObject, then instantiate some content, and have the layoutGroup recognize what needs to be done?

8 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Sun-Pengfei · Dec 01, 2017 at 06:16 AM

I came cross the same problem, my solution: https://answers.unity.com/questions/1437041/how-to-get-childrens-world-position-in-a-gridlayou.html?childToView=1437433#answer-1437433

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by misher · Apr 01, 2019 at 01:13 PM

Short answer: call LayoutRebuilder.ForceRebuildLayoutImmediate(root rect transform); after you have changed something inside dynamically calculated groups.

I've been struggling for a while with this a long time, usually doing workarounds like using coroutines and wait for the next frame (or a couple of frames) to be sure my new layout is recalculated.

Someone goes with Canvas.ForceUpdateCanvases() but this is rather expensive.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kkl888 · Jan 09, 2020 at 02:09 PM 0
Share

I tried calling both of the methods, still not working

avatar image
0

Answer by Faucheur_de_chattons · May 30, 2021 at 05:57 AM

What worked for me: Instantiate the object then set its parent at the end of the frame:

 GameObject newGameObject = Instantiate(
      prefab,
     new Vector3(),
     Quaternion.identity
 );
 
 // set parent is done at the end of frame to force re-calculate layout
 StartCoroutine(SetTransformParent(newGameObject ));



 private IEnumerator SetTransformParent(GameObject newGameObject){
     yield return new WaitForEndOfFrame();
     RectTransform rectTransform = (RectTransform) newGameObject.transform;
     rectTransform.SetParent(someGameObject);
 }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • ‹
  • 1
  • 2

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

31 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fill empty space in parent in UI Layout 1 Answer

Instantiating UI element on Screen space - Overlay not instantiating exact specified location 0 Answers

How do I layout dynamically instantiated UGUI components? 1 Answer

How to Save Property Values of a Component on a Scriptable Object? 1 Answer

Is there a way to Instantiate a button prefab as a child of a Canvas? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges