RectTransform GenerateScrollContent <T>(List <T> dataList, int mainPivotVectorValue) { if (_currentDisposable == null) { _currentDisposable = new DisposableBundle(); } var rootTransform = GetComponent <RectTransform>(); var scrollRect = CreateScrollRect(rootTransform); var scrollRectTransform = scrollRect.GetComponent <RectTransform>(); var viewport = CreateViewport(scrollRectTransform); var content = CreateContent(viewport, _scrollType, _arrangementType, dataList, mainPivotVectorValue, _columnLimit); scrollRect.content = content; scrollRect.viewport = viewport; SetScrollbar(scrollRect); _currentDisposable.Add(new GameObjectDisposable(content.gameObject)); _currentDisposable.Add(new GameObjectDisposable(viewport.gameObject)); _currentDisposable.Add(new GameObjectDisposable(scrollRect.gameObject)); return(content); }
List <IScrollableCell <T> > GenerateCellList <T>(GameObject cellPrefab, List <T> dataList, Transform cellRoot, out IDisposable cellDisposable) { DisposableBundle disposable = new DisposableBundle(); var rootTransform = GetComponent <RectTransform>(); int cellNum = 0; switch (_scrollType) { case ScrollType.Horizontal: cellNum = (Mathf.CeilToInt(rootTransform.rect.width / _cellSize.x) + 1) * _columnLimit; break; case ScrollType.Vertical: cellNum = (Mathf.CeilToInt(rootTransform.rect.height / _cellSize.y) + 1) * _columnLimit; break; } List <IScrollableCell <T> > result = new List <IScrollableCell <T> >(); for (int i = 0; i < cellNum; i++) { if (i >= dataList.Count) { continue; } var cellObject = GameObject.Instantiate(cellPrefab); IScrollableCell <T> cell = cellObject.GetComponent <IScrollableCell <T> >(); cell.SetValue(dataList[i]); Transform cellTransform = cell.transform; cellTransform.SetParent(cellRoot); cellTransform.localScale = Vector3.one; cellTransform.rotation = cellRoot.rotation; result.Add(cell); disposable.Add(new GameObjectDisposable(cellObject)); } cellDisposable = disposable; return(result); }
public IDisposable SetDatas <T>(IEnumerable <T> datas) { // Dispose before scroll if (_currentDisposable != null) { _currentDisposable.Dispose(); _currentDisposable = null; } ; _currentDisposable = new DisposableBundle(); if (_cellPrefab == null) { Debug.LogError("Prefab not found."); return(_currentDisposable); } if (_cellPrefab.GetComponent <IScrollableCell <T> >() == null) { Debug.LogError("No corresponding type of component is does not exist to this Prefab."); return(_currentDisposable); } int mainPivotVectorValue; int subPivotVectorValue; float baseCellPosition; switch (_scrollType) { case ScrollType.Horizontal: mainPivotVectorValue = _arrangementType == ArrangementType.LeftDownToRightUp || _arrangementType == ArrangementType.LeftUpToRightDown ? 1 : -1; subPivotVectorValue = _arrangementType == ArrangementType.LeftDownToRightUp || _arrangementType == ArrangementType.RightDownToLeftUp ? 1 : -1; break; case ScrollType.Vertical: mainPivotVectorValue = _arrangementType == ArrangementType.LeftDownToRightUp || _arrangementType == ArrangementType.RightDownToLeftUp ? 1 : -1; subPivotVectorValue = _arrangementType == ArrangementType.LeftDownToRightUp || _arrangementType == ArrangementType.LeftUpToRightDown ? 1 : -1; break; default: throw new Exception(string.Format("ScrollType <{0}> does not exist.", _scrollType)); } baseCellPosition = GetScrollTypeValue(_scrollType, _cellSize) * mainPivotVectorValue; List <T> dataList = datas.ToList(); // Generate Scroll items IDisposable cellDisposable; var cellRoot = GenerateScrollContent(dataList, mainPivotVectorValue); var cellList = GenerateCellList(_cellPrefab, dataList, cellRoot, out cellDisposable); _currentDisposable.Add(cellDisposable); // Move to init position cellList.ForEach(cell => UpdateCellPosition(cell, cell.GetValue(), dataList, mainPivotVectorValue, subPivotVectorValue, baseCellPosition)); // Start scroll coroutine var coroutine = StartCoroutine(UpdateCells(cellRoot, dataList, cellList, _scrollType, mainPivotVectorValue, subPivotVectorValue, baseCellPosition)); _currentDisposable.Add(new CoroutineDisposable(coroutine, this)); return(_currentDisposable); }