Skip to content

Commit

Permalink
fix: update the scroll position by a scrollbar
Browse files Browse the repository at this point in the history
  • Loading branch information
mopsicus committed Aug 27, 2024
1 parent 0333480 commit 26627ae
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@

All notable changes to this project will be documented in this file.

## [2.0.1] - 2024-08-27
### Changed
- ADDON_VIEWS_COUNT moved to property from const
### Fixed
- Update the scroll position by a scrollbar

## [2.0.0] - 2024-04-02
### Release v2
2 changes: 2 additions & 0 deletions Documentation~/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ offsets and pull value for drag

`Parent container` – you can set container size by another game object with RectTransform

`AddonViewsCount` – views count which created on initialization to make gap for scrolling

## Recycle

UIS doesn't destroy or create list items each time. When you did the first initialization, UIS created the pool of elements and uses them for scrolling. And when you call `Recycle(int index)` or `RecycleAll()`, the items are not destroyed, they are just "hidden" and remain in that state until they are needed again.
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public class Demo : MonoBehaviour {
[SerializeField]
Scroller List = null;

/// </summary>
void Start() {
List.OnFill += OnFillItem;
List.OnHeight += OnHeightItem;
Expand Down
1 change: 0 additions & 1 deletion README.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public class Demo : MonoBehaviour {
[SerializeField]
Scroller List = null;

/// </summary>
void Start() {
List.OnFill += OnFillItem;
List.OnHeight += OnHeightItem;
Expand Down
24 changes: 16 additions & 8 deletions Runtime/Scroller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ public enum ScrollerDirection {
/// </summary>
public class Scroller : MonoBehaviour, IDropHandler {

/// <summary>
/// Addon count views
/// </summary>
const int ADDON_VIEWS_COUNT = 4;

/// <summary>
/// Velocity for scroll to function
/// </summary>
Expand Down Expand Up @@ -190,6 +185,11 @@ public class Scroller : MonoBehaviour, IDropHandler {
/// </summary>
public RectTransform ParentContainer = null;

/// <summary>
/// Addon count views
/// </summary>
public int AddonViewsCount = 4;

[HideInInspector]
/// <summary>
/// Top label
Expand Down Expand Up @@ -537,6 +537,14 @@ void UpdateHorizontal() {
/// Handler on scroller
/// </summary>
void OnScrollChange(Vector2 vector) {
// by https://github.com/webitube
// Note: If the scroller position changed but the scroll velocity is exactly zero, the movement was done via a scrollbar. In this case, we need to ScrollTo() the indicated position directly.
// Note 2: The normalized scrollbar position is opposite from the ScrollTo() index. This is why the we take (1.0 - pos) instead of pos directly.
if (_scroll.velocity.magnitude == 0.0f) {
var pos = (Type == 0) ? vector.y : vector.x;
var index = Mathf.RoundToInt(_count * (1.0f - pos));
ScrollTo(index);
}
if (Type == 0) {
ScrollChangeVertical();
} else {
Expand Down Expand Up @@ -955,7 +963,7 @@ public void ScrollTo(int index) {
index = 0;
}
if (index + _views.Length >= _count) {
index = _count - _views.Length + ADDON_VIEWS_COUNT;
index = _count - _views.Length + AddonViewsCount;
}
for (var i = 0; i < _views.Length; i++) {
var position = (index < gap) ? index : index + i - gap;
Expand Down Expand Up @@ -1144,7 +1152,7 @@ void CreateViewsVertical(bool isForceCreate = false) {
height += item + ItemSpacing;
}
height /= _heights.Count;
var fillCount = Mathf.RoundToInt(_container.height / height) + ADDON_VIEWS_COUNT;
var fillCount = Mathf.RoundToInt(_container.height / height) + AddonViewsCount;
_views = new GameObject[fillCount];
for (var i = 0; i < fillCount; i++) {
clone = Instantiate(Prefab, Vector3.zero, Quaternion.identity);
Expand Down Expand Up @@ -1190,7 +1198,7 @@ void CreateViewsHorizontal(bool isForceCreate = false) {
width += item + ItemSpacing;
}
width /= _widths.Count;
var fillCount = Mathf.RoundToInt(_container.width / width) + ADDON_VIEWS_COUNT;
var fillCount = Mathf.RoundToInt(_container.width / width) + AddonViewsCount;
_views = new GameObject[fillCount];
for (var i = 0; i < fillCount; i++) {
clone = Instantiate(Prefab, Vector3.zero, Quaternion.identity);
Expand Down

0 comments on commit 26627ae

Please sign in to comment.