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(GameObject item, int itemCount, Action callbackSetUp = null) where T : Component { if (item == null) { Debug.LogError("[uGUIListViewer.cs]項目のテンプレートにnullが指定されました"); return; } this.m_TempItem = item; this.Show(itemCount, callbackSetUp); } public void Show(int itemCount, Action callbackSetUp = null) where T : Component { if (this.m_TempItem == null) { Debug.LogError("[uGUIListViewer.cs]項目のテンプレートが設定されていません"); return; } T component = this.m_TempItem.GetComponent(); if (component == null) { Debug.LogError("[uGUIListViewer.cs]項目のテンプレートに「" + typeof(T).Name + "」コンポーネントが見つかりませんでした"); return; } this.SetUpList(component, itemCount, callbackSetUp); } protected void SetUpList(T tempItem, int itemCount, Action 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(tempItem); arg.gameObject.SetActive(true); Transform component = arg.GetComponent(); 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 list = new List(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; }