Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 deltron1830 · Nov 25, 2016 at 10:29 PM · uicanvaslayout

Get layoutgroup and contentSizeFitter to update immediately

I'm populating a new UI scrollview programatically. The content gameobject of the scrollview has a verticalLayoutGroup and a contentSizeFitter to arrange it. The children of it I'm adding programmatically are prefabs with layoutElement components.

I want to populate it, have the verticalLayoutGroup and contentSizeFitter arrange everything, and then disable them so I can move the elements around (I want to animate them in), all in the same frame.

The only way Ive found to do this has been to add the elements. Then wait one frame, then disable the verticalLayoutGroup and contentSizeFitter.

Ive searched around and tried the following things but they dont work (all the children end up on top of each other, not arranged properly)

 void SetupAnimateIn()
     {
         //Populate content with children elements here

         Canvas.ForceUpdateCanvases();
 
         _ContentSizeFitter.enabled = false;
         _VerticalLayoutGroup.enabled = false;
     }

 void SetupAnimateIn()
     {
         //Populate content with children elements here

         LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)Content.transform );
 
         _ContentSizeFitter.enabled = false;
         _VerticalLayoutGroup.enabled = false;
     }

 void SetupAnimateIn()
     {
         //Populate content with children elements here

         Canvas.ForceUpdateCanvases();
         _VerticalLayoutGroup.CalculateLayoutInputVertical();
         _VerticalLayoutGroup.SetLayoutVertical();
         _ContentSizeFitter.SetLayoutVertical();
         LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)Content.transform );
 
         _ContentSizeFitter.enabled = false;
         _VerticalLayoutGroup.enabled = false;
     }


The only thing that has worked for me so far is..

 void SetupAnimateIn()
     {
         //Populate content with children elements here

          // Wait one frame

         _ContentSizeFitter.enabled = false;
         _VerticalLayoutGroup.enabled = false;
     }


Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by k1ljcore · Jun 12, 2017 at 09:13 AM

This solved the issue for me:

 Canvas.ForceUpdateCanvases();
 _vertLayoutGroup.SetLayoutVertical();
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
7

Answer by MaxWeiChen · Jul 05, 2017 at 04:46 AM

i have a solution, you just need add enable = true

 //Populate content with children elements here
 
 _ContentSizeFitter.enabled = true;
 _ContentSizeFitter.SetLayoutVertical();
 LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)Content.transform );
 _ContentSizeFitter.enabled = false;

@deltron1830

Comment
Add comment · Show 2 · 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 rossadamsm · Apr 24, 2018 at 10:15 AM 0
Share

@$$anonymous$$axWeiChen your solution worked for me just swapping the .enable = true and .enabled = false (turn it off update then turn it back on)

avatar image m0guz · Jun 06, 2018 at 03:14 PM 6
Share

Thanks. Just calling LayoutRebuilder.ForceRebuildLayoutImmediate method solved my issue.

avatar image
3

Answer by Lord-Megabite · Feb 13, 2017 at 12:09 PM

I dont understand what cause this issue, but y have this problem in editor when im create a new object with this component, and for me, this solved the issue:

HorizontalLayoutGroup hlg = itSelf.container.GetComponent(); hlg.enabled = false; hlg.enabled = true;

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 drHogan · May 02, 2017 at 11:10 AM 0
Share

Did you ever solve this in a stable way as I am having the same issue?

avatar image
1

Answer by WylieFoxxx · Oct 19, 2020 at 08:16 PM

Trying to Rebuild the layout immediately wasn't working for me, so I had to use a coroutine that waits until fixed update, then toggles the menu on and off... A pretty bad solution if you ask me, but it works:

 // Usage: StartCoroutine(RefreshLayout(layout));
 
 IEnumerator RefreshLayout(GameObject layout)
     {
         yield return new WaitForFixedUpdate();
         VerticalLayoutGroup vlg = layout.GetComponent<VerticalLayoutGroup>();
         vlg.enabled = false;
         vlg.enabled = true;
     }

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

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

94 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 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 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 avatar image

Related Questions

[UI] Grid Layout Group Custom Sort Order 1 Answer

Unity crashes when using ExecuteAlways on a UIBehaviour 0 Answers

How can I edit the horizontal layout group on a panel or object 1 Answer

Nested UI elements and layout 0 Answers

How can I return a Dragged Object From a Layer Group to its Original Position? 0 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