- Home /
Can't use horizontal layout with ScrollRect scrollview in Unity 5.3.3f1
I read the docs, watched a couple videos and still cannot get this to work. I'm not sure if I am doing things wrong or if Unity broke it. Hopefully it is just me, but since they already broke UI rendering to texture in 5.4.x and I had to revert to 5.3 and in 5.3 I see UI elements not updating in the editor at times, I wouldn't be surprised if this is broken too.
Basically, I'm trying to create a horizontal scrollview with a bunch of elements inside, each taking up the whole 'viewport' dimensions and have the 'content' of the scrollview both resize dynamically depending on the number of elements and also apply horizontal layout to the items.
Essentially this:
I'm including a sample project and even a video of it (inside the zip attached).
The gist of what I tried is:
Create scrollview from UI menu (notice now there is a new 'scrollview' as opposed to 'bare'
ScrollRect
-- but I also tried with theScrollRect
).The default scrollview object from the menu assumes as vertical layout, so I changed the content's transform to flexible height and absolute width.
Added a bunch of images to the scrollview's content, with layout element components with a min width equal to the scrollview's width.
Tried adding horizontal layout group and / or content size fitter to scrollview's content.
The problem is that the content does not resize, staying always at exactly the size of the viewport. Tried the content fitter too... same thing.
Any help would be gladly appreciated. Thanks!
Answer by Brijs · Jun 10, 2016 at 12:04 PM
Here you can use Grid Layout group instead Horizontal Layout Group with Content Size Fitter as follows
Go to Hierarchy-->Canvas-->ScrollView-->ViewPort-->Content Attach GridLayoutGroup and ContentSizeFitter component to Content object and set parameters as shown below
Here cell size is the size of ScrollView.
Now you can add elements dynamically to Content gameobject and content will resize automatically allowing added elements to be fixed sized(Cell size of GridLayoutGroup).
You can refer attached package
Thanks for putting in the effort in even making a package.
I'm accepting your answer because it works. But I think of it as a workaround, and I am still wondering why would the horizontal layout not just work.
Your answer
Follow this Question
Related Questions
How do I dynamically update a rect scroll from a List? 0 Answers
Unity 5 UI Scroll Rect Problem 0 Answers
Scroll Rect with Grid Layout not working properly. 2 Answers
Performance issue with ScrollRect.LateUpdate() 1 Answer
Make an snapped ScrollView 2 Answers