Vector3 GetLastPosition()
    {
        Transform temp = mChildren[mChildren.Count - 1].transform;
        PTPUIDragScrollViewLoop item = mChildren[mChildren.Count - 1].GetComponent <PTPUIDragScrollViewLoop>();
        Vector3 tempVec3             = temp.localPosition;

        if (reverseArrangement)
        {
            tempVec3.x -= (item.bounds.size.x * 0.5f);
            tempVec3.y += (item.bounds.size.y * 0.5f);
        }
        else
        {
            tempVec3.x += (item.bounds.size.x * 0.5f);
            tempVec3.y -= (item.bounds.size.y * 0.5f);
        }
        tempVec3.x += item.bounds.center.x;
        tempVec3.y += item.bounds.center.y;
        return(tempVec3);
    }
    private IEnumerator StepBack()
    {
        if (mTopIndex <= 0)          // list top
        {
            //print( "List top" );
            yield break;
        }

        Vector3 topPos = GetTopPosition();

        mTopIndex--;

        //print( "Top Index = " + mTopIndex );

        GameObject obj = mChildren[mChildren.Count - 1];

        mChildren.Remove(obj);
        mChildren.Insert(0, obj);

        obj.SendMessage("SetupListItem", mTopIndex);

        PTPUIDragScrollViewLoop item = obj.GetComponent <PTPUIDragScrollViewLoop>();

        item.updateLocked = true;

        // Wait for next frame
        if (delayedUpdate)
        {
            yield return(new WaitForEndOfFrame());

            yield return(new WaitForEndOfFrame());
        }

        // Following code will be executed in the next frame
        item.updateLocked = false;
        item.UpdateBounds();

        Vector3 pos = new Vector3(0.0f, 0.0f, 0.0f);

        if (reverseArrangement)
        {
            if (arrangement == Arrangement.Horizontal)
            {
                pos.x = topPos.x + (padding + (item.bounds.size.x * 0.5f)) - item.bounds.center.x;
            }
            else
            {
                pos.y = topPos.y - (padding + (item.bounds.size.y * 0.5f)) - item.bounds.center.y;
            }
        }
        else
        {
            if (arrangement == Arrangement.Horizontal)
            {
                pos.x = topPos.x - (padding + (item.bounds.size.x * 0.5f)) - item.bounds.center.x;
            }
            else
            {
                pos.y = topPos.y + (padding + (item.bounds.size.y * 0.5f)) - item.bounds.center.y;
            }
        }
        obj.transform.localPosition = pos;

        //ScrollView.RecalculateBounds();

        UpdateMargin();
    }
    private IEnumerator InitDelayed()
    {
        List <GameObject> children = new List <GameObject>();

        for (int index = 1; index < mKeepNum; ++index)
        {
            GameObject obj = (GameObject)Instantiate(listItemPrefab);
            obj.AddComponent <PTPUIDragScrollViewLoop>();
            if (bDragDrop)
            {
                obj.AddComponent <UIDragDropItem>();
                obj.AddComponent <UIDragDropItem>().cloneOnDrag = true;
            }
            obj.transform.parent     = gameObject.transform;
            obj.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
//			obj.GetComponent< ScrollViewItemBase >().Id = IdCounter++;

            PTPUIDragScrollViewLoop item = obj.GetComponent <PTPUIDragScrollViewLoop>();
            item.InitGrid(this);

            obj.SendMessage("SetupListItem", index);

            children.Add(obj);
        }

        // Wait for next frame
        if (delayedUpdate)
        {
            yield return(new WaitForEndOfFrame());

            yield return(new WaitForEndOfFrame());
        }

        // Following code will be executed in the next frame
        //		mChildren[0].transform.localPosition = itemFirstPos;
        mChildren[0].GetComponent <PTPUIDragScrollViewLoop>().UpdateBounds();

        while (children.Count > 0)
        {
            Vector3 lastPos = GetLastPosition();

            GameObject obj = children[0];
            children.RemoveAt(0);

            PTPUIDragScrollViewLoop item = obj.GetComponent <PTPUIDragScrollViewLoop>();
            item.UpdateBounds();

            Vector3 pos = new Vector3(0.0f, 0.0f, 0.0f);
            if (reverseArrangement)
            {
                if (arrangement == Arrangement.Horizontal)
                {
                    pos.x = lastPos.x - padding - (item.bounds.size.x * 0.5f) - item.bounds.center.x;
                }
                else
                {
                    pos.y = lastPos.y + padding + (item.bounds.size.y * 0.5f) - item.bounds.center.y;
                }
            }
            else
            {
                if (arrangement == Arrangement.Horizontal)
                {
                    pos.x = lastPos.x + padding + (item.bounds.size.x * 0.5f) - item.bounds.center.x;
                }
                else
                {
                    pos.y = lastPos.y - padding - (item.bounds.size.y * 0.5f) - item.bounds.center.y;
                }
            }
            obj.transform.localPosition = pos;
            //print( "pos " + pos.ToString()  );

            mChildren.Add(obj);
        }

        UpdateMargin();

        // Wait for next frame
        yield return(new WaitForEndOfFrame());

        yield return(new WaitForEndOfFrame());

        // Reset scroll position instead of calling ResetPosition()
        if (reverseArrangement)
        {
            mDrag.SetDragAmount(1.0f, 1.0f, false);
            mDrag.SetDragAmount(1.0f, 1.0f, true);
        }
        else
        {
            mDrag.SetDragAmount(0.0f, 0.0f, false);
            mDrag.SetDragAmount(0.0f, 0.0f, true);
        }
    }
    void Init(int itemNum)
    {
        mInitDone = true;
        mPanel    = NGUITools.FindInParents <UIPanel>(gameObject);
        mDrag     = NGUITools.FindInParents <UIScrollView>(gameObject);

        mTopIndex = 0;
        mDrag.transform.localPosition = new Vector3(0.0f, 0.0f, 0.0f);
        mDrag.panel.clipOffset        = new Vector3(0.0f, 0.0f, 0.0f);
        mAllItemNum = itemNum;

        if (listItemPrefab != null)
        {
            GameObject firstOne = (GameObject)Instantiate(listItemPrefab);
            firstOne.transform.parent = gameObject.transform;
            firstOne.AddComponent <PTPUIDragScrollViewLoop>();
            if (bDragDrop)
            {
                firstOne.AddComponent <UIDragDropItem>();
                firstOne.AddComponent <UIDragDropItem>().cloneOnDrag = true;
            }

//			firstOne.GetComponent< ScrollViewItemBase >().Id = IdCounter++;

            firstOne.transform.localScale    = new Vector3(1.0f, 1.0f, 1.0f);
            firstOne.transform.localPosition = itemFirstPos;             // new Vector3( -1000.0f, -1000.0f, 0.0f );
            //print( "first one = " + firstOne.transform.localPosition.ToString() );

            mChildren.Add(firstOne);

            PTPUIDragScrollViewLoop item = firstOne.GetComponent <PTPUIDragScrollViewLoop>();
            item.InitGrid(this);

            //firstOne.SendMessage( "SetItemNum" );
            int firstIndex = 0;
            firstOne.SendMessage("SetupListItem", firstIndex);

            item.UpdateBounds();

//			cellWidth = (item.bounds.size.x + padding);
//			cellHeight = (item.bounds.size.y + padding);

            if (arrangement == Arrangement.Horizontal)
            {
                mKeepNum = (int)(mPanel.finalClipRegion.z / (item.bounds.size.x + padding) + margin);
                //mMarginSpace = item.bounds.size.x * margin * 0.5f;
            }
            else
            {
                mKeepNum = (int)(mPanel.finalClipRegion.w / (item.bounds.size.y + padding) + margin);
                //mMarginSpace = item.bounds.size.y * margin * 0.5f;
            }

            //mAllItemNum = 5;

            if (mKeepNum > mAllItemNum)
            {
                mKeepNum = mAllItemNum;
            }
        }
        else
        {
            //error
            print("I need prefab!");
            return;
        }

        /*
         * for( int index = 1; index < mKeepNum; ++index )
         * {
         *      Vector3 lastPos = GetLastPosition();
         *
         *      GameObject obj = (GameObject)Instantiate( listItemPrefab );
         *      obj.transform.parent = gameObject.transform;
         *
         *      obj.transform.localScale = new Vector3( 1.0f, 1.0f, 1.0f );
         *
         *      LS4UIDragScrollViewLoop item = obj.GetComponent<LS4UIDragScrollViewLoop>();
         *      item.InitGrid( this );
         *
         *      obj.SendMessage( "SetupListItem", index );
         *
         *      item.UpdateBounds();
         *
         *      Vector3 pos = new Vector3( 0.0f, 0.0f, 0.0f );
         *      if( reverseArrangement )
         *      {
         *              if( arrangement == Arrangement.Horizontal )
         *              {
         *                      pos.x = lastPos.x - padding - (item.bounds.size.x * 0.5f) - item.bounds.center.x;
         *              }
         *              else
         *              {
         *                      pos.y = lastPos.y + padding + (item.bounds.size.y * 0.5f) - item.bounds.center.y;
         *              }
         *      }
         *      else
         *      {
         *              if( arrangement == Arrangement.Horizontal )
         *              {
         *                      pos.x = lastPos.x + padding + (item.bounds.size.x * 0.5f) - item.bounds.center.x;
         *              }
         *              else
         *              {
         *                      pos.y = lastPos.y - padding - (item.bounds.size.y * 0.5f) - item.bounds.center.y;
         *              }
         *      }
         *      obj.transform.localPosition = pos;
         *
         *      mChildren.Add( obj );
         * }
         *
         * UpdateMargin();
         *
         * if( reverseArrangement )
         * {
         *      mDrag.SetDragAmount( 1.0f, 1.0f, false );
         *      mDrag.SetDragAmount( 1.0f, 1.0f, true );
         * }
         * else
         * {
         *      mDrag.SetDragAmount( 0.0f, 0.0f, false );
         *      mDrag.SetDragAmount( 0.0f, 0.0f, true );
         * }
         */

        StartCoroutine(InitDelayed());
    }