public void Recenter ()
{
if (mDrag == null)
{
mDrag = NGUITools.FindInParents<UIScrollView>(gameObject);
if (mDrag == null)
{
Debug
.LogWarning(GetType
() + " requires " + typeof(UIScrollView
) + " on a parent object in order to work",
this); enabled = false;
return;
}
else
{
mDrag.onDragFinished = OnDragFinished;
if (mDrag.horizontalScrollBar != null)
mDrag.horizontalScrollBar.onDragFinished = OnDragFinished;
if (mDrag.verticalScrollBar != null)
mDrag.verticalScrollBar.onDragFinished = OnDragFinished;
}
}
if (mDrag.panel == null) return;
// Calculate the panel's center in world coordinates
Vector3[] corners = mDrag.panel.worldCorners;
Vector3 panelCenter = (corners[2] + corners[0]) * 0.5f;
// Offset this value by the momentum
Vector3 pickingPoint = panelCenter - mDrag.currentMomentum * (mDrag.momentumAmount * 0.1f);
mDrag.currentMomentum = Vector3.zero;
float min = float.MaxValue;
Transform closest = null;
Transform trans = transform;
int index = 0;
// Determine the closest child
for (int i = 0, imax = trans.childCount; i < imax; ++i)
{
Transform t = trans.GetChild(i);
float sqrDist = Vector3.SqrMagnitude(t.position - pickingPoint);
if (sqrDist < min)
{
min = sqrDist;
closest = t;
index = i;
}
}
if(allowFlickToNext && UICamera.currentTouch != null){
if(UICamera.currentTouch.totalDelta.x > flickThreshold && centeredObject.transform == trans.GetChild(index)){
if(index - 1 >= 0){
closest = trans.GetChild(index - 1);
}
}
else if(UICamera.currentTouch.totalDelta.x < -flickThreshold && centeredObject.transform == trans.GetChild(index))
{
if(index + 1 < trans.childCount){
closest = trans.GetChild(index + 1);
}
}
}
CenterOn(closest, panelCenter);
}