Unity3D how to get centered object of a ScrollView?

1.2k views Asked by At

So I have this ScrollView set up using ScrollRect that has a Viewport child which has an Image and a Mask (pretty basic ScrollView setup). Inside the scroll view I have 15 buttons. I want to know what button is at the center of my ScrollView, so I can disable all other buttons (to be unclickable). I tried it with content.position.y but it's different for every screen size. Is there even anyway to do this?

1

There are 1 answers

4
pixlhero On

One practical way I can see is if you have a new "Center" GameObject outside of the ScrollRect somewhere. And then compare the global position of that to each of the buttons.

So if the global y coordinate of the button is close (by some margin) to the global y coordinate of the "Center" Gameobject, you make the button interactable, otherwise not.

The nice thing here is that it's easily debuggable, because you can actually see the "Center" Gameobject. And because the "Center" Gameobject is also somewhere in the Canvas, you can set it up such that it adapts to the screensize however you like.