ManaReso.cs 18 KB

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