3

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?

sesmajster
  • 752
  • 7
  • 32

1 Answers1

0

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.

pixlhero
  • 418
  • 2
  • 8
  • the problem here is that buttons inside the content always have the same y position, because when scrolling they don't actually change y position. Or is there a way to determine Y position regardless of the parent? ^^ – sesmajster Aug 03 '22 at 12:30
  • Ah, it should be the _global_ [position](https://docs.unity3d.com/ScriptReference/Transform-position.html). Also works for `RectTransform`. Or how are you reading the Y position of the buttons? – pixlhero Aug 03 '22 at 12:41
  • well, I just tried button.transform.position.y, but as I am now reading it should have something to do with rectTransform.TransformPoint? – sesmajster Aug 03 '22 at 12:47
  • huh. position should _really_ change though, it basically does TransformPoint for you. Can you check if `position.y` (__not__ `localPosition`) really doesn't change if the button moves on the screen? is it maybe the wrong button? – pixlhero Aug 03 '22 at 12:52