void Update() { if (_reloadData) { // if the reload flag is true, then reload the data ReloadData(); } // if the scroll rect size has changed and looping is on, // or the loop setting has changed, then we need to resize if ( (loop && _lastScrollRectSize != ScrollRectSize) || (loop != _lastLoop) ) { _Resize(true); _lastScrollRectSize = ScrollRectSize; _lastLoop = loop; } // update the scroll bar visibility if it has changed if (_lastScrollbarVisibility != scrollbarVisibility) { ScrollbarVisibility = scrollbarVisibility; _lastScrollbarVisibility = scrollbarVisibility; } // determine if the scroller has started or stopped scrolling // and call the delegate if so. if (LinearVelocity != 0 && !IsScrolling) { IsScrolling = true; if (scrollerScrollingChanged != null) scrollerScrollingChanged(this, true); } else if (LinearVelocity == 0 && IsScrolling) { IsScrolling = false; if (scrollerScrollingChanged != null) scrollerScrollingChanged(this, false); } }
/// <summary> /// Caches and initializes the scroller /// </summary> void Awake() { GameObject go; // cache some components _scrollRect = this.GetComponent<ScrollRect>(); _scrollRectTransform = _scrollRect.GetComponent<RectTransform>(); // destroy any content objects if they exist. Likely there will be // one at design time because Unity gives errors if it can't find one. if (_scrollRect.content != null) { DestroyImmediate(_scrollRect.content.gameObject); } // Create a new active cell view container with a layout group go = new GameObject("Container", typeof(RectTransform)); go.transform.SetParent(_scrollRectTransform, false); if (scrollDirection == ScrollDirectionEnum.Vertical) go.AddComponent<VerticalLayoutGroup>(); else go.AddComponent<HorizontalLayoutGroup>(); _container = go.GetComponent<RectTransform>(); // set the containers anchor and pivot if (scrollDirection == ScrollDirectionEnum.Vertical) { _container.anchorMin = new Vector2(0, 1); _container.anchorMax = Vector2.one; _container.pivot = new Vector2(0.5f, 1f); } else { _container.anchorMin = Vector2.zero; _container.anchorMax = new Vector2(0, 1f); _container.pivot = new Vector2(0, 0.5f); } _container.offsetMax = Vector2.zero; _container.offsetMin = Vector2.zero; _container.localScale = Vector3.one; _scrollRect.content = _container; // cache the scrollbar if it exists if (scrollDirection == ScrollDirectionEnum.Vertical) { _scrollbar = _scrollRect.verticalScrollbar; } else { _scrollbar = _scrollRect.horizontalScrollbar; } // cache the layout group and set up its spacing and padding _layoutGroup = _container.GetComponent<HorizontalOrVerticalLayoutGroup>(); _layoutGroup.spacing = spacing; _layoutGroup.padding = padding; _layoutGroup.childAlignment = TextAnchor.UpperLeft; _layoutGroup.childForceExpandHeight = (scrollDirection == ScrollDirectionEnum.Vertical ? false : true); _layoutGroup.childForceExpandWidth = (scrollDirection == ScrollDirectionEnum.Vertical ? true : false); // force the scroller to scroll in the direction we want _scrollRect.horizontal = scrollDirection == ScrollDirectionEnum.Horizontal; _scrollRect.vertical = scrollDirection == ScrollDirectionEnum.Vertical; // create the padder objects go = new GameObject("First Padder", typeof(RectTransform), typeof(LayoutElement)); go.transform.SetParent(_container, false); _firstPadder = go.GetComponent<LayoutElement>(); go = new GameObject("Last Padder", typeof(RectTransform), typeof(LayoutElement)); go.transform.SetParent(_container, false); _lastPadder = go.GetComponent<LayoutElement>(); // create the recycled cell view container go = new GameObject("Recycled Cells", typeof(RectTransform)); go.transform.SetParent(_scrollRect.transform, false); _recycledCellViewContainer = go.GetComponent<RectTransform>(); _recycledCellViewContainer.gameObject.SetActive(false); // set up the last values for updates _lastScrollRectSize = ScrollRectSize; _lastLoop = loop; _lastScrollbarVisibility = scrollbarVisibility; }