self:RefreshCommentsList()self.scrollView = self.CommentsView:GetComponent(ScrollRect)
self.scrollView.verticalNormalizedPosition = 0
如上所示,当我想刷新Unity中的一个ScrollView的列表后,将这个列表瞬间移至底部。但是上述这三行代码会出现一个问题:存在ScrollView列表中的东西还未刷新完,就执行了移至底部的方法,导致最终的效果与预期不符。
那怎么让它在刷新完成之后再进行后续的操作?
- 可以将下述代码移到下一帧去进行操作
self.scrollView = self.CommentsView:GetComponent(ScrollRect)
self.scrollView.verticalNormalizedPosition = 0
- 使用Unity中的LayoutRebuilder(强制立即重建布局),其中rectTrans为带有layout组件的trans
self:RefreshCommentsList()
LayoutRebuilder.ForceRebuildLayoutImmediate(rectTrans);
self.scrollView = self.CommentsView:GetComponent(ScrollRect)
self.scrollView.verticalNormalizedPosition = 0