ManaReso.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Events;
  4. using System;
  5. using System.Collections;
  6. using System.Linq.Expressions;
  7. using System.ComponentModel;
  8. using System.Collections.Generic;
  9. using Object = UnityEngine.Object;
  10. using Random = UnityEngine.Random;
  11. using Component = UnityEngine.Component;
  12. public enum Folder
  13. {
  14. UI,
  15. Scene,
  16. Config,
  17. Object,
  18. Shader,
  19. }
  20. public enum ObjType
  21. {
  22. Null,
  23. Flower,
  24. HudText,
  25. FlowerItem,
  26. AchieveItem,
  27. DropGold,
  28. DropDiamond,
  29. BeeRed,
  30. BeeBlue,
  31. BeeWhite,
  32. BeePurple,
  33. BeeYellow,
  34. ButterflyRed,
  35. ButterflyBlue,
  36. ButterflyWhite,
  37. ButterflyPurple,
  38. ButterflyYellow,
  39. }
  40. public class ManaReso : Regist
  41. {
  42. #region 变量
  43. public static Dictionary<string, Object> ObjDic = new Dictionary<string, Object>();
  44. public static Dictionary<string, Transform> TraDic = new Dictionary<string, Transform>();
  45. public static Dictionary<ObjType, List<Transform>> ObjectPool = new Dictionary<ObjType, List<Transform>>();
  46. #endregion
  47. public override void Instantiate()
  48. {
  49. Transform objPool = new GameObject("ObjPool").transform;
  50. objPool.parent = transform;
  51. objPool.SetActive(false);
  52. TraDic.Add(objPool.name, objPool);
  53. }
  54. #region TraDic
  55. public static T Get<T>(string goName)
  56. {
  57. Transform tra;
  58. if (TraDic.TryGetValue(goName, out tra))
  59. {
  60. T t = tra.GetComponent<T>();
  61. if (t == null)
  62. {
  63. throw new Exception();
  64. }
  65. return t;
  66. }
  67. else
  68. {
  69. throw new Exception(goName);
  70. }
  71. }
  72. public static Transform Get(string goName)
  73. {
  74. Transform tra;
  75. if (TraDic.TryGetValue(goName, out tra))
  76. {
  77. return tra;
  78. }
  79. else
  80. {
  81. throw new Exception(goName);
  82. }
  83. }
  84. #endregion
  85. #region ObjPool
  86. public static void Save<T>(T t, bool warn = true) where T : Component
  87. {
  88. Transform tra = t.transform;
  89. ObjRoot objRoot = tra.GetComponent<ObjRoot>();
  90. if (objRoot == null)
  91. {
  92. throw new Exception();
  93. }
  94. List<Transform> traList;
  95. if (ObjectPool.TryGetValue(objRoot.ObjType, out traList))
  96. {
  97. if (traList.Contains(tra))
  98. {
  99. if (warn)
  100. {
  101. throw new Exception();
  102. }
  103. else
  104. {
  105. return;
  106. }
  107. }
  108. tra.SetActive(false);
  109. traList.Add(tra);
  110. tra.transform.SetParent(Get("ObjPool"));
  111. }
  112. else
  113. {
  114. throw new Exception();
  115. }
  116. }
  117. public static void Save(GameObject go, bool warn = true)
  118. {
  119. ObjRoot objRoot = go.GetComponent<ObjRoot>();
  120. if (objRoot == null)
  121. {
  122. throw new Exception();
  123. }
  124. List<Transform> traList;
  125. if (ObjectPool.TryGetValue(objRoot.ObjType, out traList))
  126. {
  127. if (traList.Contains(go.transform))
  128. {
  129. if (warn)
  130. {
  131. throw new Exception();
  132. }
  133. else
  134. {
  135. return;
  136. }
  137. }
  138. go.SetActive(false);
  139. traList.Add(go.transform);
  140. go.transform.SetParent(Get("ObjPool"));
  141. }
  142. else
  143. {
  144. throw new Exception();
  145. }
  146. }
  147. public static void SaveUI<T>(T t, bool warn = true) where T : Component
  148. {
  149. Transform tra = t.transform;
  150. ObjRoot objRoot = tra.GetComponent<ObjRoot>();
  151. if (objRoot == null)
  152. {
  153. throw new Exception();
  154. }
  155. List<Transform> traList;
  156. if (ObjectPool.TryGetValue(objRoot.ObjType, out traList))
  157. {
  158. if (traList.Contains(tra))
  159. {
  160. if (warn)
  161. {
  162. throw new Exception();
  163. }
  164. else
  165. {
  166. return;
  167. }
  168. }
  169. tra.SetActive(false);
  170. traList.Add(tra);
  171. tra.transform.SetParent(Get("J_ObjPool"));
  172. }
  173. else
  174. {
  175. throw new Exception();
  176. }
  177. }
  178. public static void SaveUI(GameObject go, bool warn = true)
  179. {
  180. ObjRoot objRoot = go.GetComponent<ObjRoot>();
  181. if (objRoot == null)
  182. {
  183. throw new Exception();
  184. }
  185. List<Transform> traList;
  186. if (ObjectPool.TryGetValue(objRoot.ObjType, out traList))
  187. {
  188. if (traList.Contains(go.transform))
  189. {
  190. if (warn)
  191. {
  192. throw new Exception();
  193. }
  194. else
  195. {
  196. return;
  197. }
  198. }
  199. go.SetActive(false);
  200. traList.Add(go.transform);
  201. go.transform.SetParent(Get("J_ObjPool"));
  202. }
  203. else
  204. {
  205. throw new Exception();
  206. }
  207. }
  208. public static Transform Get(ObjType objType)
  209. {
  210. List<Transform> traList;
  211. if (ObjectPool.TryGetValue(objType, out traList))
  212. {
  213. if (traList.Count > 0)
  214. {
  215. Transform tra = traList[0];
  216. tra.SetActive(true);
  217. traList.RemoveAt(0);
  218. return tra.transform;
  219. }
  220. }
  221. return null;
  222. }
  223. #endregion
  224. #region ShortCut
  225. public static void SetText(string goName)
  226. {
  227. Text text = Get<Text>(goName);
  228. text.text = Language.GetStr("UI", goName);
  229. }
  230. public static void SetText(string goName, string str)
  231. {
  232. Text text = Get<Text>(goName);
  233. text.text = str;
  234. }
  235. public static void SetSprite(string goName, Sprite sprite)
  236. {
  237. Image image = Get<Image>(goName);
  238. image.sprite = sprite;
  239. }
  240. public static void SetActive(string goName, bool active)
  241. {
  242. Get(goName).SetActive(active);
  243. }
  244. public static void SetButtonEvent(string goName, UnityAction onClick)
  245. {
  246. Button button = Get<Button>(goName);
  247. button.onClick = new Button.ButtonClickedEvent();
  248. button.onClick.AddListener(onClick);
  249. }
  250. public static void AddButtonEvent(string goName, UnityAction onClick)
  251. {
  252. Button button = Get<Button>(goName);
  253. button.onClick.AddListener(onClick);
  254. }
  255. public static void PushButtonEvent(string goName, UnityAction onClick)
  256. {
  257. Button button = Get<Button>(goName);
  258. Button.ButtonClickedEvent click = button.onClick;
  259. button.onClick = new Button.ButtonClickedEvent();
  260. button.onClick.AddListener(onClick);
  261. button.onClick.AddListener(click.Invoke);
  262. }
  263. public static void AddButtonEventOnetime(string goName, UnityAction onClick)
  264. {
  265. Button button = Get<Button>(goName);
  266. onClick += () =>
  267. {
  268. button.onClick.RemoveListener(onClick);
  269. };
  270. button.onClick.AddListener(onClick);
  271. }
  272. public static void PushButtonEventOnetime(string goName, UnityAction onClick)
  273. {
  274. Button button = Get<Button>(goName);
  275. onClick += () =>
  276. {
  277. button.onClick.RemoveListener(onClick);
  278. };
  279. Button.ButtonClickedEvent click = button.onClick;
  280. button.onClick = new Button.ButtonClickedEvent();
  281. button.onClick.AddListener(onClick);
  282. button.onClick.AddListener(click.Invoke);
  283. }
  284. #endregion
  285. public static T Load<T>(string goName, Folder folder, ObjType objType = ObjType.Null) where T : Object
  286. {
  287. Object obj;
  288. if (ObjDic.TryGetValue(goName, out obj))
  289. {
  290. return (T)obj;
  291. }
  292. else
  293. {
  294. T t;
  295. if (folder == Folder.UI)
  296. {
  297. t = Bundle.UI.LoadAsset<T>(goName);
  298. }
  299. else if (folder == Folder.Config)
  300. {
  301. t = Bundle.Config.LoadAsset<T>(goName);
  302. }
  303. else if (folder == Folder.Object)
  304. {
  305. t = Bundle.Object.LoadAsset<T>(goName);
  306. }
  307. else if (folder == Folder.Shader)
  308. {
  309. t = Bundle.Shader.LoadAsset<T>(goName);
  310. }
  311. else if (folder == Folder.Scene)
  312. {
  313. t = Bundle.Scene.LoadAsset<T>(goName);
  314. }
  315. else
  316. {
  317. throw new Exception();
  318. }
  319. if (t == null)
  320. {
  321. throw new Exception(goName);
  322. }
  323. ObjDic.Add(goName, t);
  324. if (objType != ObjType.Null)
  325. {
  326. ObjectPool.Add(objType, new List<Transform>());
  327. }
  328. return t;
  329. }
  330. }
  331. public static Transform Get(string goName, Folder folder, bool compile, Transform par, Vector3 pos, ObjType objType = ObjType.Null)
  332. {
  333. Transform tra = Get(objType);
  334. if (tra != null)
  335. {
  336. tra.SetParent(par);
  337. tra.position = pos;
  338. return tra;
  339. }
  340. else
  341. {
  342. GameObject go = Load<GameObject>(goName, folder, objType);
  343. go = Instantiate(go, pos, Quaternion.identity, par);
  344. go.name = go.name.Replace("(Clone)", "");
  345. if (compile)
  346. {
  347. Auxiliary.CompileDic(go.transform, TraDic);
  348. }
  349. return go.transform;
  350. }
  351. }
  352. public static Transform Get(string goName, Folder folder, bool compile, Transform par, bool worldSpace, ObjType objType = ObjType.Null)
  353. {
  354. Transform tra = Get(objType);
  355. if (tra == null)
  356. {
  357. GameObject go = Load<GameObject>(goName, folder, objType);
  358. go = Instantiate(go, par, worldSpace);
  359. go.name = go.name.Replace("(Clone)", "");
  360. if (compile)
  361. {
  362. Auxiliary.CompileDic(go.transform, TraDic);
  363. }
  364. return go.transform;
  365. }
  366. else
  367. {
  368. tra.SetParent(par);
  369. return tra;
  370. }
  371. }
  372. public static DropGold GetGold()
  373. {
  374. Vector3 pos = Vector3.Lerp(Get("LeftPosTraMini").position, Get("RightPosTraMini").position, Random.Range(0, 1f));
  375. pos.z = -2;
  376. Transform tra = Get("DropGold", Folder.UI, false, null, pos, ObjType.DropGold);
  377. DropGold dropGold = tra.GetComponent<DropGold>();
  378. if (dropGold == null)
  379. {
  380. dropGold = tra.AddScript<DropGold>();
  381. }
  382. dropGold.ResetStatus();
  383. return dropGold;
  384. }
  385. public static DropDiamond GetDiamond()
  386. {
  387. Vector3 pos = Vector3.Lerp(Get("LeftPosTraMini").position, Get("RightPosTraMini").position, Random.Range(0, 1f));
  388. pos.z = -2;
  389. Transform tra = Get("DropDiamond", Folder.UI, false, null, pos, ObjType.DropDiamond);
  390. DropDiamond dropDiamond = tra.GetComponent<DropDiamond>();
  391. if (dropDiamond == null)
  392. {
  393. dropDiamond = tra.AddScript<DropDiamond>();
  394. }
  395. dropDiamond.ResetStatus();
  396. return dropDiamond;
  397. }
  398. public static Flower GetFlower(FlowerInfo flowerInfo, Slot slot, bool collider)
  399. {
  400. Transform tra = Get("Flower", Folder.Scene, false, slot.transform, false, ObjType.Flower);
  401. Flower flower = tra.GetComponent<Flower>();
  402. if (flower == null)
  403. {
  404. flower = tra.AddScript<Flower>();
  405. flower.ObjType = ObjType.Flower;
  406. flower.LocalPos = flower.transform.localPosition;
  407. }
  408. else
  409. {
  410. flower.GoldBk.SetActive(false);
  411. flower.transform.localPosition = flower.LocalPos;
  412. }
  413. flower.FlowerInfo = flowerInfo;
  414. flower.Slot = slot;
  415. flower.SetCollider(collider);
  416. return flower;
  417. }
  418. public static Flower GetFlower(FlowerInfo flowerInfo, Transform par)
  419. {
  420. Transform tra = Get("Flower", Folder.Scene, false, par, false, ObjType.Flower);
  421. Flower flower = tra.GetComponent<Flower>();
  422. if (flower == null)
  423. {
  424. flower = tra.AddScript<Flower>();
  425. flower.ObjType = ObjType.Flower;
  426. flower.LocalPos = flower.transform.localPosition;
  427. }
  428. else
  429. {
  430. flower.GoldBk.SetActive(false);
  431. flower.transform.localPosition = flower.LocalPos;
  432. }
  433. flower.FlowerInfo = flowerInfo;
  434. flower.SetCollider(false);
  435. return flower;
  436. }
  437. public static Transform GetAchieveItem()
  438. {
  439. Transform tra = Get("AchieveItem", Folder.UI, false, ManaReso.Get("M_Grid"), false, ObjType.AchieveItem);
  440. ObjRoot objRoot = tra.GetComponent<ObjRoot>();
  441. if (objRoot == null)
  442. {
  443. tra.AddComponent<ObjRoot>().ObjType = ObjType.AchieveItem;
  444. }
  445. return tra;
  446. }
  447. public static Transform GetSkillItem(SkillRoot skillRoot)
  448. {
  449. Transform tra;
  450. if (skillRoot.SkillTab == SkillTab.Elf)
  451. {
  452. tra = Get("SkillItem", Folder.UI, false, Get("Fd_Grid"), false);
  453. }
  454. else if (skillRoot.SkillTab == SkillTab.Store)
  455. {
  456. tra = Get("SkillItem", Folder.UI, false, Get("Fc_Grid"), false);
  457. }
  458. else if (skillRoot.SkillTab == SkillTab.Magic)
  459. {
  460. tra = Get("SkillItem", Folder.UI, false, Get("Fb_Grid"), false);
  461. }
  462. else if (skillRoot.SkillTab == SkillTab.Garden)
  463. {
  464. tra = Get("SkillItem", Folder.UI, false, Get("Fa_Grid"), false);
  465. }
  466. else
  467. {
  468. throw new Exception();
  469. }
  470. skillRoot.SkillItem = tra;
  471. return tra;
  472. }
  473. public static Transform GetElf(Flower flower, ObjType obj)
  474. {
  475. Transform tra;
  476. Bounds bounds = flower.FlowerIcon.bounds;
  477. Vector3 pos = flower.FlowerIcon.transform.position;
  478. Transform par = flower.transform;
  479. pos.z -= Random.Range(0.001f, 0.1f);
  480. tra = Get(obj.ToString(), Folder.Scene, false, par, pos, obj);
  481. ObjRoot objRoot = tra.GetComponent<ObjRoot>();
  482. if (objRoot == null)
  483. {
  484. tra.AddComponent<ObjRoot>().ObjType = obj;
  485. tra.GetChild(0).AddScript<Elf>();
  486. }
  487. if (Random.Range(0f, 1f) <= 0.5f)
  488. {
  489. tra.SetEY(180);
  490. }
  491. else
  492. {
  493. tra.SetEY(0);
  494. }
  495. float offsetX = Mathf.Lerp(-0.75f*bounds.extents.x, 0.75f*bounds.extents.x, Random.Range(0f, 1f));
  496. float offsetY = Mathf.Lerp(0, 0.75f*bounds.extents.y, Random.Range(0f, 1f));
  497. tra.position += new Vector3(offsetX, offsetY, 0);
  498. Elf elf = tra.GetChild(0).GetComponent<Elf>();
  499. elf.Flower = flower;
  500. elf.Animator.SetTrigger("Play");
  501. return tra;
  502. }
  503. public static HudText GetHudText(string str, Color color, int size, Transform posTra, Transform parTra, bool scene)
  504. {
  505. Vector3 pos;
  506. if (scene)
  507. {
  508. pos = Camera.main.WorldToScreenPoint(posTra.position);
  509. }
  510. else
  511. {
  512. pos = posTra.position;
  513. }
  514. Transform tra = Get("HudText", Folder.UI, false, parTra, pos, ObjType.HudText);
  515. HudText hudText = tra.GetComponent<HudText>();
  516. if (hudText == null)
  517. {
  518. hudText = tra.AddComponent<HudText>();
  519. hudText.ObjType = ObjType.HudText;
  520. }
  521. hudText.Show(str, color, size);
  522. return hudText;
  523. }
  524. }