public void snap(int id, bool triggerEvent = true) { XYID finalXYID = snapPoints[id]; currentSnap = id; tween1 = _scrollTargetRect.DOLocalMove(new Vector2(0 - finalXYID.x, finalXYID.y), snapSpeed).SetEase(Ease.OutExpo).OnUpdate(calcSnapPercent); if (triggerEvent) { tween1.onComplete += () => { snapped.Invoke(id); }; } }
void Update() { Vector2 currentPos = _scrollTargetRect.anchoredPosition; if (_animating == true && snapPoints.Count > 0) { int counter = 0; double closest = 0; int closestKey = 0; bool closestSet = false; XYID finalXYID = new XYID(0, 0, 0); foreach (XYID xYID in snapPoints) { var dis = GetDistance(0 - currentPos[0], currentPos[1], xYID.x, xYID.y); if (counter == currentSnap) { dis = dis * 4; } if (dis < closest || closestSet == false) { closestKey = counter; closest = dis; closestSet = true; finalXYID = xYID; } counter++; } snap(closestKey); _animating = false; } if (_overstretchX != 0 && _finishdragging == true) { _finishdragging = false; float target = minX; if (_overstretchX == 2) { target = maxX; } tween1 = _rect.DOLocalMoveX(target, snapSpeed).SetEase(Ease.OutExpo).OnUpdate(calcSnapPercent); _animating = false; } else if (_overstretchY != 0 && _finishdragging == true) { _finishdragging = false; float target = minY; if (_overstretchY == 2) { target = maxY; } tween1 = _rect.DOLocalMoveY(target, snapSpeed).SetEase(Ease.OutExpo).OnUpdate(calcSnapPercent); _animating = false; } if (_animating) { float newX = currentPos[0] + Xspeed; float newY = currentPos[1] + Yspeed; newX = Mathf.Clamp(newX, minX, maxX); newY = Mathf.Clamp(newY, minY, maxY); SetPosition(newX, newY); Xspeed = Xspeed / divideBy; Yspeed = Yspeed / divideBy; if (Mathf.Abs(Yspeed) < 0.01 && Mathf.Abs(Xspeed) < 0.01) { _animating = false; } } else if (permanentScrollUntilLimit != 0) { float newY = currentPos[1] + permanentScrollUntilLimit; newY = Mathf.Clamp(newY, minY, maxY); if (newY == maxY) { permanentScrollUntilLimit = 0; } SetPosition(scrollTarget.GetComponent <RectTransform>().anchoredPosition[0], newY); Yspeed = Yspeed / divideBy; } }
public void addSnapPoint(float x, float y, int id) { XYID xYID = new XYID(x, y, id); snapPoints.Add(xYID); }