Draco18s

scroll.StopMovement()

Jul 8th, 2022
1,087
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | None | 0 0
  1. public class ScrollSpeedClamp : MonoBehaviour {
  2.     //clamps inertia of scroll rects to cease updating when the speed is below a certain threshold
  3.     private void Start() {
  4.         ScrollRect[] allScrolls = GameObject.FindObjectsOfType<ScrollRect>();
  5.         foreach (ScrollRect scroll in allScrolls.Where(x => x.inertia)) {
  6.             ScrollRect self = scroll;
  7.             scroll.onValueChanged.AddListener((v) => OnScroll(self, v));
  8.         }
  9.     }
  10.  
  11.     private static Dictionary<ScrollRect, Vector2> lastScrollPosition = new Dictionary<ScrollRect, Vector2>();
  12.     private static void OnScroll(ScrollRect scroll, Vector2 v) {
  13.         if(lastScrollPosition.ContainsKey(scroll)) {
  14.             Vector2 last = lastScrollPosition[scroll];
  15.             lastScrollPosition[scroll] = v;
  16.             if((last - v).sqrMagnitude > 1f) return;
  17.             scroll.StopMovement();
  18.             Debug.Log($"JUST STOP ALREADY! D: {v.y}");
  19.         }
  20.         else {
  21.             lastScrollPosition.Add(scroll, v);
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment