uGUIListViewer.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class uGUIListViewer : MonoBehaviour
  5. {
  6. public Transform parentItemArea
  7. {
  8. get
  9. {
  10. return this.m_ParentItemArea;
  11. }
  12. set
  13. {
  14. this.m_ParentItemArea = value;
  15. }
  16. }
  17. public GameObject tempItem
  18. {
  19. get
  20. {
  21. return this.m_TempItem;
  22. }
  23. set
  24. {
  25. this.m_TempItem = value;
  26. }
  27. }
  28. public GameObject[] ItemArray
  29. {
  30. get
  31. {
  32. return this.m_ItemArray;
  33. }
  34. }
  35. private void Awake()
  36. {
  37. if (this.m_TempItem)
  38. {
  39. this.m_TempItem.SetActive(false);
  40. }
  41. }
  42. public void Show<T>(GameObject item, int itemCount, Action<int, T> callbackSetUp = null) where T : Component
  43. {
  44. if (item == null)
  45. {
  46. Debug.LogError("[uGUIListViewer.cs]項目のテンプレートにnullが指定されました");
  47. return;
  48. }
  49. this.m_TempItem = item;
  50. this.Show<T>(itemCount, callbackSetUp);
  51. }
  52. public void Show<T>(int itemCount, Action<int, T> callbackSetUp = null) where T : Component
  53. {
  54. if (this.m_TempItem == null)
  55. {
  56. Debug.LogError("[uGUIListViewer.cs]項目のテンプレートが設定されていません");
  57. return;
  58. }
  59. T component = this.m_TempItem.GetComponent<T>();
  60. if (component == null)
  61. {
  62. Debug.LogError("[uGUIListViewer.cs]項目のテンプレートに「" + typeof(T).Name + "」コンポーネントが見つかりませんでした");
  63. return;
  64. }
  65. this.SetUpList<T>(component, itemCount, callbackSetUp);
  66. }
  67. protected void SetUpList<T>(T tempItem, int itemCount, Action<int, T> callbackSetUp) where T : Component
  68. {
  69. Transform parentItemArea = this.m_ParentItemArea;
  70. if (parentItemArea == null)
  71. {
  72. Debug.LogError("[uGUIListViewer.cs]項目を並べるエリアが存在しません\nParent Item Area に項目を並べたいオブジェクトを設定してください");
  73. return;
  74. }
  75. if (tempItem == null)
  76. {
  77. Debug.LogError("[uGUIListViewer.cs]項目のテンプレートが存在しません\nTemp Item に項目として並べたいオブジェクトを設定してください");
  78. return;
  79. }
  80. this.ResetList();
  81. this.m_ItemArray = new GameObject[itemCount];
  82. for (int i = 0; i < itemCount; i++)
  83. {
  84. T arg = UnityEngine.Object.Instantiate<T>(tempItem);
  85. arg.gameObject.SetActive(true);
  86. Transform component = arg.GetComponent<Transform>();
  87. if (component.parent != parentItemArea)
  88. {
  89. component.SetParent(parentItemArea, false);
  90. }
  91. if (callbackSetUp != null)
  92. {
  93. callbackSetUp(i, arg);
  94. }
  95. this.m_ItemArray[i] = arg.gameObject;
  96. }
  97. }
  98. public void ResetList()
  99. {
  100. if (this.m_ItemArray != null)
  101. {
  102. for (int i = 0; i < this.m_ItemArray.Length; i++)
  103. {
  104. this.m_ItemArray[i].transform.SetParent(null, false);
  105. UnityEngine.Object.Destroy(this.m_ItemArray[i]);
  106. }
  107. }
  108. this.m_ItemArray = null;
  109. }
  110. public void RemoveItem(GameObject obj)
  111. {
  112. if (this.m_ItemArray != null)
  113. {
  114. List<GameObject> list = new List<GameObject>(this.m_ItemArray.Length);
  115. for (int i = 0; i < this.m_ItemArray.Length; i++)
  116. {
  117. if (obj != this.m_ItemArray[i])
  118. {
  119. list.Add(this.m_ItemArray[i]);
  120. }
  121. else
  122. {
  123. this.m_ItemArray[i].transform.SetParent(null, false);
  124. UnityEngine.Object.Destroy(this.m_ItemArray[i]);
  125. }
  126. }
  127. this.m_ItemArray = list.ToArray();
  128. }
  129. }
  130. [SerializeField]
  131. private Transform m_ParentItemArea;
  132. [SerializeField]
  133. private GameObject m_TempItem;
  134. private GameObject[] m_ItemArray;
  135. }