/// <summary> /// Populates the data with a lot of records /// </summary> private void LoadData() { int RowCount = COLUMN; _data = new List <InventoryItemContainer>(); int j = 0; int m = itemDatas.Count % RowCount; int n = itemDatas.Count / RowCount; if (m > 0) { n += 1; } for (var i = 0; i < n + 1; i++) { var masterData = new InventoryItemContainer() { normalizedScrollPosition = 0, childData = new List <ItemResource>() }; _data.Add(masterData); for (; j < (RowCount * (i + 1)) && j < itemDatas.Count; j++) { masterData.childData.Add(itemDatas[j]); } } // tell the scroller to reload now that we have the data masterScroller.GetComponent <ScrollRect>().verticalNormalizedPosition = 0; ReloadData(); }
protected override IEnumerator ScrollToBottomCoroutine() { if (enhancedScroller == null) { enhancedScroller = GetComponentInChildren <EnhancedScroller>(); } if (enhancedScroller == null) { Debug.LogWarning(GetType().Name + ": No Enhanced Scroller is assigned. Can't scroll to bottom."); yield break; } if (scrollRect == null) { scrollRect = enhancedScroller.GetComponent <UnityEngine.UI.ScrollRect>(); } if (scrollRect == null) { Debug.LogWarning(GetType().Name + ": No ScrollRect found on the Enhanced Scroller component. Can't scroll to bottom."); yield break; } yield return(null); if (contentPanel == null) { contentPanel = enhancedScroller.transform.GetChild(0).GetComponent <RectTransform>(); } if (contentPanel == null) { Debug.LogWarning(GetType().Name + ": No content panel found under the Enhanced Scroller. Can't scroll to bottom."); yield break; } var contentHeight = contentPanel.rect.height; var scrollRectHeight = scrollRect.GetComponent <RectTransform>().rect.height; var needToScroll = contentHeight > scrollRectHeight; if (needToScroll) { var ratio = scrollRectHeight / contentHeight; var timeout = Time.time + 10f; // Avoid infinite loops by maxing out at 10 seconds. while (scrollRect.verticalNormalizedPosition > 0.01f && Time.time < timeout) { var newPos = scrollRect.verticalNormalizedPosition - scrollSpeed * Time.deltaTime * ratio; scrollRect.verticalNormalizedPosition = Mathf.Max(0, newPos); yield return(null); } } scrollRect.verticalNormalizedPosition = 0; scrollCoroutine = null; }
/// <summary> /// This function will expand the scroller to accommodate the cells, reload the data to calculate the cell sizes, /// reset the scroller's size back, then reload the data once more to display the cells. /// </summary> private void ResizeScroller() { // capture the scroll rect size. // this will be used at the end of this method to determine the final scroll position var scrollRectSize = scroller.ScrollRectSize; // capture the scroller's position so we can smoothly scroll from it to the new cell var offset = _oldScrollPosition - scroller.ScrollSize; // capture the scroller dimensions so that we can reset them when we are done var rectTransform = scroller.GetComponent <RectTransform>(); var size = rectTransform.sizeDelta; // set the dimensions to the largest size possible to accommodate all the cells rectTransform.sizeDelta = new Vector2(size.x, float.MaxValue); // First Pass: reload the scroller so that it can populate the text UI elements in the cell view. // The content size fitter will determine how big the cells need to be on subsequent passes. _calculateLayout = true; scroller.ReloadData(); // calculate the total size required by all cells. This will be used when we determine // where to end up at after we reload the data on the second pass. _totalCellSize = scroller.padding.top + scroller.padding.bottom; for (var i = 1; i < _data.Count; i++) { _totalCellSize += _data[i].cellSize + (i < _data.Count - 1 ? scroller.spacing : 0); } // set the spacer to the entire scroller size. // this is necessary because we need some space to actually do a jump _data[0].cellSize = scrollRectSize; // reset the scroller size back to what it was originally rectTransform.sizeDelta = size; // Second Pass: reload the data once more with the newly set cell view sizes and scroller content size. _calculateLayout = false; scroller.ReloadData(); // set the scroll position to the previous cell (plus the offset of where the scroller currently is) so that we can jump to the new cell. scroller.ScrollPosition = (_totalCellSize - _data[_data.Count - 1].cellSize) + offset; }
/// <summary> /// This function will exand the scroller to accommodate the cells, reload the data to calculate the cell sizes, /// reset the scroller's size back, then reload the data once more to display the cells. /// </summary> private void ResizeScroller() { // capture the scroller dimensions so that we can reset them when we are done var rectTransform = scroller.GetComponent <RectTransform>(); var size = rectTransform.sizeDelta; // set the dimensions to the largest size possible to acommodate all the cells rectTransform.sizeDelta = new Vector2(size.x, float.MaxValue); // First Pass: reload the scroller so that it can populate the text UI elements in the cell view. // The content size fitter will determine how big the cells need to be on subsequent passes scroller.ReloadData(); // reset the scroller size back to what it was originally rectTransform.sizeDelta = size; // Second Pass: reload the data once more with the newly set cell view sizes and scroller content size scroller.ReloadData(); }
/// <summary> /// Populates the data with some random Lorum Ipsum text /// </summary> private void LoadData() { _data = new List <Data>(); _data.Add(new Data() { cellSize = 0, someText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam augue enim, scelerisque ac diam nec, efficitur aliquam orci. Vivamus laoreet, libero ut aliquet convallis, dolor elit auctor purus, eget dapibus elit libero at lacus. Aliquam imperdiet sem ultricies ultrices vestibulum. Proin feugiat et dui sit amet ultrices. Quisque porta lacus justo, non ornare nulla eleifend at. Nunc malesuada eget neque sit amet viverra. Donec et lectus ac lorem elementum porttitor. Praesent urna felis, dapibus eu nunc varius, varius tincidunt ante. Vestibulum vitae nulla malesuada, consequat justo eu, dapibus elit. Nulla tristique enim et convallis facilisis." }); _data.Add(new Data() { cellSize = 0, someText = "Nunc convallis, ipsum a porta viverra, tortor velit feugiat est, eget consectetur ex metus vel diam." }); _data.Add(new Data() { cellSize = 0, someText = "Phasellus laoreet vitae lectus sit amet venenatis. Duis scelerisque ultricies tincidunt. Cras ullamcorper lectus sed risus porttitor, id viverra urna venenatis. Maecenas in odio sed mi tempus porta et a justo. Nullam non ullamcorper est. Nam rhoncus nulla quis commodo aliquam. Maecenas pulvinar est sed ex iaculis, eu pretium tellus placerat. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Praesent in ipsum faucibus, fringilla lectus id, congue est. " }); _data.Add(new Data() { cellSize = 0, someText = "Fusce ex lectus." }); _data.Add(new Data() { cellSize = 0, someText = "Fusce mollis elementum sem euismod malesuada. Aenean et convallis turpis. Suspendisse potenti." }); _data.Add(new Data() { cellSize = 0, someText = "Fusce nec sapien orci. Pellentesque mollis ligula vitae interdum imperdiet. Aenean ultricies velit at turpis luctus, nec lacinia ligula malesuada. Nulla facilisi. Donec at nisi lorem. Aenean vestibulum velit velit, sed eleifend dui sodales in. Nunc vulputate, nulla non facilisis hendrerit, neque dolor lacinia orci, et fermentum nunc quam vel purus. Donec gravida massa non ullamcorper consectetur. Sed pellentesque leo ac ornare egestas. " }); _data.Add(new Data() { cellSize = 0, someText = "Curabitur non dignissim turpis, vel viverra elit. Cras in sem rhoncus, gravida velit ut, consectetur erat. Proin ac aliquet nulla. Mauris quis augue nisi. Sed purus magna, mollis sed massa ac, scelerisque lobortis leo. Nullam at facilisis ex. Nullam ut accumsan orci. Integer vitae dictum felis, quis tristique sem. Suspendisse potenti. Curabitur bibendum eleifend eros at porta. Ut malesuada consectetur arcu nec lacinia. " }); _data.Add(new Data() { cellSize = 0, someText = "Pellentesque pulvinar ac arcu fermentum interdum. Pellentesque gravida faucibus ipsum at blandit. Vestibulum pharetra erat sit amet feugiat sodales. Nunc et dui viverra tellus efficitur egestas. Sed ex mauris, eleifend in nisi sed, consequat tincidunt elit. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Proin vel bibendum enim. Etiam feugiat nulla ac dui commodo, eget vehicula est scelerisque. In metus neque, congue a justo ac, consequat lacinia neque. Vivamus non velit vitae ex dictum pharetra. Aliquam blandit nisi eget libero feugiat porta. " }); _data.Add(new Data() { cellSize = 0, someText = "Proin bibendum ligula a pulvinar convallis. Mauris tincidunt tempor ipsum id viverra. Vivamus congue ipsum venenatis tellus semper, vel venenatis mauris finibus. Vivamus a nisl in lacus fermentum varius. Mauris bibendum magna placerat risus interdum, vitae facilisis nulla pellentesque. Curabitur vehicula odio quis magna pulvinar, et lacinia ante bibendum. Morbi laoreet eleifend ante, quis luctus augue luctus sit amet. Sed consectetur enim et orci posuere euismod. Curabitur sollicitudin metus eu nisl dictum suscipit. " }); _data.Add(new Data() { cellSize = 0, someText = "Sed gravida augue ligula, tempus auctor ante rutrum sit amet. Vestibulum finibus magna ut viverra rhoncus. Vestibulum rutrum eu nibh interdum imperdiet. Curabitur ac nunc a turpis ultricies dictum. Phasellus in molestie eros. Morbi porta imperdiet odio sed pharetra. Cras blandit tincidunt ultricies. " }); _data.Add(new Data() { cellSize = 0, someText = "Integer pellentesque viverra orci, sollicitudin luctus dui rhoncus sed. Duis placerat at felis vel placerat. Mauris massa urna, scelerisque vitae posuere vitae, ultrices in nibh. Mauris posuere hendrerit viverra. In lacinia urna nibh, ut lobortis lectus finibus et. Aliquam arcu dolor, suscipit eget massa id, eleifend dapibus est. Quisque eget bibendum urna. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed condimentum pulvinar ornare. Aliquam venenatis eget nunc et euismod. " }); // capture the scroller dimensions so that we can reset them when we are done var rectTransform = scroller.GetComponent <RectTransform>(); var size = rectTransform.sizeDelta; // set the dimensions to the largest size possible to acommodate all the cells rectTransform.sizeDelta = new Vector2(size.x, float.MaxValue); // First Pass: reload the scroller so that it can populate the text UI elements in the cell view. // The content size fitter will determine how big the cells need to be on subsequent passes scroller.ReloadData(); // reset the scroller size back to what it was originally rectTransform.sizeDelta = size; // set up our frame countdown so that we can reload the scroller on subsequent frames _reloadScrollerFrameCountLeft = 1; }
/// <summary> /// This handles the toggle for the masks /// </summary> /// <param name="val">Is the mask on?</param> public void MaskToggle_OnValueChanged(bool val) { // set the mask component of each scroller vScroller.GetComponent <Mask>().enabled = val; hScroller.GetComponent <Mask>().enabled = val; }