Tanoda
ScrollRectTweener.cs
Go to the documentation of this file.
1
3
4using System.Collections;
6
8{
9 [RequireComponent(typeof(ScrollRect))]
10 [AddComponentMenu("UI/Extensions/ScrollRectTweener")]
11 public class ScrollRectTweener : MonoBehaviour, IDragHandler
12 {
13
14 ScrollRect scrollRect;
15 Vector2 startPos;
16 Vector2 targetPos;
17
18 bool wasHorizontal;
19 bool wasVertical;
20
21 public float moveSpeed = 5000f;
22 public bool disableDragWhileTweening = false;
23
24 void Awake()
25 {
26 scrollRect = GetComponent<ScrollRect>();
27 wasHorizontal = scrollRect.horizontal;
28 wasVertical = scrollRect.vertical;
29 }
30
31 public void ScrollHorizontal(float normalizedX)
32 {
33 Scroll(new Vector2(normalizedX, scrollRect.verticalNormalizedPosition));
34 }
35
36 public void ScrollHorizontal(float normalizedX, float duration)
37 {
38 Scroll(new Vector2(normalizedX, scrollRect.verticalNormalizedPosition), duration);
39 }
40
41 public void ScrollVertical(float normalizedY)
42 {
43 Scroll(new Vector2(scrollRect.horizontalNormalizedPosition, normalizedY));
44 }
45
46 public void ScrollVertical(float normalizedY, float duration)
47 {
48 Scroll(new Vector2(scrollRect.horizontalNormalizedPosition, normalizedY), duration);
49 }
50
51 public void Scroll(Vector2 normalizedPos)
52 {
53 Scroll(normalizedPos, GetScrollDuration(normalizedPos));
54 }
55
56 float GetScrollDuration(Vector2 normalizedPos)
57 {
58 Vector2 currentPos = GetCurrentPos();
59 return Vector2.Distance(DeNormalize(currentPos), DeNormalize(normalizedPos)) / moveSpeed;
60 }
61
62 Vector2 DeNormalize(Vector2 normalizedPos)
63 {
64 return new Vector2(normalizedPos.x * scrollRect.content.rect.width, normalizedPos.y * scrollRect.content.rect.height);
65 }
66
67 Vector2 GetCurrentPos()
68 {
69 return new Vector2(scrollRect.horizontalNormalizedPosition, scrollRect.verticalNormalizedPosition);
70 }
71
72 public void Scroll(Vector2 normalizedPos, float duration)
73 {
74 startPos = GetCurrentPos();
75 targetPos = normalizedPos;
76
78 LockScrollability();
79
80 StopAllCoroutines();
81 StartCoroutine(DoMove(duration));
82 }
83
84 IEnumerator DoMove(float duration)
85 {
86
87 // Abort if movement would be too short
88 if (duration < 0.05f)
89 yield break;
90
91 Vector2 posOffset = targetPos - startPos;
92
93 float currentTime = 0f;
94 while (currentTime < duration)
95 {
96 currentTime += Time.deltaTime;
97 scrollRect.normalizedPosition = EaseVector(currentTime, startPos, posOffset, duration);
98 yield return null;
99 }
100
101 scrollRect.normalizedPosition = targetPos;
102
104 RestoreScrollability();
105 }
106
107 public Vector2 EaseVector(float currentTime, Vector2 startValue, Vector2 changeInValue, float duration)
108 {
109 return new Vector2(
110 changeInValue.x * Mathf.Sin(currentTime / duration * (Mathf.PI / 2)) + startValue.x,
111 changeInValue.y * Mathf.Sin(currentTime / duration * (Mathf.PI / 2)) + startValue.y
112 );
113 }
114
115 public void OnDrag(PointerEventData eventData)
116 {
118 StopScroll();
119 }
120
121 void StopScroll()
122 {
123 StopAllCoroutines();
125 RestoreScrollability();
126 }
127
128 void LockScrollability()
129 {
130 scrollRect.horizontal = false;
131 scrollRect.vertical = false;
132 }
133
134 void RestoreScrollability()
135 {
136 scrollRect.horizontal = wasHorizontal;
137 scrollRect.vertical = wasVertical;
138 }
139
140 }
141}
void ScrollVertical(float normalizedY, float duration)
void OnDrag(PointerEventData eventData)
void Scroll(Vector2 normalizedPos, float duration)
Vector2 EaseVector(float currentTime, Vector2 startValue, Vector2 changeInValue, float duration)
void ScrollHorizontal(float normalizedX, float duration)
Credit Erdener Gonenc - @PixelEnvision.