コード例 #1
0
        private float AdjustItemView(ScrollViewItem item, float anchorOffset)
        {
            // set parent
            item.rectTransform.SetParent(contentTransform, false);

            if (scrollDirection == ScrollDirection.TopToBottom)
            {
                item.rectTransform.ChangeAnchors(new Vector2(0.5f, 1), new Vector2(0.5f, 1));
                item.rectTransform.ChangePivot(new Vector2(0.5f, 1));
                item.rectTransform.anchoredPosition = new Vector2(0, anchorOffset);
                return(item.rectTransform.rect.height);
            }
            if (scrollDirection == ScrollDirection.BottomToTop)
            {
                item.rectTransform.ChangeAnchors(new Vector2(0.5f, 0), new Vector2(0.5f, 0));
                item.rectTransform.ChangePivot(new Vector2(0.5f, 0));
                item.rectTransform.anchoredPosition = new Vector2(0, -anchorOffset);
                return(item.rectTransform.rect.height);
            }
            if (scrollDirection == ScrollDirection.LeftToRight)
            {
                item.rectTransform.ChangeAnchors(new Vector2(0, 0.5f), new Vector2(0, 0.5f));
                item.rectTransform.ChangePivot(new Vector2(0, 0.5f));
                item.rectTransform.anchoredPosition = new Vector2(-anchorOffset, 0);
                return(item.rectTransform.rect.width);
            }
            // scrollDirection == ScrollDirection.RightToLeft
            item.rectTransform.ChangeAnchors(new Vector2(1, 0.5f), new Vector2(1, 0.5f));
            item.rectTransform.ChangePivot(new Vector2(1, 0.5f));
            item.rectTransform.anchoredPosition = new Vector2(anchorOffset, 0);
            return(item.rectTransform.rect.width);
        }
コード例 #2
0
        private float AdjustItemViewBottomToTop(ScrollViewItem item, float anchoredPosition, bool useSelfSize = false)
        {
            float size = item.rectTransform.rect.height;

            item.rectTransform.SetParent(contentTransform, false);
            item.rectTransform.ChangeAnchors(LayoutUtil.LayoutAnchorType.BottomCenter);
            item.rectTransform.ChangePivot(LayoutUtil.LayoutPivotType.CenterBottom);
            item.rectTransform.anchoredPosition = new Vector2(0, anchoredPosition - (useSelfSize ? size : 0));

            return(size);
        }
コード例 #3
0
        public ScrollViewItem CreateInstance(int index)
        {
            int type = GetTypeByIndex(index);

            ScrollViewItem item = null;

            if (caches != null && caches[type] != null && caches[type].Count > 0)
            {
                item = caches[type].Pop();
                item.gameObject.SetActive(true);
            }
            if (item == null)
            {
                ScrollViewPrefabData prefabData = GetPrefabDataByType(type);
                if (prefabData != null && prefabData.prefab != null)
                {
                    GameObject obj = GameObject.Instantiate(prefabData.prefab);
                    item = obj.AddComponent <ScrollViewItem>();

                    if (prefabData.widthStretch)
                    {
                        item.rectTransform.sizeDelta = new Vector2(scrollView.viewPortSzie.x, item.rectTransform.sizeDelta.y);
                    }

                    if (prefabData.heightStretch)
                    {
                        item.rectTransform.sizeDelta = new Vector2(item.rectTransform.sizeDelta.x, scrollView.viewPortSzie.y);
                    }
                    item.prefabType = type;
                }
            }

            if (item != null)
            {
                item.index = index;
                if (onNewItem != null)
                {
                    onNewItem.Invoke(index, item);
                }
            }

            return(item);
        }
コード例 #4
0
        public void RecycleInstance(ScrollViewItem item)
        {
            if (item == null)
            {
                return;
            }

            item.gameObject.SetActive(false);
            int type = item.prefabType;

            if (type < 0 && type >= GetTypeCount())
            {
                return;
            }
            if (caches[type] == null)
            {
                caches[type] = new Stack <ScrollViewItem>();
            }
            caches[type].Push(item);
        }
