public class MyInfinitePanel : MonoBehaviour
{
class ItemInfo
{
public int m_index;
public string m_someInfoText;
public float m_someMoreInfo;
// You get the idea :)
}
// These components references are set via drag & drop in Editor.
// Our grid contains 3 ItemInfoDisplays because each ItemInfoDisplay fills the panel's clip region, but it
// should work in case where we display N items simultaneously and there are N+2 ItemInfoDisplays in the array
public ItemInfoDisplay[] m_itemInfoDisplays;
public UIDraggablePanel m_draggablePanel;
public UICenterOnChild m_centerOnChild;
// A large array of ItemInfo (which contains "info" about an "item", which can be fed
// to a ItemInfoDisplay which will update to display this info).
// Not shown: how we populate this array, but it's not important really.
ItemInfo[] m_items;
int m_currentItemIndex = 0;
void Start()
{
// Hook up to onFinished, so we can rearrange the display to keep the scrolling infinite
if(m_centerOnChild)
{
m_centerOnChild.onFinished += OnCenterOnChildFinished;
}
}
void OnCenterOnChildFinished()
{
if(m_centerOnChild != null)
{
GameObject go = m_centerOnChild.centeredObject;
if(go != null)
{
ItemInfoDisplay centeredDisplay = go.GetComponent<ItemInfoDisplay>();
if(centeredDisplay != null)
{
// Get the index from the currently-selected ItemInfoDisplay, and set that
// as the currently selected item index
int selectedIndex = centeredDisplay.m_index;
if(selectedIndex != m_currentItemIndex)
{
m_currentItemIndex = centeredDisplay.m_index;
// Re-center panel if necessary
if(m_draggablePanel)
{
int lastIndex = m_itemInfoDisplays.Length - 1;
if(centeredDisplay == m_itemInfoDisplays[0])
{
BumpEntries(centeredDisplay.entryPanelHeight, -1);
}
else if (centeredDisplay == m_itemInfoDisplays[lastIndex])
{
BumpEntries(centeredDisplay.entryPanelHeight, 1);
}
}
}
}
}
}
}
// Bump all the display entries along 1 (either up or down), so we
// can maintain an infinitely-scrolling carousel
void BumpEntries(float panelHeight, int bumpDir)
{
// This is for a vertically-scrolling panel
m_draggablePanel
.MoveRelative(new Vector3
(0,
-panelHeight
* bumpDir,
0));
for(int i = 0; i < m_itemInfoDisplays.Length; ++i)
{
ItemInfoDisplay itemInfoDisplay = m_itemInfoDisplays[i];
if(itemInfoDisplay != null)
{
int nextIndex = (int)Mathf.Repeat(itemInfoDisplay.m_index + bumpDir, m_items.Length);
// ItemInfoDisplay has this SetDisplay method which takes the information from an ItemInfo
// and applies it to the relevant widgets so that the display reflects the information. Also
// tells the itemInfoDisplay the index of the item its displaying.
itemInfoDisplay.SetDisplay(m_items[nextIndex], nextIndex);
}
}
}
}