1

I have a scrollrect object on a UI canvas that has a viewport object child, and the viewport object has a content object child that stores all of the items to be scrolled. enter image description here

I have selected the viewport for reference. I want to resize the viewport rect relative to LevelScroller (see hierarchy) in order for there to be a margin around the scrollview, so that the scrolling items do not touch the edge of the gray LevelScroller rect. But the viewport rect is not resizeable. Right now the result is this:

enter image description here

See how the button at the top (1S-Levitation) is cut off by the grey panel. I want there to be a margin between the point where it is cut off and the gray box.

I have tried adding a horizontal layout group to LevelScroller to forcibly add a margin, but this did not work. Why is the viewport not resizeable?

Any help is appreciated

1 Answer 1

1

I believe that the viewport is not resizable because it is supposed to manage how your scroll will behave during the user interaction. As an example, you have only one cell in your content container and the user drag it down, once it is released it is supposed to move back to the top of your viewport, and if you put a margin in there it will not be capable of move entirely back to the top.

But hopefully you can do the following trick to have your margin:

Create an empty game object called Scroller and set it as the father of your LevelScroller. Then, set your LevelScroller RectTransform to stretch in both directions, and set the desired margin on the RectTransform Top, Bottom, Right and Left fields. You should have something like this:

LevelScroller as a child with 10 pixels of top and bottom margin

Then create another empty object child of your parent Scroller, and put it behind the LevelScroller. Lets call it BgImage. Now, set the RectTransform of BgImage to stretch in both directions and add an image to it. Ta daaa, you should have now your background with your margins on your viewport.

BgImage working as the background of the LevelScroller

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, thank you. I am new to Unity UI/UX and adding a parent panel and expanding the margins there is a good idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.