コード例 #5
0
        private void UpdateViewBottomToTop(int index, float offset)
        {
            float beforeSize = adapter.GetSizeOffset(index) + padding[1].y;

            // init behind [include self]
            float totalSize        = viewPortTransform.rect.height;
            float itemsSize        = 0;
            float anchorOffsetTemp = offset + beforeSize;
            int   indexTemp        = index;

            while (itemsSize < totalSize + reservedSize - offset && indexTemp < adapter.GetCount())
            {
                ScrollViewItem item = adapter.CreateInstance(indexTemp++);
                float          size = AdjustItemViewBottomToTop(item, anchorOffsetTemp);

                anchorOffsetTemp += size;
                itemsSize        += size;
                items.Add(item);
            }

            // init before
            anchorOffsetTemp = offset + beforeSize;
            itemsSize        = 0;
            indexTemp        = index;
            while (itemsSize < reservedSize + offset && indexTemp > 0)
            {
                ScrollViewItem item = adapter.CreateInstance(--indexTemp);
                float          size = AdjustItemViewBottomToTop(item, anchorOffsetTemp, true);

                anchorOffsetTemp -= size;
                itemsSize        += size;
                items.Insert(0, item);
            }

            contentTransform.anchoredPosition = new Vector2(contentTransform.anchoredPosition.x, -beforeSize);
        }
コード例 #6
0
        private void UpdateTopToBottom()
        {
            // top
            int firstShowItemIndex = -1;

            for (int i = 0; i < items.Count; ++i)
            {
                if (items[i].rectTransform.localPosition.y + contentTransform.anchoredPosition.y - items[i].rectTransform.rect.height > reservedSize)
                {
                    firstShowItemIndex = i;
                    break;
                }
            }
            for (int i = 0; i <= firstShowItemIndex; ++i)
            {
                ScrollViewItem recycleItem = items[0];
                adapter.RecycleInstance(recycleItem);
                items.RemoveAt(0);
            }
            if (items[0].rectTransform.localPosition.y + contentTransform.anchoredPosition.y < reservedSize)
            {
                int            index     = items[0].index;
                float          emptySize = -(items[0].rectTransform.localPosition.y + contentTransform.anchoredPosition.y) + reservedSize;
                ScrollViewItem preItem   = items[0];
                while (index > 0 && emptySize > 0)
                {
                    ScrollViewItem item = adapter.CreateInstance(--index);
                    AdjustItemView(item, preItem.rectTransform.anchoredPosition.y + item.rectTransform.rect.height);
                    emptySize -= item.rectTransform.rect.height;
                    preItem    = item;
                    items.Insert(0, item);
                }
            }

            // bottom
            int lastShowItemIndex = items.Count;

            for (int i = items.Count - 1; i >= 0; --i)
            {
                if (items[i].rectTransform.localPosition.y + contentTransform.anchoredPosition.y < -viewPortSize - reservedSize)
                {
                    lastShowItemIndex = i;
                    break;
                }
            }
            for (int i = 0; i < items.Count - lastShowItemIndex; ++i)
            {
                ScrollViewItem recycleItem = items[items.Count - 1];
                adapter.RecycleInstance(recycleItem);
                items.RemoveAt(items.Count - 1);
            }
            if (items[items.Count - 1].rectTransform.localPosition.y + contentTransform.anchoredPosition.y - items[items.Count - 1].rectTransform.rect.height > -viewPortSize - reservedSize)
            {
                int            index     = items[items.Count - 1].index;
                float          emptySize = viewPortSize + reservedSize + (items[items.Count - 1].rectTransform.localPosition.y + contentTransform.anchoredPosition.y - items[items.Count - 1].rectTransform.rect.height);
                ScrollViewItem preItem   = items[items.Count - 1];
                while (emptySize > 0 && index < adapter.GetCount())
                {
                    ScrollViewItem item = adapter.CreateInstance(++index);
                    AdjustItemView(item, preItem.rectTransform.anchoredPosition.y - preItem.rectTransform.rect.height);
                    emptySize -= preItem.rectTransform.rect.height;
                    preItem    = item;
                    items.Add(item);
                }
            }
        }