ManaReso.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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("LeftTraMini").position, Get("RightTraMini").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("LeftTraMini").position, Get("RightTraMini").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.ObjType = ObjType.Flower;
  192. flower.LocalPos = flower.transform.localPosition;
  193. }
  194. else
  195. {
  196. flower.GoldBk.SetActive(false);
  197. flower.transform.localPosition = flower.LocalPos;
  198. }
  199. flower.FlowerInfo = flowerInfo;
  200. flower.Slot = slot;
  201. flower.SetCollider(collider);
  202. return flower;
  203. }
  204. public static Flower GetFlower(FlowerInfo flowerInfo, Transform par)
  205. {
  206. Transform tra = Get("Flower", Folder.Garden, false, par, false, ObjType.Flower);
  207. Flower flower = tra.GetComponent<Flower>();
  208. if (flower == null)
  209. {
  210. flower = tra.AddComponent<Flower>();
  211. flower.ObjType = ObjType.Flower;
  212. flower.LocalPos = flower.transform.localPosition;
  213. }
  214. else
  215. {
  216. flower.GoldBk.SetActive(false);
  217. flower.transform.localPosition = flower.LocalPos;
  218. }
  219. flower.FlowerInfo = flowerInfo;
  220. flower.SetCollider(false);
  221. return flower;
  222. }
  223. public static HudText GetHudText(string str, Color color, int size, Transform posTra, Transform parTra, bool scene)
  224. {
  225. Vector3 pos;
  226. if (scene)
  227. {
  228. pos = Camera.main.WorldToScreenPoint(posTra.position);
  229. }
  230. else
  231. {
  232. pos = posTra.position;
  233. }
  234. Transform tra = Get("HudText", Folder.UI, false, parTra, pos, ObjType.HudText);
  235. HudText hudText = tra.GetComponent<HudText>();
  236. if (hudText == null)
  237. {
  238. hudText = tra.AddComponent<HudText>();
  239. hudText.ObjType = ObjType.HudText;
  240. }
  241. hudText.Show(str, color, size);
  242. return hudText;
  243. }
  244. public static Transform GetAnim(Bounds bounds, ObjType obj, Vector3 pos, Transform parTra)
  245. {
  246. Transform tra;
  247. tra = Get(obj.ToString(), Folder.Character, false, parTra, pos, obj);
  248. ObjRoot objRoot = tra.GetComponent<ObjRoot>();
  249. if (objRoot == null)
  250. {
  251. tra.AddComponent<ObjRoot>().ObjType = obj;
  252. tra.GetChild(0).AddComponent<AnimationReceiver>();
  253. }
  254. if (Random.Range(0f, 1f) <= 0.5f)
  255. {
  256. tra.SetEY(180);
  257. }
  258. else
  259. {
  260. tra.SetEY(0);
  261. }
  262. float offsetX = Mathf.Lerp(-0.75f*bounds.extents.x, 0.75f*bounds.extents.x, Random.Range(0f, 1f));
  263. float offsetY = Mathf.Lerp(0, 0.75f*bounds.extents.y, Random.Range(0f, 1f));
  264. tra.position += new Vector3(offsetX, offsetY, 0);
  265. return tra;
  266. }
  267. public static Transform GetSkillItem(SkillRoot skillRoot)
  268. {
  269. Transform tra;
  270. if (skillRoot.SkillTab == SkillTab.Elf)
  271. {
  272. tra = Get("SkillItem", Folder.UI, false, Get("Fd_Grid"), false);
  273. }
  274. else if (skillRoot.SkillTab == SkillTab.Store)
  275. {
  276. tra = Get("SkillItem", Folder.UI, false, Get("Fc_Grid"), false);
  277. }
  278. else if (skillRoot.SkillTab == SkillTab.Magic)
  279. {
  280. tra = Get("SkillItem", Folder.UI, false, Get("Fb_Grid"), false);
  281. }
  282. else if (skillRoot.SkillTab == SkillTab.Garden)
  283. {
  284. tra = Get("SkillItem", Folder.UI, false, Get("Fa_Grid"), false);
  285. }
  286. else
  287. {
  288. throw new Exception();
  289. }
  290. skillRoot.SkillItem = tra;
  291. return tra;
  292. }
  293. public static Transform GetFlowerItemH()
  294. {
  295. Transform tra = Get("FlowerItemH", Folder.UI, false, Get("H_Grid"), false, ObjType.FlowerItem);
  296. ObjRoot objRoot = tra.GetComponent<ObjRoot>();
  297. if (objRoot == null)
  298. {
  299. objRoot = tra.AddComponent<ObjRoot>();
  300. }
  301. objRoot.ObjType = ObjType.FlowerItem;
  302. return objRoot.transform;
  303. }
  304. #region TraDic
  305. public static T Get<T>(string goName)
  306. {
  307. Transform tra;
  308. if (TraDic.TryGetValue(goName, out tra))
  309. {
  310. T t = tra.GetComponent<T>();
  311. if (t == null)
  312. {
  313. throw new Exception();
  314. }
  315. return t;
  316. }
  317. else
  318. {
  319. throw new Exception(goName);
  320. }
  321. }
  322. public static Transform Get(string goName)
  323. {
  324. Transform tra;
  325. if (TraDic.TryGetValue(goName, out tra))
  326. {
  327. return tra;
  328. }
  329. else
  330. {
  331. throw new Exception(goName);
  332. }
  333. }
  334. #endregion
  335. #region ObjPool
  336. public static void Save<T>(T t, bool warn = true) where T : Component
  337. {
  338. Transform tra = t.transform;
  339. ObjRoot objRoot = tra.GetComponent<ObjRoot>();
  340. if (objRoot == null)
  341. {
  342. throw new Exception();
  343. }
  344. List<Transform> traList;
  345. if (ObjectPool.TryGetValue(objRoot.ObjType, out traList))
  346. {
  347. if (traList.Contains(tra))
  348. {
  349. if (warn)
  350. {
  351. throw new Exception();
  352. }
  353. else
  354. {
  355. return;
  356. }
  357. }
  358. tra.SetActive(false);
  359. traList.Add(tra);
  360. tra.transform.SetParent(Get("ObjPool"));
  361. }
  362. else
  363. {
  364. throw new Exception();
  365. }
  366. }
  367. public static void Save(GameObject go, bool warn = true)
  368. {
  369. ObjRoot objRoot = go.GetComponent<ObjRoot>();
  370. if (objRoot == null)
  371. {
  372. throw new Exception();
  373. }
  374. List<Transform> traList;
  375. if (ObjectPool.TryGetValue(objRoot.ObjType, out traList))
  376. {
  377. if (traList.Contains(go.transform))
  378. {
  379. if (warn)
  380. {
  381. throw new Exception();
  382. }
  383. else
  384. {
  385. return;
  386. }
  387. }
  388. go.SetActive(false);
  389. traList.Add(go.transform);
  390. go.transform.SetParent(Get("ObjPool"));
  391. }
  392. else
  393. {
  394. throw new Exception();
  395. }
  396. }
  397. public static void SaveUI<T>(T t, bool warn = true) where T : Component
  398. {
  399. Transform tra = t.transform;
  400. ObjRoot objRoot = tra.GetComponent<ObjRoot>();
  401. if (objRoot == null)
  402. {
  403. throw new Exception();
  404. }
  405. List<Transform> traList;
  406. if (ObjectPool.TryGetValue(objRoot.ObjType, out traList))
  407. {
  408. if (traList.Contains(tra))
  409. {
  410. if (warn)
  411. {
  412. throw new Exception();
  413. }
  414. else
  415. {
  416. return;
  417. }
  418. }
  419. tra.SetActive(false);
  420. traList.Add(tra);
  421. tra.transform.SetParent(Get("J_ObjPool"));
  422. }
  423. else
  424. {
  425. throw new Exception();
  426. }
  427. }
  428. public static void SaveUI(GameObject go, bool warn = true)
  429. {
  430. ObjRoot objRoot = go.GetComponent<ObjRoot>();
  431. if (objRoot == null)
  432. {
  433. throw new Exception();
  434. }
  435. List<Transform> traList;
  436. if (ObjectPool.TryGetValue(objRoot.ObjType, out traList))
  437. {
  438. if (traList.Contains(go.transform))
  439. {
  440. if (warn)
  441. {
  442. throw new Exception();
  443. }
  444. else
  445. {
  446. return;
  447. }
  448. }
  449. go.SetActive(false);
  450. traList.Add(go.transform);
  451. go.transform.SetParent(Get("J_ObjPool"));
  452. }
  453. else
  454. {
  455. throw new Exception();
  456. }
  457. }
  458. public static Transform Get(ObjType objType)
  459. {
  460. List<Transform> traList;
  461. if (ObjectPool.TryGetValue(objType, out traList))
  462. {
  463. if (traList.Count > 0)
  464. {
  465. Transform tra = traList[0];
  466. tra.SetActive(true);
  467. traList.RemoveAt(0);
  468. return tra.transform;
  469. }
  470. }
  471. return null;
  472. }
  473. #endregion
  474. #region ShortCut
  475. public static void SetText(string goName)
  476. {
  477. Text text = Get<Text>(goName);
  478. text.text = Language.GetStr("UI", goName);
  479. }
  480. public static void SetText(string goName, string str)
  481. {
  482. Text text = Get<Text>(goName);
  483. text.text = str;
  484. }
  485. public static void SetSprite(string goName, Sprite sprite)
  486. {
  487. Image image = Get<Image>(goName);
  488. image.sprite = sprite;
  489. }
  490. public static void SetActive(string goName, bool active)
  491. {
  492. Get(goName).SetActive(active);
  493. }
  494. public static void SetButtonEvent(string goName, params UnityAction[] onClicks)
  495. {
  496. Button button = Get<Button>(goName);
  497. button.onClick = new Button.ButtonClickedEvent();
  498. for (int i = 0; i < onClicks.Length; i++)
  499. {
  500. button.onClick.AddListener(onClicks[i]);
  501. }
  502. }
  503. public static void AddButtonEvent(string goName, params UnityAction[] onClicks)
  504. {
  505. Button button = Get<Button>(goName);
  506. for (int i = 0; i < onClicks.Length; i++)
  507. {
  508. button.onClick.AddListener(onClicks[i]);
  509. }
  510. }
  511. #endregion
  512. }