Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
4

Answer by steven_scottyd · May 11, 2020 at 12:41 PM

I know this is an old thread, but for anyone still wanting an answer you can force a LayoutGroup to recalculate if you call LayoutRebuilder.ForceRebuildLayoutImmediate(_rootRectTransform); where "_rootRectTransform" is the RectTransform on the Gameobject that contains the LayoutGroup component (or any parent in the hierarchy above it).

This method essentially "refreshes" the root RectTransform and all it's children, which forces the LayoutGroup to recalculate where each of its elements should be.

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
1

Answer by ilya_ca · Oct 27, 2016 at 10:30 PM

Use RectTransform.SetAsFirstSibling() or RectTransform.SetAsLastSibling() after adding new elements. This will reorganize the LayoutGroup properly.

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 FMaz · Mar 18, 2018 at 01:49 AM 0
Share

As a beginner reading this response, it is not detailled enough. Calling only "RectTransform.SetAsLastSibbling()" does not bring the last instanciated object on top.

In my case, buttonInstance.transform.SetAsLastSibbling(); does not bring the UI element to the front and I end up dragging (and dropping) elements under their slots/place holders.

avatar image
0

Answer by David_Biggs · Feb 27, 2015 at 02:40 AM

Try adding a LayoutElement compoment to your new objects

         LayoutElement layout = nametag.AddComponent<LayoutElement>();
         layout.minHeight = 100f;
         layout.preferredWidth = 600f;
         layout.preferredHeight = 100f;
         layout.flexibleHeight = 0;

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 T_Lavell · Feb 27, 2015 at 03:34 AM 0
Share

Thanks for the suggestion, I gave it a shot just now (without my 'invoke' fix); not only did the layout group not organize the elements, but the preferredWidth, preferredHeight etc traits did not update! When I altered the screen size / reordered the siblings (with the game running), the 'new' width and height then updated.

avatar image
0

Answer by Guy-Corbett · Sep 13, 2015 at 02:04 PM

I don't know if it's the same issue but I've encountered something similar as has this person;

http://answers.unity3d.com/answers/1066088/view.html

I've posted a response to that question which could help.

Comment
Add comment · Show 4 · 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 infosekr · Feb 24, 2016 at 08:07 AM 0
Share

This link seems to be dead.

avatar image jeromeWork · May 27, 2016 at 09:54 AM 0
Share

@Guy Corbett As has been mentioned, this page seems to have gone awol... Any chance of a hint of what was covered? I'm having much the same problem and can't find a solution. Thanks

avatar image Guy-Corbett jeromeWork · May 27, 2016 at 10:17 AM 0
Share

Not sure what happened there. I think this is the right link;

http://answers.unity3d.com/questions/876592/ui-instantiation-layout-group-does-not-work.html#comment-1066088

avatar image jeromeWork Guy-Corbett · May 27, 2016 at 12:40 PM 0
Share

Thanks! sadly not the answer for me but much appreciate your quick response :)

avatar image
0

Answer by Curb · Feb 24, 2016 at 08:53 AM

I'm not at my pc atm but have you tried Canvas.ForceUpdateCanvases()?

If that doesn't work check if layoutgroup has a public function you could call named somewhat like ForceUpdate().

I had a somewhat similar problem with inputfields not updating the visible text when adding characters through code. The line would get longer than the inputfield having characters disappear instead of the field scrolling with the caretposition. Some sort of forceupdate function fixed that for me.

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
  • 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.

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

How do I layout dynamically instantiated UGUI components? 1 Answer

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

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

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

Layout Group not working when instantiate from prefab 1 Answer


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