Horizontal scrollview with images constant height in Unity3D UI - How? - unity3d

I want to implement scrollview in Unity3D 5 UI with horizontal images downloaded from web, they have different random aspect ratios, but I want them to be constant height. And width to be according their aspect ratio, like this:
I tried to use Horizontal Layout Group, but images get too little, so it can all fit in group, when I want them to be constant height and stretch parent's width. So, like, parent's widht is primary and it affects children, but I want children to affect total parent's width (expand it when adding new children images).
Any ideas how to achieve this? Thanks.

You need to use ScrollView with horizontal scrolling enabled and vertical scrolling disabled. Setup your hierarchy as in the image below:
On the GameObject named Content attach Horizontal Layout Group and Content Size Fitter with the following settings:
Then on all your images attach a Layout Element component and during runtime calculate the width using the aspect ratio of the image and height to the Content gameobject and assign this width to Preferred Width property of Layout Element Component: In the following image 300 is the calculated width for the image:
As a result any images you add as child to the Content gameobject will increase their parent's width and also you will be able to scroll horizontally to view all the image.
p.s. I wrote this answer in a hurry. Hopefully it will make sense to you if not let me know and I will improve my answer.

Related

How do I get images to fit inside a panel in unity

I have some UI panels laid out with layout groups and I wish to have images inside the panels.
How do I go about doing this?
I am aware that sprite masks exist but unsure if I can get the size of the panels to drive the bounding area of the mask.
The panels are variable in size since I would like the UI to work on any aspect ratio so I can't just use a static object for the sprite mask.
My UI Layout
I would like these images to keep their original aspect ratio but be cropped to the size of the panels.
On the panel gameobject you can add a RectMask2D component.
Make sure the image is a child of the panel and add the AspectRatioFitter with a AspectMode "Envelope Parent" and the aspect ratio of the image.

Layout issues in Unity

I have a Vertical Layout Group with a Content Size Fitter, with children a TextMeshPro and a ScrollView.
(The TextMeshPro is not a problem, I only put it to show that I needed a Vertical Layout Group, so let's forget that)
Inside the ScrollView I have another Text element. This Text element expands both vertically and horizontally. I want the horizontality to be scrolled by the ScrollView and I want the ScrollView to adjust to the height.
So I want the ScrollView to be the same height as all of its internal elements. this is my goal. I don't want a scripted solution, I want to be able to do it directly from the hierarchy, where you define the layout of all elements.
However I am not able to do this, my idea was to put the Vertical Layout Group with a Content Size Fitter in the ScrollView, but if I do, I can no longer scroll the text horizontally, as it is constantly realigned based on the alignment defined in the Vertical Layout Group.
ps. At first you might think, but a text with a horizontal Content Size Fitter does not stretch vertically, what need is there to enlarge the ScrollView based on the height of the text? In reality I don't really use the Text component, I use a similar component created specifically for particular fonts, which also expand the text vertically, for this I need that the height of the ScrollView dynamically adapts to the child components, but I repeat, with a Vertical Layout Group, then I can't scroll anymore.
Solutions?
I had done well the layout system. Disable Pixel Perfect on Canvas fix the problem of the text realignment.

How to set which view expands in a UIStackView?

I was just experimenting with UIStackView. In this quiz app i have different sized imaged, so i want the image to fill the space between the top button and the question text at the top. The buttons have constraints on them so they should be any size under 50px. As of now the top button is the one getting auto increased size, but that seems kinda random.
According to what I understand you have all the items inside a vertical UIstack. The images have different sizes so the available space for the buttons will changes based on the size of the image. I suggest that you add constraints to all buttons to be equal in height. This way they will re-size based on the available space but they will all have the same height.

Perfect square buttons in Unity3d

I'm trying to create a flexible menu for my mobile project but i'm having trouble with new UI system. I could do this with code but i want to know if it's possible without coding.
Here is what i'm trying to do:
http://i.imgur.com/33yMRgV.jpg
Basically, i want to have a menu area with height of %10 of total screen height. So for example, if it's a HD device, i want height of the red area in the image to be 192 pixel (1920/10). I can do that easily but i also want to have 192x192 buttons aligned on top right corner. I can set the height to parent so it becomes 192 too but i can't set the width to height. I can use ratio fitter to make them squares but i can't place them side by side.
So basically i just want to set width of an element to it's height and puting them side by side without coding. Is it possible?
Thanks
Have a panel inside Canvas, and add horizontal layout group and then add your buttons inside this panel.
Note : You will need to increase the width of the panel more than the canvas in order to have buttons horizontally.

Dynamically change height of slide for jssor

I'm using jssor plugin for slideshow of my content. There is a bunch of configuration options, which is great, but I can't find one for dynamically changing height of slide, and not sure if that is possible at all.
I see that one must specify height and width of outer container, and that slides will take its width and height. But can that be changed on the fly?
Hope the $ScaleHeight api help.
instance.$ScaleHeight(); //Get scaled height
instance.$ScaleHeight(height); //Scale the slider to new height and keep aspect ratio.