您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

229 行
6.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using SUISS.Core;
  4. using UnityEngine;
  5. namespace SUISSEngine
  6. {
  7. public sealed class GameObjectManager : SingletonMonobehaviour<GameObjectManager>
  8. {
  9. protected override void Awake()
  10. {
  11. base.Awake();
  12. if (!this._isValidNewInstance)
  13. {
  14. return;
  15. }
  16. List<CIGBuilding> list = new List<CIGBuilding>();
  17. List<CIGResidentialBuilding> list2 = new List<CIGResidentialBuilding>();
  18. List<CIGCommercialBuilding> list3 = new List<CIGCommercialBuilding>();
  19. List<CIGCommunityBuilding> list4 = new List<CIGCommunityBuilding>();
  20. List<CIGDecoration> list5 = new List<CIGDecoration>();
  21. List<CIGUnlockBuilding> list6 = new List<CIGUnlockBuilding>();
  22. int count = this._additionalPrefabs.Count;
  23. for (int i = 0; i < count; i++)
  24. {
  25. CIGBuilding component = this._additionalPrefabs[i].GetComponent<CIGBuilding>();
  26. if (!(component == null))
  27. {
  28. this.AddToList<CIGResidentialBuilding>(list2, component);
  29. this.AddToList<CIGCommercialBuilding>(list3, component);
  30. this.AddToList<CIGCommunityBuilding>(list4, component);
  31. this.AddToList<CIGDecoration>(list5, component);
  32. this.AddToList<CIGUnlockBuilding>(list6, component);
  33. list.Add(component);
  34. }
  35. }
  36. this.SortList<CIGResidentialBuilding>(list2);
  37. this.SortList<CIGCommercialBuilding>(list3);
  38. this.SortList<CIGCommunityBuilding>(list4);
  39. this.SortList<CIGDecoration>(list5);
  40. this.AllBuildings = list.ToArray();
  41. this.Residential = list2.ToArray();
  42. this.Commercial = list3.ToArray();
  43. this.Community = list4.ToArray();
  44. this.Decoration = list5.ToArray();
  45. this.Unlock = list6.ToArray();
  46. this.CollectPrefabs();
  47. }
  48. public void Register(GameObject prefab)
  49. {
  50. if (this.stores.ContainsKey(prefab.name.ToLower()))
  51. {
  52. UnityEngine.Debug.LogError(string.Format("Prefab with name {0} registered more than once.", prefab.name));
  53. return;
  54. }
  55. GameObjectStore value = new GameObjectStore(prefab);
  56. this.stores.Add(prefab.name.ToLower(), value);
  57. }
  58. public void ReturnToStore(GameObject obj)
  59. {
  60. this.CollectPrefabs();
  61. if (!this.stores.ContainsKey(obj.name.ToLower()))
  62. {
  63. UnityEngine.Debug.LogError(string.Format("Object {0} disabled which is not registered, object will be destroyed instead.", obj.name));
  64. UnityEngine.Object.Destroy(obj);
  65. return;
  66. }
  67. obj.SetActive(false);
  68. this.stores[obj.name.ToLower()].objects.Add(obj);
  69. }
  70. public GameObject GetOrCreate(string name)
  71. {
  72. this.CollectPrefabs();
  73. name = name.ToLower();
  74. if (this.stores.ContainsKey(name))
  75. {
  76. UnityEngine.Debug.LogError(string.Format("Prefab with name {0} NOT registered.", name));
  77. return null;
  78. }
  79. GameObjectStore gameObjectStore = this.stores[name];
  80. if (gameObjectStore.objects.Count > 0)
  81. {
  82. GameObject gameObject = gameObjectStore.objects[gameObjectStore.objects.Count - 1];
  83. gameObjectStore.objects.RemoveAt(gameObjectStore.objects.Count - 1);
  84. gameObject.SetActive(true);
  85. return gameObject;
  86. }
  87. GameObject gameObject2 = UnityEngine.Object.Instantiate<GameObject>(gameObjectStore.prefab);
  88. gameObject2.name = gameObjectStore.prefab.name;
  89. return gameObject2;
  90. }
  91. public bool HavePrefabWithName(string name)
  92. {
  93. this.CollectPrefabs();
  94. return this.stores.ContainsKey(name.ToLower());
  95. }
  96. public GameObject GetPrefabWithName(string name)
  97. {
  98. this.CollectPrefabs();
  99. name = name.ToLower();
  100. if (this.stores.ContainsKey(name))
  101. {
  102. return this.stores[name].prefab;
  103. }
  104. return null;
  105. }
  106. public bool AddAdditionalPrefab(GameObject newPrefab)
  107. {
  108. if (!this._additionalPrefabs.Contains(newPrefab))
  109. {
  110. this._additionalPrefabs.Add(newPrefab);
  111. return true;
  112. }
  113. return false;
  114. }
  115. public List<GameObject> AdditionalPrefabs
  116. {
  117. get
  118. {
  119. return this._additionalPrefabs;
  120. }
  121. }
  122. public CIGResidentialBuilding[] Residential { get; private set; }
  123. public CIGCommercialBuilding[] Commercial { get; private set; }
  124. public CIGCommunityBuilding[] Community { get; private set; }
  125. public CIGDecoration[] Decoration { get; private set; }
  126. public CIGUnlockBuilding[] Unlock { get; private set; }
  127. public CIGBuilding[] AllBuildings { get; private set; }
  128. public CIGBuilding GetBuilding(string prefabName)
  129. {
  130. if (string.IsNullOrEmpty(prefabName))
  131. {
  132. return null;
  133. }
  134. int num = this.AllBuildings.Length;
  135. for (int i = 0; i < num; i++)
  136. {
  137. CIGBuilding cigbuilding = this.AllBuildings[i];
  138. if (cigbuilding.CachedName == prefabName)
  139. {
  140. return cigbuilding;
  141. }
  142. }
  143. return null;
  144. }
  145. private void CollectPrefabs()
  146. {
  147. if (this.prefabsCollected)
  148. {
  149. return;
  150. }
  151. if (this._additionalPrefabs != null)
  152. {
  153. int count = this._additionalPrefabs.Count;
  154. for (int i = 0; i < count; i++)
  155. {
  156. this.Register(this._additionalPrefabs[i]);
  157. }
  158. }
  159. this.prefabsCollected = true;
  160. }
  161. private void AddToList<T>(List<T> list, object element) where T : class
  162. {
  163. T t = element as T;
  164. if (t != null)
  165. {
  166. list.Add(t);
  167. }
  168. }
  169. private void SortList<T>(List<T> list) where T : CIGBuilding
  170. {
  171. list.Sort(delegate(T a, T b)
  172. {
  173. if (a.unlockLevels.Length != 0 || b.unlockLevels.Length != 0)
  174. {
  175. int num = Mathf.Min(a.unlockLevels.Length, b.unlockLevels.Length);
  176. for (int i = 0; i < num; i++)
  177. {
  178. if (a.unlockLevels[i] != b.unlockLevels[i])
  179. {
  180. return a.unlockLevels[i] - b.unlockLevels[i];
  181. }
  182. }
  183. return a.unlockLevels.Length - b.unlockLevels.Length;
  184. }
  185. if (a.baseHappinessValue != b.baseHappinessValue)
  186. {
  187. return b.baseHappinessValue - a.baseHappinessValue;
  188. }
  189. decimal value = a.initialPurchasePrice.GetValue("Cash");
  190. decimal value2 = b.initialPurchasePrice.GetValue("Cash");
  191. if (value < value2)
  192. {
  193. return 1;
  194. }
  195. if (value > value2)
  196. {
  197. return -1;
  198. }
  199. return a.LocalName.Translate().CompareTo(b.LocalName.Translate());
  200. });
  201. }
  202. [SerializeField]
  203. private List<GameObject> _additionalPrefabs;
  204. private Dictionary<string, GameObjectStore> stores = new Dictionary<string, GameObjectStore>();
  205. private bool prefabsCollected;
  206. }
  207. }