0

I have a scroll view in Unity3D which has all of my elements in it. It scrolls only horizontally and has multiple child elements in view at any time. Most of the time this will be 6, in some rare occasions it can be 5. The setup is 2 rows of 3 objects

Example: enter image description here

As you can see it's 6(and part of number 7/8) objects most of the time and in rare occasions it can be 5. (towards the end for example)

What I would like is: When dragging objects, it always snaps to 6 centered objects. So when I have 12 objects and the user drags the window a bit, I want it to snap to the closest 6. I used Unity's Scroll Rect window with a horizontal layout group below it to achieve my current result. With the settings below: Where "GameGrid" Is just an empty object with a horizontal layout group on it. Below the game grid, the game tiles are made during runtime.

enter image description here

Patrick
  • 135
  • 1
  • 14

1 Answers1

0

Use the ScrollView and make your objects child of the ScrollView->Viewport->Content object. You can now control the anchoredPosition of the Content RectTransform and at the same time position of all its children. You can easily calculate the "snap" position by dividing the Content RectTransform sizeDelta.x by the given width and using the correct position values.

Dave
  • 2,684
  • 2
  • 21
  • 38