public void Initialize(int pTotalItems)
        {
            if (pTotalItems == total)
            {
                return;
            }

            mListItemRect = new List <RectTransform>();

            if (mListItem == null || mListItem.Count == 0)
            {
                mListItem = new List <OptimizedScrollItem>();
                mListItem.Prepare(prefab, container.parent, 5);
            }
            else
            {
                mListItem.Free(container);
            }

            total = pTotalItems;

            container.anchoredPosition3D = new Vector3(0, 0, 0);

            if (mMaskRect == null)
            {
                mMaskRect = mask.GetComponent <RectTransform>();
            }

            Vector2 prefabScale = mListItem[0].GetComponent <RectTransform>().rect.size;

            mPrefabSize = (Direction == ScrollDirection.Horizontal ? prefabScale.x : prefabScale.y) + spacing;

            container.sizeDelta = Direction == ScrollDirection.Horizontal ? (new Vector2(mPrefabSize * total, prefabScale.y)) : (new Vector2(prefabScale.x, mPrefabSize * total));
            mHalftSizeContainer = Direction == ScrollDirection.Horizontal ? (container.rect.size.x * 0.5f) : (container.rect.size.y * 0.5f);

            mTotalVisible = Mathf.CeilToInt((Direction == ScrollDirection.Horizontal ? mMaskRect.rect.size.x : mMaskRect.rect.size.y) / mPrefabSize);

            mOffsetVec      = Direction == ScrollDirection.Horizontal ? Vector3.right : Vector3.down;
            mStartPos       = container.anchoredPosition3D - (mOffsetVec * mHalftSizeContainer) + (mOffsetVec * ((Direction == ScrollDirection.Horizontal ? prefabScale.x : prefabScale.y) * 0.5f));
            mOptimizedTotal = Mathf.Min(total, mTotalVisible + mTotalBuffer);
            for (int i = 0; i < mOptimizedTotal; i++)
            {
                OptimizedScrollItem item = mListItem.Obtain(container);
                RectTransform       rt   = item.GetComponent <RectTransform>();
                rt.anchoredPosition3D = mStartPos + (mOffsetVec * i * mPrefabSize);
                mListItemRect.Add(rt);

                item.SetActive(true);
                item.UpdateContent(i);
            }

            prefab.gameObject.SetActive(false);
            container.anchoredPosition3D += mOffsetVec * (mHalftSizeContainer - ((Direction == ScrollDirection.Horizontal ? mMaskRect.rect.size.x : mMaskRect.rect.size.y) * 0.5f));
        }
Exemplo n.º 2
0
        public void Init(int pTotalItems, bool pForce)
        {
            if (pTotalItems == total && !pForce)
            {
                return;
            }

            mListItemRect = new List <RectTransform>();

            if (mListItem == null || mListItem.Count == 0)
            {
                mListItem = new List <OptimizedScrollItem>();
                mListItem.Prepare(prefab, container.parent, 5);
            }
            else
            {
                mListItem.Free(container);
            }

            total = pTotalItems;

            container.anchoredPosition3D = new Vector3(0, 0, 0);

            var     rectZero    = mListItem[0].GetComponent <RectTransform>();
            Vector2 prefabScale = rectZero.rect.size;

            mPrefabSizeY = prefabScale.y + spacing;
            mPrefabSizeX = prefabScale.x + spacing;
            mPivot       = rectZero.pivot;
            if (itemFixedWidth > 0)
            {
                var size = rectZero.sizeDelta;
                size.x             = itemFixedWidth;
                rectZero.sizeDelta = size;
            }

            container.sizeDelta = new Vector2(prefabScale.x * totalCellOnRow, mPrefabSizeY * Mathf.CeilToInt(total * 1f / totalCellOnRow));
            mHalftSizeContainer = container.rect.size.y * 0.5f;

            var viewRect   = mask.transform as RectTransform;
            var scrollRect = scrollView.transform as RectTransform;

            //Re Update min-max view size
            if (autoMatchHeight)
            {
                float preferHeight = container.sizeDelta.y + spacing * 2;
                if (maxViewHeight > 0 && preferHeight > maxViewHeight)
                {
                    preferHeight = maxViewHeight;
                }
                else if (minViewHeight > 0 && preferHeight < minViewHeight)
                {
                    preferHeight = minViewHeight;
                }

                var size = scrollRect.sizeDelta;
                size.y = preferHeight;
                scrollRect.sizeDelta = size;
            }

            mTotalVisible = Mathf.CeilToInt(viewRect.rect.size.y / mPrefabSizeY) * totalCellOnRow;
            mTotalBuffer *= totalCellOnRow;

            mOffsetVec      = Vector3.down;
            mStartPos       = container.anchoredPosition3D - (mOffsetVec * mHalftSizeContainer) + (mOffsetVec * (prefabScale.y * 0.5f));
            mOptimizedTotal = Mathf.Min(total, mTotalVisible + mTotalBuffer);

            for (int i = 0; i < mOptimizedTotal; i++)
            {
                int cellIndex = i % totalCellOnRow;
                int rowIndex  = Mathf.FloorToInt(i * 1f / totalCellOnRow);

                OptimizedScrollItem item = mListItem.Obtain(container);
                RectTransform       rt   = item.transform as RectTransform;
                if (itemFixedWidth > 0)
                {
                    var size = rt.sizeDelta;
                    size.x       = itemFixedWidth;
                    rt.sizeDelta = size;
                }
                rt.anchoredPosition3D = mStartPos + (mOffsetVec * rowIndex * mPrefabSizeY);
                rt.anchoredPosition3D = new Vector3(-container.rect.size.x / 2 + cellIndex * mPrefabSizeX + mPrefabSizeX * 0.5f,
                                                    rt.anchoredPosition3D.y,
                                                    rt.anchoredPosition3D.z);
                mListItemRect.Add(rt);

                item.SetActive(true);
                item.UpdateContent(i);
            }

            prefab.gameObject.SetActive(false);
            container.anchoredPosition3D += mOffsetVec * (mHalftSizeContainer - (viewRect.rect.size.y * 0.5f));
        }