From 04df196896fa231deae5a1e0554eef843e19882a Mon Sep 17 00:00:00 2001 From: lujun Date: Sun, 27 Dec 2020 17:46:32 +0800 Subject: [PATCH] fix performance lag caused by scrollbar value set invoke --- .../Sources/Runtime/Scroller/Scroller.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Assets/FancyScrollView/Sources/Runtime/Scroller/Scroller.cs b/Assets/FancyScrollView/Sources/Runtime/Scroller/Scroller.cs index 27da6fc..39d5cf2 100644 --- a/Assets/FancyScrollView/Sources/Runtime/Scroller/Scroller.cs +++ b/Assets/FancyScrollView/Sources/Runtime/Scroller/Scroller.cs @@ -9,6 +9,7 @@ using UnityEngine.EventSystems; using UnityEngine.UI; using EasingCore; +using UnityEngine.Events; namespace FancyScrollView { @@ -141,6 +142,11 @@ public float Position UpdatePosition(value); } } + + /// + /// Scrollbar onValueChangedAction + /// + private UnityAction scrollbarOnValueChangedAction; readonly AutoScrollState autoScrollState = new AutoScrollState(); @@ -205,7 +211,7 @@ protected override void Start() if (scrollbar) { - scrollbar.onValueChanged.AddListener(x => UpdatePosition(x * (totalCount - 1f), false)); + scrollbarOnValueChangedAction = x => UpdatePosition(x * (totalCount - 1f), false); } } @@ -472,9 +478,18 @@ void UpdatePosition(float position, bool updateScrollbar = true) { onValueChanged?.Invoke(currentPosition = position); - if (scrollbar && updateScrollbar) + if (!scrollbar) + { + return; + } + + // If 'updateScrollbar' equals 'true' means this method is called by 'Scroller', otherwise from the + // scrollbar drag invoke + if (updateScrollbar) { + scrollbar.onValueChanged.RemoveListener(scrollbarOnValueChangedAction); scrollbar.value = Mathf.Clamp01(position / Mathf.Max(totalCount - 1f, 1e-4f)); + scrollbar.onValueChanged.AddListener(scrollbarOnValueChangedAction); } }