BaseCreateViewerCtrl.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public abstract class BaseCreateViewerCtrl : MonoBehaviour
  6. {
  7. protected void CreateViewer<K, V>(Dictionary<K, V> dicData)
  8. {
  9. this.CreateViewer<K, V>(dicData, false);
  10. }
  11. public void CreateViewerWhenDataNotExist<K, V>(Dictionary<K, V> dicData)
  12. {
  13. this.CreateViewer<K, V>(dicData, true);
  14. }
  15. private void CreateViewer<K, V>(Dictionary<K, V> dicData, bool CanCreateWhenDataNotExist)
  16. {
  17. if (!CanCreateWhenDataNotExist && !this.HasDicData<K, V>(dicData))
  18. {
  19. return;
  20. }
  21. this.ExecuteTemplateOperation();
  22. }
  23. public void CreateViewer<T>(List<T> listData)
  24. {
  25. if (listData == null || listData.Count == 0)
  26. {
  27. return;
  28. }
  29. this.ExecuteTemplateOperation();
  30. }
  31. public void CreateViewer<T>(T data)
  32. {
  33. if (data == null)
  34. {
  35. return;
  36. }
  37. this.ExecuteTemplateOperation();
  38. }
  39. private void ExecuteTemplateOperation()
  40. {
  41. this.DelChildGameObject(this.m_goUnitParent);
  42. this.ValidateData();
  43. this.m_goUnitPrefab = this.GetUnitPrefab(this.m_unitPrefabPath);
  44. this.SetDataForViewer();
  45. this.Reposition();
  46. }
  47. private GameObject GetUnitPrefab(string unitPrefabPath)
  48. {
  49. this.m_goUnitPrefab = (this.m_goUnitPrefab ?? (Resources.Load(unitPrefabPath) as GameObject));
  50. return this.m_goUnitPrefab;
  51. }
  52. protected void DelChildGameObject(GameObject goParent)
  53. {
  54. IEnumerator enumerator = goParent.transform.GetEnumerator();
  55. try
  56. {
  57. while (enumerator.MoveNext())
  58. {
  59. object obj = enumerator.Current;
  60. Transform transform = (Transform)obj;
  61. UnityEngine.Object.Destroy(transform.gameObject);
  62. }
  63. }
  64. finally
  65. {
  66. IDisposable disposable;
  67. if ((disposable = (enumerator as IDisposable)) != null)
  68. {
  69. disposable.Dispose();
  70. }
  71. }
  72. goParent.transform.DetachChildren();
  73. }
  74. protected void DelChildGameObject()
  75. {
  76. IEnumerator enumerator = this.m_goUnitParent.transform.GetEnumerator();
  77. try
  78. {
  79. while (enumerator.MoveNext())
  80. {
  81. object obj = enumerator.Current;
  82. Transform transform = (Transform)obj;
  83. UnityEngine.Object.Destroy(transform.gameObject);
  84. }
  85. }
  86. finally
  87. {
  88. IDisposable disposable;
  89. if ((disposable = (enumerator as IDisposable)) != null)
  90. {
  91. disposable.Dispose();
  92. }
  93. }
  94. this.m_goUnitParent.transform.DetachChildren();
  95. }
  96. protected void Reposition()
  97. {
  98. UITable component = this.m_goUnitParent.GetComponent<UITable>();
  99. if (component)
  100. {
  101. component.Reposition();
  102. return;
  103. }
  104. UIGrid component2 = this.m_goUnitParent.GetComponent<UIGrid>();
  105. if (component2)
  106. {
  107. component2.Reposition();
  108. return;
  109. }
  110. }
  111. public static void Reposition(GameObject parent)
  112. {
  113. UITable component = parent.GetComponent<UITable>();
  114. if (component)
  115. {
  116. component.Reposition();
  117. return;
  118. }
  119. UIGrid component2 = parent.GetComponent<UIGrid>();
  120. if (component2)
  121. {
  122. component2.Reposition();
  123. return;
  124. }
  125. }
  126. protected virtual void ValidateData()
  127. {
  128. }
  129. protected void SetTransformInfo(GameObject copyPrefab)
  130. {
  131. this.SetTransformInfo(this.m_goUnitParent, copyPrefab);
  132. }
  133. protected void SetTransformInfo(GameObject parent, GameObject copyPrefabs)
  134. {
  135. copyPrefabs.transform.parent = parent.transform;
  136. copyPrefabs.transform.localScale = Vector3.one;
  137. copyPrefabs.transform.localPosition = Vector3.zero;
  138. copyPrefabs.transform.rotation = Quaternion.identity;
  139. }
  140. public bool HasDicData<K, V>(Dictionary<K, V> dicData)
  141. {
  142. return dicData != null && dicData.Count != 0;
  143. }
  144. public bool HasListData<T>(List<T> listData)
  145. {
  146. return listData != null && listData.Count > 0;
  147. }
  148. protected abstract void SetDataForViewer();
  149. protected GameObject m_goUnitParent;
  150. protected GameObject m_goUnitPrefab;
  151. protected string m_unitPrefabPath;
  152. private const float DURATION_TO_FADE = 0.5f;
  153. }