Hi, everyone.
I have to made a upScrollable/downScrollable indicator sprite.
Each time scrollView dragged or gameObjects added/removed, below is desired:
bool upScrollable = ?
bool bottomScrollable = ?
upScrollableSprite.enabled = upScrollable
bottomScrollableSprite.enabled = bottomScrollable
The quesion is how to get upScrollable/bottomScrollable ?
I tried two ways, both have defects:
1. use UIScrollBar
bool upScrollable = scrollBar.value > threshold
but threshold may varies for gameObjects count.
2. calculate with scroll.panel/ scroll.bounds.size.y / scroll.panel.GetViewSize() things.
It works, but too complex.
public void setGameObjects(IEnumerable<GameObject> gameObjects) {
scroll.ResetPosition();
// clear existed
// set new
grid.Reposition();
scroll.ResetPosition();
yWhenTop = scroll.panel.clipOffset.y;
StartCoroutine("delayedUpdateButtonVisibility");
}
// scroll.bounds.size seems won't update until next frame
IEnumerator delayedUpdateButtonVisibility() {
yield return new WaitForEndOfFrame();
updateButtonVisibility();
}
float yWhenTop;
public void updateButtonVisibility() {
Debug.Log("updateButtonVisibility()");
if (scroll.bounds.size.y <= scroll.panel.GetViewSize().y) { // contents less than one screen
buttonPrev.gameObject.SetActive(false);
buttonNext.gameObject.SetActive(false);
} else {
float yWhenBottom = yWhenTop - (scroll.bounds.size.y - scroll.panel.GetViewSize().y);
buttonPrev.gameObject.SetActive(scroll.panel.clipOffset.y < yWhenTop);
buttonNext.gameObject.SetActive(scroll.panel.clipOffset.y > yWhenBottom);
}
}
So what would the best practice be to indicate whether scrollView at top/bottom now?