/// <summary>s /// Gets the closest page to the point the user clicked inside the scrollbar. /// </summary> public void GetPredictedContentPosition(float normalizedScrollValue) { //Find the bounds of the viewport and content Bounds viewBounds = new Bounds(scrollRect.viewport.rect.center, scrollRect.viewport.rect.size); Bounds contentBounds = new Bounds(scrollRect.content.rect.center, scrollRect.content.rect.size); RectTransform contentRect = scrollRect.content; debugTextContainerMinX.text = "Min X: " + scrollRect.content.rect.min.x; debugTextContainerMaxX.text = "Max X: " + scrollRect.content.rect.max.x; debugTextContainerMinY.text = "Min Y: " + scrollRect.content.rect.min.y; debugTextContainerMaxY.text = "Max Y: " + scrollRect.content.rect.max.y; debugTextContainerCenterPos.text = "Center: " + scrollRect.content.rect.center; // How much larger the content is then the scroll view. float hiddenLength = contentBounds.size[Axis] - viewBounds.size[Axis]; // Where the position of the lower left corner of the contents bounds should be, in the space of the view //float contentBoundsMinPosition = contentRect.localPosition[Axis] + (normalizedScrollValue * hiddenLength); float contentBoundsMinPosition = scrollSnap._scrollStartPosition + (normalizedScrollValue * hiddenLength) * -1f; Debug.Log("Content Bounds MIN POS: " + contentBoundsMinPosition); // The new content localPosition in the space of the view. float newLocalPosition = contentBoundsMinPosition; Vector3 localPosition = Vector3.zero; localPosition [Axis] = scrollSnap._scrollStartPosition; //Debug.Log ("Content rect CURRENT position is " + localPosition); debugTextContainerPosition.text = "Current Container Position: " + scrollSnap._screensContainer.localPosition; localPosition[Axis] = newLocalPosition; debugTextPredictedContainerPosition.text = "Predicted Container Position " + localPosition; //Debug.Log ("Content rect UPDATED position is " + localPosition); int predictedPagePosition = scrollSnap.GetPageforPosition(localPosition); //Debug.Log ("Content rect page is " + predictedPagePosition); debugTextPredictedPage.text = "Predicted Page: " + predictedPagePosition; }
public int GetPageForNormalizedValue(float normalizedValue) { //Find the bounds of the viewport and content Bounds viewBounds = new Bounds(scrollRect.viewport.rect.center, scrollRect.viewport.rect.size); Bounds contentBounds = new Bounds(scrollRect.content.rect.center, scrollRect.content.rect.size); RectTransform contentRect = scrollRect.content; // How much larger the content is then the scroll view. float hiddenLength = contentBounds.size[Axis] - viewBounds.size[Axis]; // Where the position of the lower left corner of the contents bounds should be, in the space of the view float contentBoundsMinPosition = scrollSnap._scrollStartPosition + (normalizedValue * hiddenLength) * -1f; // The new content localPosition in the space of the view. float newLocalPosition = contentBoundsMinPosition; Vector3 localPosition = Vector3.zero; localPosition [Axis] = scrollSnap._scrollStartPosition; localPosition[Axis] = newLocalPosition; return(scrollSnap.GetPageforPosition(localPosition)); }