123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- public class uGUIListViewer : MonoBehaviour
- {
- public Transform parentItemArea
- {
- get
- {
- return this.m_ParentItemArea;
- }
- set
- {
- this.m_ParentItemArea = value;
- }
- }
- public GameObject tempItem
- {
- get
- {
- return this.m_TempItem;
- }
- set
- {
- this.m_TempItem = value;
- }
- }
- public GameObject[] ItemArray
- {
- get
- {
- return this.m_ItemArray;
- }
- }
- private void Awake()
- {
- if (this.m_TempItem)
- {
- this.m_TempItem.SetActive(false);
- }
- }
- public void Show<T>(GameObject item, int itemCount, Action<int, T> callbackSetUp = null) where T : Component
- {
- if (item == null)
- {
- Debug.LogError("[uGUIListViewer.cs]項目のテンプレートにnullが指定されました");
- return;
- }
- this.m_TempItem = item;
- this.Show<T>(itemCount, callbackSetUp);
- }
- public void Show<T>(int itemCount, Action<int, T> callbackSetUp = null) where T : Component
- {
- if (this.m_TempItem == null)
- {
- Debug.LogError("[uGUIListViewer.cs]項目のテンプレートが設定されていません");
- return;
- }
- T component = this.m_TempItem.GetComponent<T>();
- if (component == null)
- {
- Debug.LogError("[uGUIListViewer.cs]項目のテンプレートに「" + typeof(T).Name + "」コンポーネントが見つかりませんでした");
- return;
- }
- this.SetUpList<T>(component, itemCount, callbackSetUp);
- }
- protected void SetUpList<T>(T tempItem, int itemCount, Action<int, T> callbackSetUp) where T : Component
- {
- Transform parentItemArea = this.m_ParentItemArea;
- if (parentItemArea == null)
- {
- Debug.LogError("[uGUIListViewer.cs]項目を並べるエリアが存在しません\nParent Item Area に項目を並べたいオブジェクトを設定してください");
- return;
- }
- if (tempItem == null)
- {
- Debug.LogError("[uGUIListViewer.cs]項目のテンプレートが存在しません\nTemp Item に項目として並べたいオブジェクトを設定してください");
- return;
- }
- this.ResetList();
- this.m_ItemArray = new GameObject[itemCount];
- for (int i = 0; i < itemCount; i++)
- {
- T arg = UnityEngine.Object.Instantiate<T>(tempItem);
- arg.gameObject.SetActive(true);
- Transform component = arg.GetComponent<Transform>();
- if (component.parent != parentItemArea)
- {
- component.SetParent(parentItemArea, false);
- }
- if (callbackSetUp != null)
- {
- callbackSetUp(i, arg);
- }
- this.m_ItemArray[i] = arg.gameObject;
- }
- }
- public void ResetList()
- {
- if (this.m_ItemArray != null)
- {
- for (int i = 0; i < this.m_ItemArray.Length; i++)
- {
- this.m_ItemArray[i].transform.SetParent(null, false);
- UnityEngine.Object.Destroy(this.m_ItemArray[i]);
- }
- }
- this.m_ItemArray = null;
- }
- public void RemoveItem(GameObject obj)
- {
- if (this.m_ItemArray != null)
- {
- List<GameObject> list = new List<GameObject>(this.m_ItemArray.Length);
- for (int i = 0; i < this.m_ItemArray.Length; i++)
- {
- if (obj != this.m_ItemArray[i])
- {
- list.Add(this.m_ItemArray[i]);
- }
- else
- {
- this.m_ItemArray[i].transform.SetParent(null, false);
- UnityEngine.Object.Destroy(this.m_ItemArray[i]);
- }
- }
- this.m_ItemArray = list.ToArray();
- }
- }
- [SerializeField]
- private Transform m_ParentItemArea;
- [SerializeField]
- private GameObject m_TempItem;
- private GameObject[] m_ItemArray;
- }
|