Unity version 2017.2
When using Unity Scroll View to create a feature that automatically adapts to the size of the content and scrolls to the bottom, there is a problem.
First, the content adapts to what I use when a content size Fitter component and sets only the best vertical direction size. Set to scroll view's content object, allowing this to automatically change size according to the size of the sub-object, making it possible to scroll
Then I added a text text at the bottom, with the text of the text Mesh Pro plug-in. In fact, it is similar to Ugui. This text is also set size adaptive
Then I'll add the message to the text component in the code,
At the time of testing, a problem was found that the content size adaptation was always slower than the size of the text component in the bottom, and then in the second time, the first connection was counted in the calculation size.
Baidu, said the problem is the content Size fitter This component will be updated slowly, so it will cause this problem.
The solution is:
Call Layoutrebuilder.forcerebuildlayoutimmediate () This function to pass the content's Recttransform component to the past, to solve the problem of slow size update frame
Layoutrebuilder.forcerebuildlayoutimmediate (Recttransform);
In fact, there are some other ways of saying it, but I did not actually take effect when I tested it in my own project.
Canvas.forceupdatecanvases ();
Update content Adaptive size immediately, test invalid
Container.ContentController.Container.ContentSizeFitter.SetLayoutVertical ();
Container.ContentController.Container.ContentSizeFitter.SetLayoutHorizontal ();
After that, let's scroll to the bottom of the scroll bar each time you add a message:
Scrollrect.verticalnormalizedposition=0
The front is the Scrollrect component on your ScrollView object, then set Verticalnormalizedposition, where 1 represents the top and 0 is the bottom
In fact, you can also set the message up to add, each add in front of the previous sentence, I was added in the back of the previous sentence, so you will need to scroll to the bottom of the function
Some issues with the setting of the scroll bar inside Unity