Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ScrollSpeedClamp : MonoBehaviour {
- //clamps inertia of scroll rects to cease updating when the speed is below a certain threshold
- private void Start() {
- ScrollRect[] allScrolls = GameObject.FindObjectsOfType<ScrollRect>();
- foreach (ScrollRect scroll in allScrolls.Where(x => x.inertia)) {
- ScrollRect self = scroll;
- scroll.onValueChanged.AddListener((v) => OnScroll(self, v));
- }
- }
- private static Dictionary<ScrollRect, Vector2> lastScrollPosition = new Dictionary<ScrollRect, Vector2>();
- private static void OnScroll(ScrollRect scroll, Vector2 v) {
- if(lastScrollPosition.ContainsKey(scroll)) {
- Vector2 last = lastScrollPosition[scroll];
- lastScrollPosition[scroll] = v;
- if((last - v).sqrMagnitude > 1f) return;
- scroll.StopMovement();
- Debug.Log($"JUST STOP ALREADY! D: {v.y}");
- }
- else {
- lastScrollPosition.Add(scroll, v);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment