using System.Collections; using System.Collections.Generic; using UnityEngine; public abstract class UIListView : MonoBehaviour, IListView where T : UIListCell { public abstract int Count { get; } [SerializeField] GameObject mTplCell; [SerializeField] Transform mCtnCells; protected List mCellList; public virtual void Show(bool pShow) { gameObject.SetActive(pShow); } public virtual void LoadData() { if (mCellList == null) { mCellList = new List(); } for (int i = 0; i < Count; i++) { T tCell = null; if (i < mCellList.Count) { tCell = mCellList[i]; } else { tCell = SpawnCell(); mCellList.Add(tCell); } tCell.gameObject.SetActive(true); tCell.SetIndex(i); ConfigCell(tCell); } HideUnused(); } public virtual void ClearSelection() { } protected abstract void ConfigCell(T pCell); private T SpawnCell() { GameObject tCellOb = Instantiate(mTplCell, mCtnCells); tCellOb.transform.localScale = Vector3.one; return tCellOb.GetComponent(); } private void HideUnused() { for (int i = Count; i < mCellList.Count; i++) { mCellList[i].gameObject.SetActive(false); } } } public interface IListView { void Show(bool pShow); void LoadData(); void ClearSelection(); }