I’m was wondering how to update the content size of a ScrollRect component ?
Let’s say I have a Panel which is assigned as the RectTransform Content of a ScrollRect component - by default the scrolling area is based on the Content RectTransform size - But the size it’s not related to the children GameObject of the Panel. So if I add Button programmatically into my Panel, its RectTransform does not change, so tI will never see those Buttons.
To make it simple, I try to simulate a list which is dynamically populated - Is it possible ?
Your Grid component, the one with the GridLayout, has a no editable height, which let me think its size is dynamic. In my test project I added a VerticalLayout into my container, the one that will contain my buttons, but the height size is editable and when the buttons are added its size does not change, the buttons are not visible (but visible in the hierarchy when they are created).
Do I have to do something special in order to have my container height dynamic ?
I have another question, how to not have my buttons deformed by the VerticalLayout ? My Button pref is 100x25 but the prefab instances are 100x10 → 10 px is also the height of the container when it is empty, this value is automatic and not editable.
I would have expected the Scrollrect to consider the gridlayout total size and the gridlayout to dynamically resize as layout elements are added to the hierarchy. As it is, the only variables that seem to have effect on the scolling is the gridlayout width and height. The scrollrect does not care whether there is content below or not.
Does this mean that we have to calculate an appropriate gridlayout width/height at runtime in order for scrolling to work, or am I missing something?
Surely the scollrect and gridlayout in combo should be intelligent enough to set correct scrolling bounds…?
Still have one problem:
When repopulating a scrollrect, the scrollview is incorrect until I drag the scrollbar at which point the view will “snap” to the correct view. Is there a function I can call to force a recalculation of the scrollrect position? I cannot seem to find any… :-/
For me ContentSizeFitter doesn’t work. I have to calculate the Content size/pos by hand. Something like:
var itemWidth = 125f;
var totalItems = content.childCount;
var newContentWidth = itemWidth * totalItems + spacing.x * totalItems;
var t = content.GetComponent<RectTransform>();
t.sizeDelta = new Vector2(newContentWidth, t.sizeDelta.y);
t.anchoredPosition = new Vector3(newContentWidth * 0.5f, t.anchoredPosition.y);
Would be better a builtin option to make that for me.
Can only confirm that ContentSizeFitter does not work for me either. The content does have LayoutElement components with the correct mindimensions, but the positioning of the gridlayoutgroup that is being scrolled is being incorrectly positioned. When I remove the ContentSizeFitter and manually scale the gridlayout, all problems associated with incorrect positioning and weirdly acting scrollbars goes away.
Are we missing something? The documentation for these components and especially their implications in dynamic situations is quite scarce.
Thanks in advance!
The thread was originally about making the context rect fit the content. That can be done with a ContentSizeFitter. If that doesn’t work for you, then I don’t have enough information to be able to say why.
However, you’re also making comments about the positioning (not size) being wrong. That’s a different issue. Beta 19 will have the following fix: “Fixed issue in ScrollRect when content is smaller than view, where scrolling position would jump around.”
If you think that might fix your issues, then wait for beta 19. If not, then file a bug report with attached repro project and description of repro steps.
I am having trouble with getting this to work with several ScrollViews in a vertical layout group. I want the group to inherit the hight of the whole content. So I am using the content size fitter along with the vertical layout group. For the inheritance of the sizes of the scrollview contents, I need to provide layout groups all the way through the hierarchy, right? I can’t for the life of me keep the width of either the scrollRect, OR the ViewPort (if it exists) to stay as wide as the screens width while its child adjusts to its respective content. I cannot use a second Content Size Fitter either, because the whole hierarchy uses layout groups… HOW do you do that?
There are lots of answered posts already with questions about ContentSizeFitter + ScrollViews. I would suggest searching around a bit and look at those. If none of that helps, I’d recommend starting a new thread and describe your setip in much more detail, possibly with a screencast. From your description I can’t tell what you’re referring to when you talk about “the whole content”, whether you have layout groups both inside the scrollviews and outside, etc.
Hey, guys! I’ve come up with a simple script that will do EXACTLY what you want. It simply does math to expand the size of the RectTransform. Here’s the steps:
Create a Panel to hold your dynamic buttons
Add a GridLayoutGroup component to it (it must be a GridLayoutGroup)
Set the panel’s anchor and especially the pivot to the top left corner.
Add the following script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class DynamicScrollFitter : MonoBehaviour {
public bool expandX, expandY = true;
RectTransform myTransform;
GridLayoutGroup layoutGroup;
Vector2 cellSize;
RectOffset padding;
Vector2 newScale;
// Use this for initialization
void Start () {
myTransform = GetComponent<RectTransform>();
layoutGroup = GetComponent<GridLayoutGroup>();
cellSize = layoutGroup.cellSize;
padding = layoutGroup.padding;
}
void OnGUI() {
newScale = myTransform.sizeDelta;
if (expandX) newScale.x = padding.left + padding.right + ((cellSize.x + layoutGroup.spacing.x) * transform.childCount);
if (expandY) newScale.y = padding.top + padding.bottom + ((cellSize.y + layoutGroup.spacing.y) * transform.childCount);
myTransform.sizeDelta = newScale;
}
}
EDIT
Now of course you don’t have to use both X and Y. In my case, I don’t need X at all, so you should simply take out the code that involves the expandX boolean.
Let me know if you guys get any problems with this.
[quote=“Firedan1176, post:18, topic: 548391, username:Firedan1176”]
Hey, guys! I’ve come up with a simple script that will do EXACTLY what you want.
[/quote]Congratulations. You’ve just reinvented a wheel.
Search for ContentSizeFitter in docs and you’ll be surprised.
@Firedan1176 - I was looking for a solution and I was sad to see it really comes down to what you did in that script, which seems super obvious and should be a tick-box in the core Unity components, but for some reason isn’t. Well done.
@TimGS - Totally not correct. Using a ContentSizeFitter on a GridLayout that serves as the content for a ScrollRect will tell you that you shouldn’t use it because the size is controlled by another Layout component. Sadly the current Layout components simply don’t have these options.