| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// This class is a generic implementantion for a view that list items. It should be use when you want to list items in |
| | 7 | | /// |
| | 8 | | /// The general logic of this class is that it will instanciate one adapter per one item in the list you set. The adapt |
| | 9 | | /// while this class will manage the availability of the adapters. This way you can switch adapter to show them in dife |
| | 10 | | /// |
| | 11 | | /// You will need to implement at least one adapter in the AddAdapters function and instanciated it as a children of th |
| | 12 | | /// |
| | 13 | | /// You are required to implement the AddAdapters with your own adapter logic, this is done in order to mantain it more |
| | 14 | | /// </summary> |
| | 15 | | public class ListView<T> : MonoBehaviour |
| | 16 | | { |
| | 17 | |
|
| | 18 | | public Transform contentPanelTransform; |
| | 19 | | public GameObject emptyContentMark; |
| | 20 | |
|
| 105 | 21 | | protected List<T> contentList = new List<T>(); |
| | 22 | | public void SetContent(List<T> content) |
| | 23 | | { |
| 0 | 24 | | contentList = content; |
| 0 | 25 | | RefreshDisplay(); |
| 0 | 26 | | } |
| | 27 | |
|
| | 28 | | public virtual void RefreshDisplay() |
| | 29 | | { |
| 0 | 30 | | RemoveAdapters(); |
| 0 | 31 | | AddAdapters(); |
| 0 | 32 | | CheckEmptyContent(); |
| 0 | 33 | | } |
| | 34 | |
|
| 0 | 35 | | public virtual void AddAdapters() { } |
| | 36 | |
|
| | 37 | | public virtual void RemoveAdapters() |
| | 38 | | { |
| 0 | 39 | | if (contentPanelTransform == null || |
| | 40 | | contentPanelTransform.transform == null || |
| | 41 | | contentPanelTransform.transform.childCount <= 0) |
| 0 | 42 | | return; |
| | 43 | |
|
| | 44 | |
|
| 0 | 45 | | for (int i = 0; i < contentPanelTransform.transform.childCount; i++) |
| | 46 | | { |
| 0 | 47 | | GameObject toRemove = contentPanelTransform.transform.GetChild(i).gameObject; |
| 0 | 48 | | if (toRemove != null) |
| 0 | 49 | | Destroy(toRemove); |
| | 50 | | } |
| 0 | 51 | | } |
| | 52 | |
|
| | 53 | | private void CheckEmptyContent() |
| | 54 | | { |
| 0 | 55 | | bool contentIsEmpty = contentList.Count == 0; |
| | 56 | |
|
| 0 | 57 | | if (contentPanelTransform != null) |
| 0 | 58 | | contentPanelTransform.gameObject.SetActive(!contentIsEmpty); |
| | 59 | |
|
| 0 | 60 | | if (emptyContentMark != null) |
| 0 | 61 | | emptyContentMark.SetActive(contentIsEmpty); |
| 0 | 62 | | } |
| | 63 | | } |