ManaReso.cs 15 KB

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