public void Recenter() { if (mScrollView == null) { mScrollView = NGUITools.FindInParents <UIScrollView>(gameObject); if (mScrollView == null) { Debug.LogWarning(GetType() + " requires " + typeof(UIScrollView) + " on a parent object in order to work", this); enabled = false; return; } else { if (mScrollView) { mScrollView.centerOnChild = this; mScrollView.onDragFinished += OnDragFinished; } if (mScrollView.horizontalScrollBar != null) { mScrollView.horizontalScrollBar.onDragFinished += OnDragFinished; } if (mScrollView.verticalScrollBar != null) { mScrollView.verticalScrollBar.onDragFinished += OnDragFinished; } } } if (mScrollView.panel == null) { return; } Transform trans = transform; if (trans.childCount == 0) { return; } // Calculate the panel's center in world coordinates Vector3[] corners = mScrollView.panel.worldCorners; Vector3 panelCenter = (corners[2] + corners[0]) * 0.5f; // Offset this value by the momentum Vector3 momentum = mScrollView.currentMomentum * mScrollView.momentumAmount; Vector3 moveDelta = NGUIMath.SpringDampen(ref momentum, 9f, 2f); Vector3 pickingPoint = panelCenter - moveDelta * 0.01f; // Magic number based on what "feels right" float min = float.MaxValue; Transform closest = null; int index = 0; int ignoredIndex = 0; // Determine the closest child for (int i = 0, imax = trans.childCount, ii = 0; i < imax; ++i) { Transform t = trans.GetChild(i); if (!t.gameObject.activeInHierarchy) { continue; } float sqrDist = Vector3.SqrMagnitude(t.position - pickingPoint); if (sqrDist < min) { min = sqrDist; closest = t; index = i; ignoredIndex = ii; } ++ii; } // If we have a touch in progress and the next page threshold set if (nextPageThreshold > 0f && UICamera.currentTouch != null) { // If we're still on the same object if (mCenteredObject != null && mCenteredObject.transform == trans.GetChild(index)) { Vector3 totalDelta = UICamera.currentTouch.totalDelta; totalDelta = transform.rotation * totalDelta; float delta = 0f; switch (mScrollView.movement) { case UIScrollView.Movement.Horizontal: { delta = totalDelta.x; break; } case UIScrollView.Movement.Vertical: { delta = totalDelta.y; break; } default: { delta = totalDelta.magnitude; break; } } if (Mathf.Abs(delta) > nextPageThreshold) { UIGrid grid = GetComponent <UIGrid>(); CLUILoopGrid loopGrid = GetComponent <CLUILoopGrid>(); // add by chenbin if (loopGrid == null) { if (grid != null && grid.sorting != UIGrid.Sorting.None) { List <Transform> list = grid.GetChildList(); if (delta > nextPageThreshold) { // Next page if (ignoredIndex > 0) { closest = list [ignoredIndex - 1]; } else { closest = (GetComponent <UIWrapContent> () == null) ? list [0] : list [list.Count - 1]; } } else if (delta < -nextPageThreshold) { // Previous page if (ignoredIndex < list.Count - 1) { closest = list [ignoredIndex + 1]; } else { closest = (GetComponent <UIWrapContent> () == null) ? list [list.Count - 1] : list [0]; } } } else { Debug.LogWarning("Next Page Threshold requires a sorted UIGrid in order to work properly", this); } #region add by chenbin } else { int _index = NumEx.stringToInt(closest.name); if (delta > nextPageThreshold) { // Next page if (_index > 0) { _index--; closest = trans.Find(NumEx.nStrForLen(_index, 6)); } } else if (delta < -nextPageThreshold) { // Previous page if (_index < loopGrid.list.Count - 1) { _index++; closest = trans.Find(NumEx.nStrForLen(_index, 6)); } } } #endregion } } } CenterOn(closest, panelCenter); }