Ejemplo n.º 1
0
        private void SetObstaclesAnchoredPosition()
        {
            m_ObstacleGapMap.Clear();

            float obstacleGap = 0f;

            foreach (KeyValuePair <int, ScrollViewObstacle> item in m_Obstacles)
            {
                int index = item.Key;
                ScrollViewObstacle scrollViewObstacle = item.Value;

                if (index < m_Capacity)
                {
                    scrollViewObstacle.Wakeup();
                }
                else
                {
                    scrollViewObstacle.Sleep();
                }

                switch (m_LayoutType)
                {
                case LayoutType.Horizontal:
                {
                    float paddingLeft = m_Left;
                    float width       = index * m_DefaultItem.rect.width;
                    float gap         = index * m_SpaceX;
                    float translateX  = scrollViewObstacle.Width * 0.5f;
                    scrollViewObstacle.SetAnchoredPositionX((paddingLeft + width + gap + obstacleGap + translateX) * m_DirectionX);
                    obstacleGap += scrollViewObstacle.Width;
                    m_ObstacleGapMap.Add(index, obstacleGap);
                }
                break;

                case LayoutType.Vertical:
                {
                    float paddingTop = m_Top;
                    float height     = index * m_DefaultItem.rect.height;
                    float gap        = index * m_SpaceY;
                    float translateY = scrollViewObstacle.Height * 0.5f;
                    scrollViewObstacle.SetAnchoredPositionY((paddingTop + height + gap + obstacleGap + translateY) * m_DirectionY);
                    obstacleGap += scrollViewObstacle.Height;
                    m_ObstacleGapMap.Add(index, obstacleGap);
                }
                break;

                case LayoutType.Grid:
                    // Not support.
                    break;

                default:
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The obstacle will be placed in front of the cell with the same index. Grid layout is not currently supported.
        /// </summary>
        /// <param name="index"></param>
        /// <param name="gameObject"></param>
        public void RegisterObstacle(int index, GameObject gameObject)
        {
            ScrollViewObstacle scrollViewObstacle;

            if (m_Obstacles.TryGetValue(index, out scrollViewObstacle))
            {
                scrollViewObstacle.Init(gameObject);
                scrollViewObstacle.SetAnchorAndPivot();
            }
            else
            {
                scrollViewObstacle = new ScrollViewObstacle();
                scrollViewObstacle.Init(gameObject);
                scrollViewObstacle.SetAnchorAndPivot();
                m_Obstacles.Add(index, scrollViewObstacle);
            }
        }
Ejemplo n.º 3
0
        public void ResizeContent(int capacity)
        {
            if (capacity < 0)
            {
                capacity = 0;
            }

            if (m_Paginate)
            {
                switch (m_LayoutType)
                {
                case LayoutType.Horizontal:
                    capacity += capacity % m_Columns == 0 ? 0 : m_Columns - capacity % m_Columns;
                    break;

                case LayoutType.Vertical:
                    capacity += capacity % m_Rows == 0 ? 0 : m_Rows - capacity % m_Rows;
                    break;

                case LayoutType.Grid:
                    capacity += capacity % (m_Columns * m_Rows) == 0 ? 0 : (m_Columns * m_Rows) - capacity % (m_Columns * m_Rows);
                    break;

                default:
                    break;
                }
            }

            if (m_Capacity == capacity)
            {
                SetObstaclesAnchoredPosition();
                return;
            }

            m_Capacity = capacity;

            SetCells();
            SetPaginations();

            int rows    = 1;
            int columns = 1;

            switch (m_LayoutType)
            {
            case LayoutType.Horizontal:
                rows    = 1;
                columns = m_Capacity;
                break;

            case LayoutType.Vertical:
                rows    = m_Capacity;
                columns = 1;
                break;

            case LayoutType.Grid:
                rows    = (m_Capacity % m_Columns) == 0 ? m_Capacity / m_Columns : m_Capacity / m_Columns + 1;
                columns = m_Columns;
                break;

            default:
                break;
            }

            float paddingLeft = m_Left;
            float width       = columns * m_DefaultItem.rect.width;
            float gapX        = (columns - 1) * m_SpaceX;
            float paddingTop  = m_Top;
            float height      = rows * m_DefaultItem.rect.height;
            float gapY        = (rows - 1) * m_SpaceY;

            float obstacleGapX = 0f;
            float obstacleGapY = 0f;

            foreach (KeyValuePair <int, ScrollViewObstacle> item in m_Obstacles)
            {
                ScrollViewObstacle scrollViewObstacle = item.Value;
                if (item.Key < m_Capacity)
                {
                    switch (m_LayoutType)
                    {
                    case LayoutType.Horizontal:
                        obstacleGapX += scrollViewObstacle.Width;
                        break;

                    case LayoutType.Vertical:
                        obstacleGapY += scrollViewObstacle.Height;
                        break;

                    case LayoutType.Grid:
                        // Not support.
                        break;

                    default:
                        break;
                    }
                }
            }

            content.sizeDelta = new Vector2(paddingLeft + width + gapX + obstacleGapX, paddingTop + height + gapY + obstacleGapY);

            m_Index       = 0;
            m_ObstacleGap = 0;

            SetObstaclesAnchoredPosition();
            SetCellsAnchoredPosition();
        }