ManaReso.cs 16 KB

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