Exemple #1
0
        private ListItemBase CreateNewItem(ListItemBase prefab, int index, float dimension)
        {
            GameObject instance = (GameObject)Instantiate(prefab.gameObject, Vector3.zero, Quaternion.identity);

            instance.transform.SetParent(_content.transform);
            instance.transform.localScale = Vector3.one;
            instance.SetActive(true);

            float position = index * (dimension + _spacing) + dimension / 2;

            RectTransform rectTransform = instance.GetComponent <RectTransform> ();

            switch (_scrollOrientation)
            {
            case ScrollOrientation.HORIZONTAL:
                rectTransform.anchorMin        = new Vector2(0, 0);
                rectTransform.anchorMax        = new Vector2(0, 1);
                rectTransform.anchoredPosition = new Vector2(position, 0);
                rectTransform.offsetMin        = new Vector2(rectTransform.offsetMin.x, 0);
                rectTransform.offsetMax        = new Vector2(rectTransform.offsetMax.x, 0);
                break;

            case ScrollOrientation.VERTICAL:
                rectTransform.anchorMin        = new Vector2(0, 1);
                rectTransform.anchorMax        = new Vector2(1, 1);
                rectTransform.anchoredPosition = new Vector2(0, -position);
                rectTransform.offsetMin        = new Vector2(0, rectTransform.offsetMin.y);
                rectTransform.offsetMax        = new Vector2(0, rectTransform.offsetMax.y);
                break;
            }

            return(instance.GetComponent <ListItemBase> ());
        }
        public void Create(int items, ListItemBase listItemPrefab)
        {
            _listItemHeight = listItemPrefab.Size.y;

            _content.sizeDelta = new Vector2(_content.sizeDelta.x, _listItemHeight * items + _spacing * (items - 1));

            _itemsTotal           = items;
            _itemsVisible         = Mathf.FloorToInt(_viewport.rect.height / _listItemHeight) * 2;
            _itemsToRecycleBottom = _itemsVisible / 2;

            _currentIndex = _itemsVisible - 1;

            _items = new List <ListItemBase> ();

            for (int i = 0; i < _itemsVisible; i++)
            {
                ListItemBase item = CreateNewItem(listItemPrefab, i * (_listItemHeight + _spacing) + _listItemHeight / 2);
                item.onSelected = HandleOnSelectedHandler;
                item.Index      = i;

                _items.Add(item);

                ItemLoaded(item);
            }

            _scrollRect.onValueChanged.AddListener((Vector2 position) =>
            {
                Recycle();
            });
        }
        private void RecycleItem(Direction direction)
        {
            ListItemBase firstItem = _items [0];
            ListItemBase lastItem  = _items [_items.Count - 1];

            float targetPositionY = (_listItemHeight + _spacing) * -1;

            switch (direction)
            {
            case Direction.DOWN:
                firstItem.Position = new Vector2(firstItem.Position.x, lastItem.Position.y + targetPositionY);
                firstItem.Index    = _currentIndex;
                firstItem.transform.SetAsLastSibling();

                _items.RemoveAt(0);
                _items.Add(firstItem);

                ItemLoaded(firstItem);
                break;

            case Direction.UP:
                lastItem.Position = new Vector2(lastItem.Position.x, firstItem.Position.y - targetPositionY);
                lastItem.Index    = _currentIndex - _itemsVisible;
                lastItem.transform.SetAsFirstSibling();

                _items.RemoveAt(_items.Count - 1);
                _items.Insert(0, lastItem);

                ItemLoaded(lastItem);
                break;
            }
        }
Exemple #4
0
        private void RecycleItem(ScrollDirection direction)
        {
            ListItemBase firstItem = _itemsList [0];
            ListItemBase lastItem  = _itemsList [_itemsList.Count - 1];

            float targetPosition = (_itemSize + _spacing);

            switch (direction)
            {
            case ScrollDirection.NEXT:

                switch (_scrollOrientation)
                {
                case ScrollOrientation.HORIZONTAL:
                    firstItem.Position = new Vector2(lastItem.Position.x + targetPosition, firstItem.Position.y);
                    break;

                case ScrollOrientation.VERTICAL:
                    firstItem.Position = new Vector2(firstItem.Position.x, lastItem.Position.y - targetPosition);
                    break;
                }

                firstItem.Index = _lastItemIndex;
                firstItem.transform.SetAsLastSibling();

                _itemsList.RemoveAt(0);
                _itemsList.Add(firstItem);

                ItemLoaded(firstItem);
                break;

            case ScrollDirection.PREVIOUS:

                switch (_scrollOrientation)
                {
                case ScrollOrientation.HORIZONTAL:
                    lastItem.Position = new Vector2(firstItem.Position.x - targetPosition, lastItem.Position.y);
                    break;

                case ScrollOrientation.VERTICAL:
                    lastItem.Position = new Vector2(lastItem.Position.x, firstItem.Position.y + targetPosition);
                    break;
                }

                lastItem.Index = _lastItemIndex - _itemsList.Count;
                lastItem.transform.SetAsFirstSibling();

                _itemsList.RemoveAt(_itemsList.Count - 1);
                _itemsList.Insert(0, lastItem);

                ItemLoaded(lastItem);
                break;
            }

            Canvas.ForceUpdateCanvases();
        }
