tomazsaraiva / unity-canvas-page-slider

A Scrollable Page Viewer for Unity
https://tomazsaraiva.github.io/unity-canvas-page-slider/
28 stars 2 forks source link

PageScroller.SetPage does not report previous page correctly #3

Closed jpetays closed 3 months ago

jpetays commented 3 months ago

PageScroller.SetPage does not report previous page correctly but it is always 0.

    public void SetPage(int index)
    {
        _scrollRect.horizontalNormalizedPosition = GetTargetPagePosition(index);

        _targetPage = index;
        _currentPage = index;
        OnPageChangeEnded?.Invoke(0, _currentPage);
    }

I fixed this

    public void SetPage(int index)
    {
        _scrollRect.horizontalNormalizedPosition = GetTargetPagePosition(index);

        var previousPage = _currentPage;
        _targetPage = index;
        _currentPage = index;
        OnPageChangeEnded?.Invoke(previousPage, _currentPage);
    }

After this dots seems to reflect current page state correctly.

I created buttons to set desired page programmatically.

public class TestButton : MonoBehaviour
{
    [SerializeField] private PageScroller _pageScroller;
    [SerializeField] private int _pageIndex;

    private Button _button;

    private void Awake()
    {
        _button = GetComponent<Button>();
        _button.onClick.AddListener(() => { _pageScroller.SetPage(_pageIndex); });
    }
}
tomazsaraiva commented 3 months ago

Fixed in develop. Will be in the next release. Thanks!