90 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			90 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			C#
		
	
	
	
| using System.Collections;
 | |
| using System.Collections.Generic;
 | |
| using UnityEngine;
 | |
| 
 | |
| namespace MMO
 | |
| {
 | |
|     public abstract class MMOListView<T> : IMMOListView where T : MMOListCell
 | |
|     {
 | |
|         public abstract int Count
 | |
|         {
 | |
|             get;
 | |
|         }
 | |
| 
 | |
|         [SerializeField] GameObject mTplCell;
 | |
|         [SerializeField] Transform mCtnCells;
 | |
| 
 | |
|         protected List<T> mCellList;
 | |
| 
 | |
|         public override void Show(bool pShow)
 | |
|         {
 | |
|             gameObject.SetActive(pShow);
 | |
|         }
 | |
| 
 | |
|         public override void LoadData()
 | |
|         {
 | |
|             BeforeLoad();
 | |
| 
 | |
|             if (mCellList == null)
 | |
|             {
 | |
|                 mCellList = new List<T>();
 | |
|             }
 | |
| 
 | |
|             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 override void ClearSelection()
 | |
|         {
 | |
| 
 | |
|         }
 | |
| 
 | |
|         protected virtual void BeforeLoad()
 | |
|         {
 | |
| 
 | |
|         }
 | |
| 
 | |
|         protected abstract void ConfigCell(T pCell);
 | |
| 
 | |
|         private T SpawnCell()
 | |
|         {
 | |
|             GameObject tCellOb = Instantiate(mTplCell, mCtnCells);
 | |
| 
 | |
|             tCellOb.transform.localScale = Vector3.one;
 | |
| 
 | |
|             return tCellOb.GetComponent<T>();
 | |
|         }
 | |
| 
 | |
|         private void HideUnused()
 | |
|         {
 | |
|             for (int i = Count; i < mCellList.Count; i++)
 | |
|             {
 | |
|                 mCellList[i].gameObject.SetActive(false);
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public abstract class IMMOListView : MonoBehaviour
 | |
|     {
 | |
|         public abstract void Show(bool pShow);
 | |
|         public abstract void LoadData();
 | |
|         public abstract void ClearSelection();
 | |
|     }
 | |
| } |