ManaReso.cs 15 KB

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