Ejemplo n.º 1
0
    private void LoopCellsHorizontal()
    {
        for (int i = 0; i < column; i++)
        {
            ListCellBase firstCellOfThisColumn = cells[i * row + 0];
            int          columnNumOfThisColumn = firstCellOfThisColumn.index / row;
            int          newColumnNum          = columnNumOfThisColumn;
            if (columnNumOfThisColumn < currentColumn)
            {
                newColumnNum += column * ((currentColumn - columnNumOfThisColumn) / column + 1);
            }
            else if (columnNumOfThisColumn >= currentColumn + column)
            {
                newColumnNum -= column * ((columnNumOfThisColumn - currentColumn) / column);
            }

            if (newColumnNum != columnNumOfThisColumn)
            {
                for (int j = 0; j < row; j++)
                {
                    ListCellBase cell = cells[i * row + j];
                    cell.index = newColumnNum * row + j;
                    cell.rectTrans.anchoredPosition = new Vector2(newColumnNum * width_cell, -j * height_cell);

                    object data = (cell.index >= 0 && cell.index < datas.Count) ? datas[cell.index] : null;
                    cell.SetData(data);
                    cell.SetSelected(cell.index == currentSelectedIndex);
                }
            }
        }
    }
Ejemplo n.º 2
0
    private void LoopCellsVertical()
    {
        for (int i = 0; i < row; i++)
        {
            ListCellBase firstCellOfThisRow = cells[i * column + 0];

            int rowNumOfThisRow = firstCellOfThisRow.index / column;
            int newRowNum       = rowNumOfThisRow;
            if (rowNumOfThisRow < currentRow)
            {
                newRowNum += row * ((currentRow - rowNumOfThisRow) / row + 1); // 中间隔的页数 * 每页行数
            }
            else if (rowNumOfThisRow >= currentRow + row)                      // 下一页 或 之后的页
            {
                newRowNum -= row * ((rowNumOfThisRow - currentRow) / row);     // 隔的页数 * 每页行数
            }

            if (newRowNum != rowNumOfThisRow)
            {
                for (int j = 0; j < column; j++)
                {
                    ListCellBase cell = cells[i * column + j];
                    cell.index = newRowNum * column + j;
                    cell.rectTrans.anchoredPosition = new Vector2(j * width_cell, -newRowNum * height_cell);

                    object data = (cell.index >= 0 && cell.index < datas.Count) ? datas[cell.index] : null;
                    cell.SetData(data);
                    cell.SetSelected(cell.index == currentSelectedIndex);
                }
            }
        }
    }
Ejemplo n.º 3
0
 public void UpdateAllCells()
 {
     for (int i = 0; i < cells.Count; i++)
     {
         ListCellBase cell = cells[i];
         object       data = (cell.index >= 0 && cell.index < datas.Count) ? datas[cell.index] : null;
         cell.SetData(data);
         cell.SetSelected(cell.index == currentSelectedIndex);
     }
 }
Ejemplo n.º 4
0
    private void InitCells()
    {
        if (vertical)
        {
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < column; j++)
                {
                    ListCellBase cell = cells[i * column + j];
                    cell.index = i * column + j;
                    cell.rectTrans.anchoredPosition = new Vector2(j * width_cell, -i * height_cell);

                    object data = (cell.index >= 0 && cell.index < datas.Count) ? datas[cell.index] : null;
                    cell.SetData(data);
                    cell.SetSelected(false);
                }
            }
        }
        else
        {
            for (int i = 0; i < column; i++)
            {
                for (int j = 0; j < row; j++)
                {
                    ListCellBase cell = cells[i * row + j];
                    cell.index = i * row + j;
                    cell.rectTrans.anchoredPosition = new Vector2(i * width_cell, -j * height_cell);

                    object data = (cell.index >= 0 && cell.index < datas.Count) ? datas[cell.index] : null;
                    cell.SetData(data);
                    cell.SetSelected(false);
                }
            }
        }
        currentSelectedIndex = -1;
    }
Ejemplo n.º 5
0
    // Initialize
    public void SetUp()
    {
        rectTransform = transform as RectTransform;

        // scrollrect
        scrollRect = GetComponent <ScrollRect>();
        if (scrollRect == null)
        {
            scrollRect = gameObject.AddComponent <ScrollRect>();
        }
        scrollRect.onValueChanged.AddListener(OnValueChanged);

        // content
        Transform contentTrans = transform.FindChild("content");

        if (contentTrans == null)
        {
            contentTrans = new GameObject("content", typeof(RectTransform)).transform;
            contentTrans.SetParent(transform, false);
        }
        content                  = contentTrans as RectTransform;
        content.anchorMax        = new Vector2(0, 1);
        content.anchorMin        = new Vector2(0, 1);
        content.pivot            = new Vector2(0, 1);
        content.anchoredPosition = Vector2.zero;

        scrollRect.content    = content;
        scrollRect.vertical   = vertical;
        scrollRect.horizontal = !vertical;

        // rect2D mask
        if (transform.GetComponent <RectMask2D>() == null)
        {
            gameObject.AddComponent <RectMask2D>();
        }
        // canvasGroup
        if (gameObject.GetComponent <CanvasGroup>() == null)
        {
            gameObject.AddComponent <CanvasGroup>();
        }

        // height  width
        height_scroll = rectTransform.rect.height;
        height_cell   = cellPrefab.rect.height;

        width_scroll = rectTransform.rect.width;
        width_cell   = cellPrefab.rect.width;

        // row and column
        if (vertical)
        {
            row    = Mathf.CeilToInt(height_scroll / height_cell) + 1;
            column = Mathf.Clamp(column, 1, Mathf.FloorToInt(width_scroll / width_cell));
        }
        else
        {
            row    = Mathf.Clamp(row, 1, Mathf.FloorToInt(height_scroll / height_cell));
            column = Mathf.CeilToInt(width_scroll / width_cell) + 1;
        }

        // create cells
        if (cells.Count == 0)
        {
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < column; j++)
                {
                    GameObject    clone     = Instantiate <GameObject>(cellPrefab.gameObject);
                    RectTransform rectTrans = clone.GetComponent <RectTransform>();
                    rectTrans.SetParent(content, false);
                    rectTrans.localScale = Vector3.one;
                    rectTrans.anchorMax  = new Vector2(0, 1);
                    rectTrans.anchorMin  = new Vector2(0, 1);
                    rectTrans.pivot      = new Vector2(0, 1);

                    ListCellBase cell = clone.GetComponent <ListCellBase>();
                    cell.onClicked = OnCellClicked;

                    cells.Add(cell);
                }
            }
        }
        cellPrefab.gameObject.SetActive(false);
    }