Exemple #5
0
        public void ItemSelected(ListItemBase item, bool clear = false)
        {
            if (onItemSelected != null)
            {
                onItemSelected(item);

                if (clear)
                {
                    onItemSelected = null;
                }
            }
        }
        private ListItemBase CreateNewItem(ListItemBase prefab, float positionY)
        {
            GameObject instance = (GameObject)Instantiate(prefab.gameObject, Vector3.zero, Quaternion.identity);

            instance.transform.SetParent(_content.transform);
            instance.transform.localScale = Vector3.one;
            instance.SetActive(true);

            RectTransform rectTransform = instance.GetComponent <RectTransform> ();

            rectTransform.anchoredPosition = new Vector2(0, -positionY);
            rectTransform.offsetMin        = new Vector2(0, rectTransform.offsetMin.y);
            rectTransform.offsetMax        = new Vector2(0, rectTransform.offsetMax.y);

            return(instance.GetComponent <ListItemBase> ());
        }
Exemple #7
0
 void HandleOnSelectedHandler(ListItemBase item)
 {
     ItemSelected(item);
 }
Exemple #8
0
        public void Create(int items, ListItemBase listItemPrefab)
        {
            switch (_scrollOrientation)
            {
            case ScrollOrientation.HORIZONTAL:
                _scrollRect.vertical   = false;
                _scrollRect.horizontal = true;

                _content.anchorMin = new Vector2(0, 0);
                _content.anchorMax = new Vector2(0, 1);

                if (_fitItemToViewport)
                {
                    listItemPrefab.Size = new Vector2(_viewport.rect.width, listItemPrefab.Size.y);
                }

                _itemSize = listItemPrefab.Size.x;

                _content.sizeDelta = new Vector2(_itemSize * items + _spacing * (items - 1), 0);
                break;

            case ScrollOrientation.VERTICAL:
                _scrollRect.vertical   = true;
                _scrollRect.horizontal = false;

                _content.anchorMin = new Vector2(0, 1);
                _content.anchorMax = new Vector2(1, 1);

                if (_fitItemToViewport)
                {
                    listItemPrefab.Size = new Vector2(listItemPrefab.Size.x, _viewport.rect.height);
                }

                _itemSize = listItemPrefab.Size.y;

                _content.sizeDelta = new Vector2(0, _itemSize * items + _spacing * (items - 1));
                break;
            }

            if (_centerOnItem)
            {
                _scrollRect.inertia = false;
            }


            //_itemsVisible = Mathf.CeilToInt (GetViewportSize () / _itemSize);
            _itemsVisible = 1;  //TODO: hard coded fix, need to be changed to work

            int itemsToInstantiate = _itemsVisible;

            if (_itemsVisible == 1)
            {
                itemsToInstantiate = 5;
            }
            else if (itemsToInstantiate < items)
            {
                itemsToInstantiate *= 2;
            }

            if (itemsToInstantiate > items)
            {
                itemsToInstantiate = items;
            }

            _itemsList = new List <ListItemBase> ();

            for (int i = 0; i < itemsToInstantiate; i++)
            {
                ListItemBase item = CreateNewItem(listItemPrefab, i, _itemSize);
                item.onSelected = HandleOnSelectedHandler;
                item.Index      = i;

                _itemsList.Add(item);

                ItemLoaded(item);
            }

            _itemsTotal = items;

            _lastItemIndex = _itemsList.Count - 1;

            _itemsToRecycleAfter = _itemsList.Count - _itemsVisible;


            _scrollRect.onValueChanged.AddListener((Vector2 position) =>
            {
                if (!_centerOnItem)
                {
                    Recycle();
                }
            });
        }