Created
October 18, 2017 03:32
-
-
Save heshuimu/383aec2f70dbfe5860c1296dfcd8e1ac to your computer and use it in GitHub Desktop.
Pooling List
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IK4UIPoolingListCell<DataType> | |
{ | |
void Show(); | |
void Hide(); | |
void UpdateInformation(int index, DataType data); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public abstract class K4UIPoolingList<DataType, CellType>: MonoBehaviour where CellType : IK4UIPoolingListCell<DataType> | |
{ | |
[SerializeField] | |
private Transform listContentTransform; | |
[SerializeField] | |
private bool isTemplatePrefab; | |
[SerializeField] | |
private bool reuseTemplate = true; | |
[SerializeField] | |
private GameObject cellTemplate; | |
protected List<CellType> listCellPool = new List<CellType>(); | |
private bool isTemplateReused; | |
public void UpdateList(ICollection<DataType> dataCollection) | |
{ | |
if(cellTemplate == null) | |
{ | |
Debug.LogErrorFormat("{0}, cellTemplate is null. Did you forget to set it in the Inspector??", name); | |
return; | |
} | |
if(!reuseTemplate) | |
{ | |
CellType cellCtrl = cellTemplate.GetComponent<CellType>(); | |
if(cellCtrl == null) | |
{ | |
Debug.LogErrorFormat("{0}: Cell Template {1} does not have the controller implementing IK4UIPoolingListCell. ", name, cellTemplate.name); | |
return; | |
} | |
cellCtrl.Hide(); | |
} | |
if(reuseTemplate && !isTemplatePrefab && !isTemplateReused) | |
{ | |
CellType cellCtrl = cellTemplate.GetComponent<CellType>(); | |
if(cellCtrl == null) | |
{ | |
Debug.LogErrorFormat("{0}: Cell Template {1} does not have the controller implementing IK4UIPoolingListCell. ", name, cellTemplate.name); | |
return; | |
} | |
listCellPool.Add(cellCtrl); | |
isTemplateReused = true; | |
} | |
while(listCellPool.Count < dataCollection.Count) | |
{ | |
GameObject go = Instantiate(cellTemplate, listContentTransform); | |
CellType cellCtrl = go.GetComponent<CellType>(); | |
if(cellCtrl == null) | |
{ | |
Destroy(go); | |
Debug.LogErrorFormat("{0}: Cell Template {1} does not have the controller implementing IK4UIPoolingListCell. ", name, cellTemplate.name); | |
return; | |
} | |
go.transform.SetParent(listContentTransform); | |
listCellPool.Add(cellCtrl); | |
} | |
int index = 0; | |
foreach(DataType d in dataCollection) | |
{ | |
listCellPool[index].Show(); | |
listCellPool[index].UpdateInformation(index, d); | |
index++; | |
} | |
while(index < listCellPool.Count) | |
{ | |
listCellPool[index].Hide(); | |
index++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment