ManagerResource.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using UnityEditor;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. using UnityEngine.Events;
  5. using System;
  6. using System.Collections;
  7. using System.Linq.Expressions;
  8. using System.Collections.Generic;
  9. using Object = UnityEngine.Object;
  10. public enum Folder
  11. {
  12. UI,
  13. Sprite,
  14. Object,
  15. }
  16. public enum ObjectPoolType
  17. {
  18. Null,
  19. HUD,
  20. }
  21. public class ManagerResource : MonoBehaviour
  22. {
  23. #region 变量
  24. #region 引用变量
  25. #region UI
  26. [NonSerialized] public Transform CanvasTra;
  27. //Log
  28. [NonSerialized] public Text LogLabel;
  29. [NonSerialized] public Image LogBk;
  30. [NonSerialized] public Image LogHandleBk;
  31. [NonSerialized] public ScrollRect LogScrollRect;
  32. //MainPanel
  33. [NonSerialized] public Text GoldTitle1;
  34. [NonSerialized] public Text GoldLabel1;
  35. [NonSerialized] public Text SettingLabel;
  36. [NonSerialized] public Text DiamondTitle1;
  37. [NonSerialized] public Text DiamondLabel1;
  38. [NonSerialized] public Text AchievementLabel;
  39. [NonSerialized] public Text OpenManageLabel;
  40. [NonSerialized] public Button SettingBtn;
  41. [NonSerialized] public Button AchievementBtn;
  42. [NonSerialized] public Button OpenManageBtn;
  43. [NonSerialized] public GameObject MainPanel;
  44. //GamePanel
  45. [NonSerialized] public Text ScoreTitle;
  46. [NonSerialized] public Text ScoreLabel;
  47. [NonSerialized] public Text RippingLabel;
  48. [NonSerialized] public Text WateringLabel;
  49. [NonSerialized] public Text FertilizingLabel;
  50. [NonSerialized] public Text GameBeginLabel;
  51. [NonSerialized] public Text GameStatusLabel;
  52. [NonSerialized] public Text RemainTimeLabel;
  53. [NonSerialized] public Image GamePanelBk;
  54. [NonSerialized] public Button RippingBtn;
  55. [NonSerialized] public Button WateringBtn;
  56. [NonSerialized] public Button FertilizingBtn;
  57. [NonSerialized] public Button GameBeginBtn;
  58. [NonSerialized] public GameObject OperatePanel;
  59. //ManagePanel
  60. [NonSerialized] public Text ElfTitle1;
  61. [NonSerialized] public Text ElfLabel1;
  62. [NonSerialized] public Text VisitTitle;
  63. [NonSerialized] public Text VisitLabel;
  64. [NonSerialized] public Text GoldTitle2;
  65. [NonSerialized] public Text GoldLabel2;
  66. [NonSerialized] public Text FlowerTitle1;
  67. [NonSerialized] public Text FlowerLabel1;
  68. [NonSerialized] public Text DiamondTitle2;
  69. [NonSerialized] public Text DiamondLabel2;
  70. [NonSerialized] public Image BarBk1;
  71. [NonSerialized] public Image BarBk2;
  72. [NonSerialized] public Image BarBk3;
  73. [NonSerialized] public Image ManagePanelBk;
  74. [NonSerialized] public GameObject ManagePanel;
  75. //ManagePanel
  76. [NonSerialized] public Text ElfLabel2;
  77. [NonSerialized] public Text ShopLabel;
  78. [NonSerialized] public Text InputLabel;
  79. [NonSerialized] public Text MagicLabel;
  80. [NonSerialized] public Text GardenLabel;
  81. [NonSerialized] public Text FlowerLabel2;
  82. [NonSerialized] public Text PlaceHolderLabel;
  83. [NonSerialized] public Text CloseManageLabel;
  84. [NonSerialized] public Image CommandBk;
  85. [NonSerialized] public Button ElfBtn2;
  86. [NonSerialized] public Button ShopBtn;
  87. [NonSerialized] public Button MagicBtn;
  88. [NonSerialized] public Button GardenBtn;
  89. [NonSerialized] public Button FlowerBtn2;
  90. [NonSerialized] public Button CloseManageBtn;
  91. [NonSerialized] public Text HintLabel;
  92. [NonSerialized] public Transform HudTra;
  93. #endregion
  94. [NonSerialized] public List<Flower> FlowerList;
  95. #endregion
  96. private Dictionary<ObjectPoolType, GameObject> PrefabDictionary;
  97. private Dictionary<ObjectPoolType, List<GameObject>> ObjectPool;
  98. public static ManagerResource Ins;
  99. #endregion
  100. private void Awake()
  101. {
  102. Ins = this;
  103. FlowerList = new List<Flower>();
  104. ObjectPool = new Dictionary<ObjectPoolType, List<GameObject>>();
  105. PrefabDictionary = new Dictionary<ObjectPoolType, GameObject>();
  106. #region 构造场景
  107. Get("MainCamera", Folder.Object);
  108. CreateUGUI();
  109. Get("GardenBk", Folder.Object, transform);
  110. GameObject player = Get("Player", Folder.Object, transform);
  111. player.AddComponent<Player>();
  112. Transform flowerTra1 = Get("FlowerTra1", Folder.Object, transform).transform;
  113. flowerTra1.gameObject.AddComponent<FlowerSlot>();
  114. Transform flowerTra2 = Get("FlowerTra2", Folder.Object, transform).transform;
  115. flowerTra2.gameObject.AddComponent<FlowerSlot>();
  116. Transform flowerTra3 = Get("FlowerTra3", Folder.Object, transform).transform;
  117. flowerTra3.gameObject.AddComponent<FlowerSlot>();
  118. #endregion
  119. }
  120. public void Save(GameObject go)
  121. {
  122. ObjectPoolRoot objectPoolRoot = go.GetComponent<ObjectPoolRoot>();
  123. if (objectPoolRoot == null)
  124. {
  125. throw new Exception();
  126. }
  127. List<GameObject> tempGoList;
  128. if (ObjectPool.TryGetValue(objectPoolRoot.ObjectPoolType, out tempGoList))
  129. {
  130. go.SetActive(false);
  131. tempGoList.Add(go);
  132. }
  133. else
  134. {
  135. throw new Exception();
  136. }
  137. }
  138. public GameObject Get(string name, Folder folder, ObjectPoolType objectPoolType = ObjectPoolType.Null)
  139. {
  140. GameObject tempGo = Load<GameObject>(name, folder, objectPoolType);
  141. return Instantiate(tempGo, tempGo.transform.position, tempGo.transform.rotation);
  142. }
  143. public GameObject Get(string name, Folder folder, Vector3 position, Quaternion rotation, Transform parent = null, ObjectPoolType objectPoolType = ObjectPoolType.Null)
  144. {
  145. GameObject tempGo = Load<GameObject>(name, folder, objectPoolType);
  146. return Instantiate(tempGo, position, rotation, parent);
  147. }
  148. public GameObject Get(string name, Folder folder, Transform parent, bool worldSpace = false, ObjectPoolType objectPoolType = ObjectPoolType.Null)
  149. {
  150. GameObject tempGo = Load<GameObject>(name, folder, objectPoolType);
  151. return Instantiate(tempGo, parent, worldSpace);
  152. }
  153. public T Load<T>(string name, Folder folder, ObjectPoolType objectPoolType = ObjectPoolType.Null) where T : Object
  154. {
  155. GameObject tempGo;
  156. if (PrefabDictionary.TryGetValue(objectPoolType, out tempGo))
  157. {
  158. return (T) Convert.ChangeType(tempGo, typeof(T));
  159. }
  160. else
  161. {
  162. #region 加载Prefab
  163. T t;
  164. if (folder == Folder.UI)
  165. {
  166. t = Resources.Load<T>(@"Prefab\UI\" + name);
  167. }
  168. else if (folder == Folder.Sprite)
  169. {
  170. t = Resources.Load<T>(@"Sprite\" + name);
  171. }
  172. else if (folder == Folder.Object)
  173. {
  174. t = Resources.Load<T>(@"Prefab\Object\" + name);
  175. }
  176. else
  177. {
  178. throw new Exception();
  179. }
  180. if (objectPoolType != ObjectPoolType.Null)
  181. {
  182. ObjectPool.Add(objectPoolType, new List<GameObject>());
  183. PrefabDictionary.Add(objectPoolType, (GameObject) Convert.ChangeType(t, typeof(GameObject)));
  184. }
  185. return t;
  186. #endregion
  187. }
  188. }
  189. public Text GetHUD(Color color, Vector3 position, bool permanent = false, string message = "", float speed = 0.5f, float stayTime = 0f)
  190. {
  191. HUD hud;
  192. GameObject tempGo;
  193. List<GameObject> tempGoList;
  194. if (ObjectPool.TryGetValue(ObjectPoolType.HUD, out tempGoList)) //从对象池中获得
  195. {
  196. if (tempGoList.Count > 0)
  197. {
  198. tempGo = tempGoList[0];
  199. tempGoList.RemoveAt(0);
  200. tempGo.SetActive(true);
  201. tempGo.transform.position = position;
  202. tempGo.transform.rotation = Quaternion.identity;
  203. hud = tempGo.GetComponent<HUD>();
  204. hud.Set(color, permanent, message, speed, stayTime);
  205. return tempGo.GetComponent<Text>();
  206. }
  207. }
  208. #region Instantiate
  209. tempGo = Get("HUD", Folder.UI, position, Quaternion.identity, HudTra, ObjectPoolType.HUD);
  210. hud = tempGo.AddComponent<HUD>();
  211. hud.Set(color, permanent, message, speed, stayTime);
  212. return tempGo.GetComponent<Text>();
  213. #endregion
  214. }
  215. private void CreateUGUI()
  216. {
  217. Language.Init(SystemLanguage.Chinese);
  218. CanvasTra = Get("Canvas", Folder.UI).transform; //实例化Canvas
  219. Get("EventSystem", Folder.UI); //实例化EventSystem
  220. #region 引用变量赋值
  221. Hashtable hashtable = new Hashtable();
  222. Transform[] transforms = CanvasTra.GetComponentsInChildren<Transform>(true);
  223. for (int i = 0; i < transforms.Length; i++)
  224. {
  225. hashtable.Add(transforms[i].name, transforms[i]);
  226. }
  227. //Log
  228. LogLabel = ((Transform) hashtable["LogLabel"]).GetComponent<Text>();
  229. LogBk = ((Transform) hashtable["LogScrollRectBk"]).GetComponent<Image>();
  230. LogHandleBk = ((Transform) hashtable["LogHandleBk"]).GetComponent<Image>();
  231. LogScrollRect = ((Transform) hashtable["LogScrollRectBk"]).GetComponent<ScrollRect>();
  232. //MainPanel
  233. GoldTitle1 = ((Transform) hashtable["GoldTitle1"]).GetComponent<Text>();
  234. GoldLabel1 = ((Transform) hashtable["GoldLabel1"]).GetComponent<Text>();
  235. SettingLabel = ((Transform) hashtable["SettingLabel"]).GetComponent<Text>();
  236. DiamondTitle1 = ((Transform) hashtable["DiamondTitle1"]).GetComponent<Text>();
  237. DiamondLabel1 = ((Transform) hashtable["DiamondLabel1"]).GetComponent<Text>();
  238. AchievementLabel = ((Transform) hashtable["AchievementLabel"]).GetComponent<Text>();
  239. OpenManageLabel = ((Transform) hashtable["OpenManageLabel"]).GetComponent<Text>();
  240. SettingBtn = ((Transform) hashtable["SettingBtn"]).GetComponent<Button>();
  241. AchievementBtn = ((Transform) hashtable["AchievementBtn"]).GetComponent<Button>();
  242. OpenManageBtn = ((Transform) hashtable["OpenManageBtn"]).GetComponent<Button>();
  243. MainPanel = ((Transform) hashtable["MainPanel"]).gameObject;
  244. //GamePanel
  245. ScoreTitle = ((Transform) hashtable["ScoreTitle"]).GetComponent<Text>();
  246. ScoreLabel = ((Transform)hashtable["ScoreLabel"]).GetComponent<Text>();
  247. RippingLabel = ((Transform) hashtable["RippingLabel"]).GetComponent<Text>();
  248. WateringLabel = ((Transform) hashtable["WateringLabel"]).GetComponent<Text>();
  249. FertilizingLabel = ((Transform) hashtable["FertilizingLabel"]).GetComponent<Text>();
  250. GameBeginLabel = ((Transform) hashtable["GameBeginLabel"]).GetComponent<Text>();
  251. GameStatusLabel = ((Transform) hashtable["GameStatusLabel"]).GetComponent<Text>();
  252. RemainTimeLabel = ((Transform) hashtable["RemainTimeLabel"]).GetComponent<Text>();
  253. GamePanelBk = ((Transform) hashtable["GamePanelBk"]).GetComponent<Image>();
  254. RippingBtn = ((Transform) hashtable["RippingBtn"]).GetComponent<Button>();
  255. WateringBtn = ((Transform) hashtable["WateringBtn"]).GetComponent<Button>();
  256. FertilizingBtn = ((Transform) hashtable["FertilizingBtn"]).GetComponent<Button>();
  257. GameBeginBtn = ((Transform) hashtable["GameBeginBtn"]).GetComponent<Button>();
  258. OperatePanel = ((Transform) hashtable["OperatePanel"]).gameObject;
  259. //ManagePanel
  260. ElfTitle1 = ((Transform) hashtable["ElfTitle1"]).GetComponent<Text>();
  261. ElfLabel1 = ((Transform) hashtable["ElfLabel1"]).GetComponent<Text>();
  262. VisitTitle = ((Transform) hashtable["VisitTitle"]).GetComponent<Text>();
  263. VisitLabel = ((Transform) hashtable["VisitLabel"]).GetComponent<Text>();
  264. GoldTitle2 = ((Transform) hashtable["GoldTitle2"]).GetComponent<Text>();
  265. GoldLabel2 = ((Transform) hashtable["GoldLabel2"]).GetComponent<Text>();
  266. FlowerTitle1 = ((Transform) hashtable["FlowerTitle1"]).GetComponent<Text>();
  267. FlowerLabel1 = ((Transform) hashtable["FlowerLabel1"]).GetComponent<Text>();
  268. DiamondTitle2 = ((Transform) hashtable["DiamondTitle2"]).GetComponent<Text>();
  269. DiamondLabel2 = ((Transform) hashtable["DiamondLabel2"]).GetComponent<Text>();
  270. BarBk1 = ((Transform) hashtable["BarBk1"]).GetComponent<Image>();
  271. BarBk2 = ((Transform) hashtable["BarBk2"]).GetComponent<Image>();
  272. BarBk3 = ((Transform) hashtable["BarBk3"]).GetComponent<Image>();
  273. ManagePanelBk = ((Transform) hashtable["ManagePanelBk"]).GetComponent<Image>();
  274. //ManagePanel
  275. ElfLabel2 = ((Transform) hashtable["ElfLabel2"]).GetComponent<Text>();
  276. ShopLabel = ((Transform) hashtable["ShopLabel"]).GetComponent<Text>();
  277. InputLabel = ((Transform) hashtable["InputLabel"]).GetComponent<Text>();
  278. MagicLabel = ((Transform) hashtable["MagicLabel"]).GetComponent<Text>();
  279. GardenLabel = ((Transform) hashtable["GardenLabel"]).GetComponent<Text>();
  280. FlowerLabel2 = ((Transform) hashtable["FlowerLabel2"]).GetComponent<Text>();
  281. PlaceHolderLabel = ((Transform) hashtable["PlaceHolderLabel"]).GetComponent<Text>();
  282. CloseManageLabel = ((Transform) hashtable["CloseManageLabel"]).GetComponent<Text>();
  283. CommandBk = ((Transform) hashtable["CommandBk"]).GetComponent<Image>();
  284. ElfBtn2 = ((Transform) hashtable["ElfBtn2"]).GetComponent<Button>();
  285. ShopBtn = ((Transform) hashtable["ShopBtn"]).GetComponent<Button>();
  286. MagicBtn = ((Transform) hashtable["MagicBtn"]).GetComponent<Button>();
  287. GardenBtn = ((Transform) hashtable["GardenBtn"]).GetComponent<Button>();
  288. FlowerBtn2 = ((Transform) hashtable["FlowerBtn2"]).GetComponent<Button>();
  289. CloseManageBtn = ((Transform) hashtable["CloseManageBtn"]).GetComponent<Button>();
  290. HintLabel = ((Transform) hashtable["HintLabel"]).GetComponent<Text>();
  291. HudTra = ((Transform) hashtable["HudTra"]);
  292. #endregion
  293. #region Image赋值
  294. #endregion
  295. #region Text赋值
  296. #endregion
  297. CloseManageBtn.onClick.AddListener(OnCloseManagePanel);
  298. OpenManageBtn.onClick.AddListener(OnOpenManagePanel);
  299. }
  300. private void OnCloseManagePanel()
  301. {
  302. MainPanel.SetActive(true);
  303. GamePanelBk.gameObject.SetActive(true);
  304. ManagePanelBk.gameObject.SetActive(false);
  305. }
  306. private void OnOpenManagePanel()
  307. {
  308. MainPanel.SetActive(false);
  309. GamePanelBk.gameObject.SetActive(false);
  310. ManagePanelBk.gameObject.SetActive(true);
  311. }
  312. }