uGUIListViewer.cs 2.8 KB

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