using UnityEngine; using UnityEngine.UI; using EnhancedUI.EnhancedScroller; using System.Collections.Generic; using System; namespace EnhancedScrollerDemos.NestedScrollers { /// /// The master cell view contains a detail EnhancedScroller /// public class MasterCellView : EnhancedScrollerCellView, IEnhancedScrollerDelegate { private bool reloadDataNextFrame = false; /// /// The detail scroller containing our detail cells /// public EnhancedScroller detailScroller; /// /// The list of detail cells for this master cell /// private MasterData _data; /// /// Detail cell prefab to instantiate /// public EnhancedScrollerCellView detailCellViewPrefab; /// /// Sets the detail scroller delegate and data /// /// public void SetData(MasterData data) { // set up delegates and callbacks detailScroller.Delegate = this; detailScroller.scrollerScrolled = ScrollerScrolled; // assign data and flag that the detail scroller needs to be reloaded. // we have to reload on the next frame through the update so that the // main scroller has time to set up the master cell views first. _data = data; reloadDataNextFrame = true; } /// /// Check to see if the scroller needs to be reloaded /// void Update() { if (reloadDataNextFrame) { // scroller needs reloaded, so we unflag and reload the detail data reloadDataNextFrame = false; detailScroller.ReloadData(_data.normalizedScrollPosition); } } #region EnhancedScroller Handlers /// /// This tells the scroller the number of cells that should have room allocated. This should be the length of your data array. /// /// The scroller that is requesting the data size /// The number of cells public int GetNumberOfCells(EnhancedScroller scroller) { // in this example, we just pass the number of our detail data elements return _data.childData.Count; } /// /// This tells the scroller what the size of a given cell will be. Cells can be any size and do not have /// to be uniform. For vertical scrollers the cell size will be the height. For horizontal scrollers the /// cell size will be the width. /// /// The scroller requesting the cell size /// The index of the data that the scroller is requesting /// The size of the cell public float GetCellViewSize(EnhancedScroller scroller, int dataIndex) { // in this example, we set the cells at 100 pixels wide return 100f; } /// /// Gets the cell to be displayed. You can have numerous cell types, allowing variety in your list. /// Some examples of this would be headers, footers, and other grouping cells. /// /// The scroller requesting the cell /// The index of the data that the scroller is requesting /// The index of the list. This will likely be different from the dataIndex if the scroller is looping /// The cell for the scroller to use public EnhancedScrollerCellView GetCellView(EnhancedScroller scroller, int dataIndex, int cellIndex) { // first, we get a cell from the scroller by passing a prefab. // if the scroller finds one it can recycle it will do so, otherwise // it will create a new cell. DetailCellView detailCellView = scroller.GetCellView(detailCellViewPrefab) as DetailCellView; // set the name of the game object to the cell's data index. // this is optional, but it helps up debug the objects in // the scene hierarchy. detailCellView.name = "Detail Cell " + dataIndex.ToString(); // in this example, we just pass the data to our cell's view which will update its UI detailCellView.SetData(_data.childData[dataIndex]); // return the cell to the scroller return detailCellView; } /// /// Capture the scroll position to use when the scroller is recycled /// private void ScrollerScrolled(EnhancedScroller scroller, Vector2 val, float scrollPosition) { _data.normalizedScrollPosition = scroller.NormalizedScrollPosition; } #endregion } }