/// <summary> /// Creates new item cells as needed, based on the panel's current bounds. /// This will reset the scrollview's position. /// </summary> public void RebuildItems() { float size = ScrollView.movement == UIScrollView.Movement.Horizontal ? ItemSize.x : ItemSize.y; float panelSize = GetPanelSize(); int itemCount = (Mathf.CeilToInt(panelSize / size) + 2) * groupSize; // Create missing items if needed. for (int i = items.Count; i < itemCount; i++) { ScrollerItem item = ( Prefab != null ? NGUITools.AddChild(gameObject, Prefab).GetComponent <ScrollerItem>() : Instantiator() ); item.Initialize(); items.Add(item); } // Toggle items on/off based on whether the item is pooled more than what we need to display. for (int i = 0; i < items.Count; i++) { items[i].Object.SetActive(i < totalSize); } // Reset scrollview position ResetPosition(); }
/// <summary> /// Updates the specified item by invoking the item update event. /// </summary> void UpdateItem(ScrollerItem item) { // If index is out of bounds, we don't fire the event. if (item.Index < 0 || item.Index >= totalSize) { item.Object.SetActive(false); return; } item.Object.SetActive(true); if (OnItemUpdate != null) { OnItemUpdate(item); } }