ManaReso.cs 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. Atlas,
  16. Atlas2,
  17. Effect,
  18. Audio,
  19. Scene,
  20. Config,
  21. Discard,
  22. }
  23. public enum ObjType
  24. {
  25. Null,
  26. Firework,
  27. LightwallUI,
  28. Player,
  29. Page,
  30. Flower,
  31. Garden,
  32. Tutorial,
  33. CloseItem,
  34. DressRoom,
  35. Canvas,
  36. GroupA,
  37. GroupB,
  38. GroupC,
  39. GroupD,
  40. GroupE,
  41. EventSystem,
  42. MainCamera,
  43. NickName,
  44. MessageBox,
  45. HudText,
  46. MailItem,
  47. InfoItem,
  48. SkillItem,
  49. SignItem,
  50. RankItem,
  51. FlowerItem,
  52. AchieveItem,
  53. CommentItem,
  54. Music,
  55. Star,
  56. Chest,
  57. DropGold,
  58. DropDiamond,
  59. Bee,
  60. Beetle,
  61. Butterfly,
  62. Dragonfly,
  63. }
  64. public class AsyncRequest
  65. {
  66. public UnityAction Callback;
  67. public AssetBundleRequest Request;
  68. }
  69. public class ManaReso : Regist
  70. {
  71. #region 变量
  72. public static bool AsyncLoadLock;
  73. public static bool AsyncInstantiateLock;
  74. public static Coroutine CoroLoad;
  75. public static Coroutine CoroInstantiate;
  76. public static ManaReso Instance;
  77. public static List<UnityAction> AsyncList = new List<UnityAction>();
  78. public static List<AssetBundleRequest> RequestList = new List<AssetBundleRequest>();
  79. public static List<KV<AsyncRequest, UnityAction>> InstantiateList = new List<KV<AsyncRequest, UnityAction>>();
  80. public static Dictionary<string, Object> ObjDic = new Dictionary<string, Object>();
  81. public static Dictionary<string, Transform> TraDic = new Dictionary<string, Transform>();
  82. public static Dictionary<GameObject, ObjType> ObjPoolDic = new Dictionary<GameObject, ObjType>();
  83. public static Dictionary<ObjType, List<Transform>> ObjectPool = new Dictionary<ObjType, List<Transform>>();
  84. #endregion
  85. public override bool RegistImmed()
  86. {
  87. if (base.RegistImmed())
  88. {
  89. return true;
  90. }
  91. Instance = this;
  92. Transform objPool = new GameObject("ObjPool").transform;
  93. objPool.parent = transform;
  94. objPool.SetActive(false);
  95. TraDic.Add(objPool.name, objPool);
  96. CoroLoad = StartCoroutine(IAsyncLoad());
  97. CoroInstantiate = StartCoroutine(IAsyncInstancitate());
  98. return false;
  99. }
  100. #region TraDic
  101. public static T Get<T>(string goName, bool warn = true)
  102. {
  103. Transform tra;
  104. if (TraDic.TryGetValue(goName, out tra))
  105. {
  106. T t = tra.GetComponent<T>();
  107. if (t == null)
  108. {
  109. throw new Exception(goName);
  110. }
  111. return t;
  112. }
  113. else
  114. {
  115. if (warn)
  116. {
  117. throw new Exception(goName);
  118. }
  119. else
  120. {
  121. return default(T);
  122. }
  123. }
  124. }
  125. public static T[] Gets<T>(string goName, bool warn = true)
  126. {
  127. Transform tra;
  128. if (TraDic.TryGetValue(goName, out tra))
  129. {
  130. T[] t = tra.GetComponentsInChildren<T>();
  131. if (t == null)
  132. {
  133. throw new Exception(goName);
  134. }
  135. return t;
  136. }
  137. else
  138. {
  139. if (warn)
  140. {
  141. throw new Exception(goName);
  142. }
  143. else
  144. {
  145. return default(T[]);
  146. }
  147. }
  148. }
  149. public static Transform Get(string goName, bool warn = true)
  150. {
  151. Transform tra;
  152. if (TraDic.TryGetValue(goName, out tra))
  153. {
  154. return tra;
  155. }
  156. else
  157. {
  158. if (warn)
  159. {
  160. throw new Exception(goName);
  161. }
  162. else
  163. {
  164. return null;
  165. }
  166. }
  167. }
  168. #endregion
  169. #region ObjPool
  170. public static void Release()
  171. {
  172. //Bundle.Discard.Unload(false);
  173. }
  174. public static void Save<T>(T t, bool warn = false) where T : Component
  175. {
  176. Save(t.gameObject);
  177. }
  178. public static void Save(GameObject go, bool warn = false)
  179. {
  180. ObjType objType;
  181. if (!ObjPoolDic.TryGetValue(go, out objType))
  182. {
  183. throw new Exception();
  184. }
  185. List<Transform> traList;
  186. if (ObjectPool.TryGetValue(objType, out traList))
  187. {
  188. if (traList.Contains(go.transform))
  189. {
  190. if (warn)
  191. {
  192. return;
  193. }
  194. else
  195. {
  196. throw new Exception();
  197. }
  198. }
  199. go.SetActive(false);
  200. traList.Add(go.transform);
  201. go.transform.SetParent(Get("ObjPool"));
  202. }
  203. else
  204. {
  205. throw new Exception();
  206. }
  207. }
  208. public static bool Contains<T>(T t) where T : Component
  209. {
  210. return Contains(t.gameObject);
  211. }
  212. public static bool Contains(GameObject go)
  213. {
  214. foreach (var kv in ObjectPool)
  215. {
  216. if (kv.Value.Contains(go.transform))
  217. {
  218. return true;
  219. }
  220. }
  221. return false;
  222. }
  223. public static Transform Get(ObjType objType)
  224. {
  225. List<Transform> traList;
  226. if (ObjectPool.TryGetValue(objType, out traList))
  227. {
  228. if (traList.Count > 0)
  229. {
  230. Transform tra = traList[0];
  231. tra.SetActive(true);
  232. traList.RemoveAt(0);
  233. return tra.transform;
  234. }
  235. }
  236. return null;
  237. }
  238. #endregion
  239. #region ShortCut
  240. public static Text SetText(string goName)
  241. {
  242. Text text = Get<Text>(goName);
  243. text.text = Language.GetStr("UI", goName);
  244. return text;
  245. }
  246. public static Text SetText(string goName, string str)
  247. {
  248. Text text = Get<Text>(goName);
  249. text.text = str;
  250. return text;
  251. }
  252. public static Image SetSprite(string goName, Sprite sprite)
  253. {
  254. Image image = Get<Image>(goName);
  255. image.sprite = sprite;
  256. return image;
  257. }
  258. public static Transform SetActive(string goName, bool active)
  259. {
  260. Transform tra = Get(goName);
  261. tra.SetActive(active);
  262. return tra;
  263. }
  264. public static Button SetButtonEvent(string goName, UnityAction onClick)
  265. {
  266. Button button = Get<Button>(goName);
  267. button.onClick = new Button.ButtonClickedEvent();
  268. button.onClick.AddListener(onClick);
  269. return button;
  270. }
  271. public static Button AddButtonEvent(string goName, UnityAction onClick)
  272. {
  273. Button button = Get<Button>(goName);
  274. button.onClick.AddListener(onClick);
  275. return button;
  276. }
  277. public static Button PushButtonEvent(string goName, UnityAction onClick)
  278. {
  279. Button button = Get<Button>(goName);
  280. Button.ButtonClickedEvent click = button.onClick;
  281. button.onClick = new Button.ButtonClickedEvent();
  282. button.onClick.AddListener(onClick);
  283. button.onClick.AddListener(click.Invoke);
  284. return button;
  285. }
  286. public static Button AddButtonEventOnetime(string goName, UnityAction onClick)
  287. {
  288. Button button = Get<Button>(goName);
  289. onClick += () =>
  290. {
  291. button.onClick.RemoveListener(onClick);
  292. };
  293. button.onClick.AddListener(onClick);
  294. return button;
  295. }
  296. public static Button PushButtonEventOnetime(string goName, UnityAction onClick)
  297. {
  298. Button button = Get<Button>(goName);
  299. onClick += () =>
  300. {
  301. button.onClick.RemoveListener(onClick);
  302. };
  303. Button.ButtonClickedEvent click = button.onClick;
  304. button.onClick = new Button.ButtonClickedEvent();
  305. button.onClick.AddListener(onClick);
  306. button.onClick.AddListener(click.Invoke);
  307. return button;
  308. }
  309. #endregion
  310. public static T Load<T>(string goName, Folder folder, ObjType objType = ObjType.Null) where T : Object
  311. {
  312. Object obj;
  313. if (ObjDic.TryGetValue(goName, out obj))
  314. {
  315. if (objType != ObjType.Null)
  316. {
  317. ObjectPool.UniqueAdd(objType, new List<Transform>());
  318. }
  319. return (T) obj;
  320. }
  321. else
  322. {
  323. T t = Bundle.Load<T>(goName, folder);
  324. if (t == null)
  325. {
  326. throw new Exception(goName + " " + folder);
  327. }
  328. ObjDic.Add(goName, t);
  329. if (objType != ObjType.Null)
  330. {
  331. ObjectPool.UniqueAdd(objType, new List<Transform>());
  332. }
  333. return t;
  334. }
  335. }
  336. public static Sprite LoadSprite(string goName, Folder folder)
  337. {
  338. return Load<Sprite>(goName, folder);
  339. }
  340. public static Transform Get(string goName, Folder folder, bool compile, Transform par, bool worldSpace, ObjType objType = ObjType.Null, Type type = null)
  341. {
  342. Transform tra = Get(objType);
  343. if (tra == null)
  344. {
  345. GameObject go = Load<GameObject>(goName, folder, objType);
  346. go = Instantiate(go, par, worldSpace);
  347. go.name = go.name.Replace("(Clone)", "");
  348. if (compile)
  349. {
  350. Auxiliary.CompileDic(go.transform, TraDic);
  351. }
  352. if (objType != ObjType.Null)
  353. {
  354. ObjPoolDic.Add(go, objType);
  355. }
  356. if (type != null)
  357. {
  358. go.AddComponent(type);
  359. }
  360. return go.transform;
  361. }
  362. else
  363. {
  364. if (compile)
  365. {
  366. Auxiliary.CompileDic(tra, TraDic);
  367. }
  368. GameObject prefab = Load<GameObject>(goName, folder, objType);
  369. tra.SetParent(par);
  370. if (worldSpace)
  371. {
  372. tra.position = prefab.transform.position;
  373. }
  374. else
  375. {
  376. tra.localPosition = prefab.transform.position;
  377. }
  378. if (type != null)
  379. {
  380. if (tra.GetComponent(type) == null)
  381. {
  382. tra.AddComponent(type);
  383. }
  384. }
  385. return tra;
  386. }
  387. }
  388. public static Transform Get(string goName, Folder folder, bool compile, Transform par, Vector3 pos, ObjType objType = ObjType.Null, Type type = null)
  389. {
  390. Transform tra = Get(objType);
  391. if (tra == null)
  392. {
  393. GameObject go = Load<GameObject>(goName, folder, objType);
  394. go = Instantiate(go, pos, Quaternion.identity, par);
  395. go.name = go.name.Replace("(Clone)", "");
  396. if (compile)
  397. {
  398. Auxiliary.CompileDic(go.transform, TraDic);
  399. }
  400. if (objType != ObjType.Null)
  401. {
  402. ObjPoolDic.Add(go, objType);
  403. }
  404. if (type != null)
  405. {
  406. go.AddComponent(type);
  407. }
  408. return go.transform;
  409. }
  410. else
  411. {
  412. if (compile)
  413. {
  414. Auxiliary.CompileDic(tra, TraDic);
  415. }
  416. tra.SetParent(par);
  417. tra.position = pos;
  418. if (type != null)
  419. {
  420. if (tra.GetComponent(type) == null)
  421. {
  422. tra.AddComponent(type);
  423. }
  424. }
  425. return tra;
  426. }
  427. }
  428. public static Star GetStar()
  429. {
  430. int slotIndex = Random.Range(0, 9);
  431. bool forceLeft = slotIndex == 3 || slotIndex == 8;
  432. bool forceRight = slotIndex == 0 || slotIndex == 4;
  433. slotIndex += Garden.CurPage*9;
  434. Slot slot = ManaGarden.SlotList[slotIndex];
  435. Vector3 pos = slot.transform.position;
  436. pos.z = -0.35f + Random.Range(-0.01f, 0.01f);
  437. Transform tra = Get("Star", Folder.Scene, false, null, pos, ObjType.Star);
  438. Star star = tra.GetComponent<Star>();
  439. tra.parent = slot.transform;
  440. if (star == null)
  441. {
  442. star = tra.AddComponent<Star>();
  443. star.Initialize(pos.y, forceLeft, forceRight);
  444. }
  445. else
  446. {
  447. star.Initialize(pos.y, forceLeft, forceRight);
  448. }
  449. return star;
  450. }
  451. public static Chest GetChest()
  452. {
  453. Transform tra = Get("Chest", Folder.Scene, false, Get("GardenPage"), true, ObjType.Chest);
  454. Chest chest = tra.GetComponent<Chest>();
  455. if (chest == null)
  456. {
  457. chest = tra.AddComponent<Chest>();
  458. chest.Initialize(tra.position.y, false, false);
  459. }
  460. else
  461. {
  462. chest.Initialize(tra.position.y, false, false);
  463. }
  464. return chest;
  465. }
  466. public static Drop GetDrop(ObjType objType)
  467. {
  468. ManaAudio.PlayClip(Clip.DropClip);
  469. Vector3 leftPos = Get("MiniLeft").position;
  470. Vector3 rightPos;
  471. if (ManaMiniGame.GameA || ManaMiniGame.GameC)
  472. {
  473. rightPos = Get("MiniRight1").position;
  474. }
  475. else
  476. {
  477. rightPos = Get("MiniRight2").position;
  478. }
  479. Vector3 pos = Vector3.Lerp(leftPos, rightPos, Random.Range(0, 1f));
  480. Transform tra;
  481. if (objType == ObjType.DropGold)
  482. {
  483. tra = Get(objType.ToString(), Folder.Scene, false, null, pos, objType, typeof(DropGold));
  484. }
  485. else if(objType == ObjType.DropDiamond)
  486. {
  487. tra = Get(objType.ToString(), Folder.Scene, false, null, pos, objType, typeof(DropDiamond));
  488. }
  489. else
  490. {
  491. throw new Exception();
  492. }
  493. Drop drop = tra.GetComponent<Drop>();
  494. drop.RegistImmed();
  495. drop.ResetStatus();
  496. return drop;
  497. }
  498. public static Flower GetFlower(FlowerInfo flowerInfo, Slot slot, bool collider)
  499. {
  500. Transform tra = Get("Flower", Folder.Scene, false, slot.transform, false, ObjType.Flower, typeof(Flower));
  501. tra.localScale = new Vector3(1, 1, 1);
  502. Flower flower = tra.GetComponent<Flower>();
  503. flower.RegistImmed();
  504. flower.FlowerInfo = flowerInfo;
  505. flower.Slot = slot;
  506. //flower.FlowerIcon.SetActive(true);
  507. //flower.FlowerIcon.SetAlpha(1);
  508. flower.SetShadow();
  509. flower.SetCollider(collider);
  510. return flower;
  511. }
  512. public static Flower GetFlower(FlowerInfo flowerInfo, Transform par)
  513. {
  514. Transform tra = Get("Flower", Folder.Scene, false, par, false, ObjType.Flower, typeof(Flower));
  515. tra.localScale = new Vector3(1, 1, 1);
  516. Flower flower = tra.GetComponent<Flower>();
  517. flower.RegistImmed();
  518. flower.FlowerInfo = flowerInfo;
  519. //flower.FlowerIcon.SetActive(true);
  520. //flower.FlowerIcon.SetAlpha(1);
  521. flower.SetShadow();
  522. flower.SetCollider(false);
  523. return flower;
  524. }
  525. public static Text GetInfoItem()
  526. {
  527. Transform tra = Get("InfoItem", Folder.UI, false, Get("J_Info"), false, ObjType.InfoItem);
  528. tra.SetAsFirstSibling();
  529. Text text = tra.GetComponent<Text>();
  530. TweenRoot tween = text.CreateTweenGra(0, 1, 0.25f, true, true, Curve.EaseOutQuad);
  531. tween.OnBackwardFinish = () =>
  532. {
  533. Save(text);
  534. };
  535. return text;
  536. }
  537. public static HudText GetHudText(string str, Color color, int size, Transform posTra, Transform parTra, bool scene, float speed = 7.5f, float time = 0.5f, float stay = 0.5f)
  538. {
  539. Vector3 pos;
  540. if (scene)
  541. {
  542. pos = Camera.main.WorldToScreenPoint(posTra.position);
  543. }
  544. else
  545. {
  546. pos = posTra.position;
  547. }
  548. Transform tra = Get("HudText", Folder.UI, false, parTra, pos, ObjType.HudText, typeof(HudText));
  549. HudText hudText = tra.GetComponent<HudText>();
  550. hudText.Show(str, color, size, speed, time, stay);
  551. return hudText;
  552. }
  553. public static Ranktem GetRanktem(string rank, string praise, string serialNumber)
  554. {
  555. Transform tra = Get("RankItem", Folder.UI, false, Get("S_Grid"), false, ObjType.RankItem, typeof(Ranktem));
  556. Ranktem ranktem = tra.GetComponent<Ranktem>();
  557. ranktem.RegistImmed();
  558. ranktem.Reset(rank, praise, serialNumber);
  559. return ranktem;
  560. }
  561. public static CommentItem GetCommentItem(string serialNumber, string content)
  562. {
  563. Transform tra = Get("CommentItem", Folder.UI, false, Get("Q_Grid"), false, ObjType.CommentItem, typeof(CommentItem));
  564. CommentItem commentItem = tra.GetComponent<CommentItem>();
  565. commentItem.RegistImmed();
  566. commentItem.Reset(serialNumber, content);
  567. return commentItem;
  568. }
  569. public static Transform GetElf(Flower flower, Vector4 offset, ElfType elfType)
  570. {
  571. Transform tra;
  572. Bounds bounds = flower.FlowerIcon.bounds;
  573. Vector3 pos = flower.FlowerIcon.transform.position + new Vector3(0, 0, -5f);
  574. Transform par = flower.transform;
  575. ObjType obj = elfType.ToString().Split('_')[0].ToEnum<ObjType>();
  576. pos.z -= Random.Range(0.001f, 0.1f);
  577. tra = Get(obj.ToString(), Folder.Scene, false, par, pos, obj);
  578. Elf elf = tra.GetChild(0).GetComponent<Elf>();
  579. if (elf == null)
  580. {
  581. elf = tra.GetChild(0).AddScript<Elf>();
  582. }
  583. tra.GetComponentInChildren<SpriteRenderer>().sprite = LoadSprite(elfType.ToString(), Folder.Atlas2);
  584. if (Random.Range(0f, 1f) <= 0.5f)
  585. {
  586. tra.SetEY(180);
  587. }
  588. else
  589. {
  590. tra.SetEY(0);
  591. }
  592. float offsetX = Mathf.Lerp(offset.x*bounds.extents.x, offset.y*bounds.extents.x, Random.Range(0f, 1f));
  593. float offsetY = Mathf.Lerp(offset.z, offset.w*bounds.extents.y, Random.Range(0f, 1f));
  594. tra.position += new Vector3(offsetX, offsetY, 0);
  595. elf.Flower = flower;
  596. elf.Animator.SetTrigger("Play");
  597. return tra;
  598. }
  599. public static Transform GetSkillItem(SkillRoot skillRoot)
  600. {
  601. Transform tra;
  602. if (skillRoot.SkillTab == SkillTab.Elf)
  603. {
  604. tra = Get("SkillItem", Folder.UI, false, Get("Fd_Grid"), false, ObjType.SkillItem);
  605. }
  606. else if (skillRoot.SkillTab == SkillTab.Store)
  607. {
  608. tra = Get("SkillItem", Folder.UI, false, Get("Fc_Grid"), false, ObjType.SkillItem);
  609. }
  610. else if (skillRoot.SkillTab == SkillTab.Magic)
  611. {
  612. tra = Get("SkillItem", Folder.UI, false, Get("Fb_Grid"), false, ObjType.SkillItem);
  613. }
  614. else if (skillRoot.SkillTab == SkillTab.Garden)
  615. {
  616. tra = Get("SkillItem", Folder.UI, false, Get("Fa_Grid"), false, ObjType.SkillItem);
  617. }
  618. else
  619. {
  620. throw new Exception();
  621. }
  622. skillRoot.SkillItem = tra;
  623. return tra;
  624. }
  625. public static Transform GetAchieveItem()
  626. {
  627. Transform tra = Get("AchieveItem", Folder.UI, false, ManaReso.Get("M_Grid"), false, ObjType.AchieveItem);
  628. return tra;
  629. }
  630. public static ParticleSystem GetFirework(Vector3 pos)
  631. {
  632. Transform tra = Get("Firework", Folder.Effect, false, null, pos, ObjType.Firework, typeof(Effect));
  633. ParticleSystem particle = tra.GetComponent<ParticleSystem>();
  634. particle.Play();
  635. return particle;
  636. }
  637. public static ParticleSystem GetLightwall()
  638. {
  639. Transform tra = Get("LightwallUI", Folder.Effect, false, Get("Canvas"), false, ObjType.LightwallUI, typeof(Effect));
  640. ParticleSystem particle = tra.GetComponent<ParticleSystem>();
  641. if(particle != null)
  642. particle.Play();
  643. return particle;
  644. }
  645. public static void AsyncLoad<T>(string goName, Folder folder, UnityAction callback = null)
  646. {
  647. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  648. AssetBundleRequest bundleRequest = Bundle.LoadAsync<T>(goName, folder);
  649. AsyncRequest asyncRequest = new AsyncRequest();
  650. asyncRequest.Request = bundleRequest;
  651. asyncRequest.Callback = callback;
  652. kv.Key = asyncRequest;
  653. kv.Value = () =>
  654. {
  655. ObjDic.UniqueAdd(goName, bundleRequest.asset);
  656. };
  657. RequestList.Add(bundleRequest);
  658. InstantiateList.Add(kv);
  659. }
  660. public static void AsyncLoad(string goName, int amt, Folder folder, ObjType objType, bool ui = false, bool canvas = false, UnityAction callback = null)
  661. {
  662. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  663. AssetBundleRequest bundleRequest = Bundle.LoadAsync<object>(goName, folder);
  664. AsyncRequest asyncRequest = new AsyncRequest();
  665. asyncRequest.Request = bundleRequest;
  666. asyncRequest.Callback = callback;
  667. kv.Key = asyncRequest;
  668. kv.Value = () =>
  669. {
  670. ObjDic.UniqueAdd(goName, bundleRequest.asset);
  671. if (objType != ObjType.Null)
  672. {
  673. ObjectPool.UniqueAdd(objType, new List<Transform>());
  674. }
  675. GameObject go;
  676. if (ui)
  677. {
  678. go = (GameObject)Instantiate(bundleRequest.asset, GameObject.Find("ManagerGame").transform.GetChild(0).GetChild(0));
  679. }
  680. else
  681. {
  682. go = (GameObject)Instantiate(bundleRequest.asset);
  683. }
  684. ObjPoolDic.Add(go, objType);
  685. go.name = go.name.Replace("(Clone)", "");
  686. Save(go);
  687. if (ui)
  688. {
  689. go.SetParent(GameObject.Find("ManagerGame").transform.GetChild(0).GetChild(0));
  690. }
  691. };
  692. RequestList.Add(bundleRequest);
  693. for (int i = 0; i < amt; i++)
  694. {
  695. InstantiateList.Add(kv);
  696. }
  697. }
  698. public static void AddAsyncLoad<T>(string goName, Folder folder, UnityAction callback = null)
  699. {
  700. AsyncList.Add
  701. (
  702. () =>
  703. {
  704. AsyncLoad<T>(goName, folder, callback);
  705. }
  706. );
  707. }
  708. public static void AddAsyncLoad(string goName, int amt, Folder folder, ObjType objType, bool ui = false, bool canvas = false, UnityAction callback = null)
  709. {
  710. AsyncList.Add
  711. (
  712. () =>
  713. {
  714. AsyncLoad(goName, amt, folder, objType, ui, canvas, callback);
  715. }
  716. );
  717. }
  718. public void StopAsync()
  719. {
  720. StopCoroutine(CoroLoad);
  721. StopCoroutine(CoroInstantiate);
  722. }
  723. public static IEnumerator IAsyncLoad()
  724. {
  725. while (true)
  726. {
  727. if (RequestList.Count > 0)
  728. {
  729. yield return null;
  730. }
  731. else
  732. {
  733. if (AsyncList.Valid() && !AsyncLoadLock)
  734. {
  735. AsyncLoadLock = true;
  736. Auxiliary.Instance.DelayCall
  737. (
  738. () =>
  739. {
  740. AsyncLoadLock = false;
  741. AsyncList[0].SafeInvoke();
  742. AsyncList.RemoveAt(0);
  743. },
  744. 1
  745. );
  746. yield return null;
  747. }
  748. else
  749. {
  750. yield return null;
  751. }
  752. }
  753. }
  754. }
  755. public static IEnumerator IAsyncInstancitate()
  756. {
  757. while (true)
  758. {
  759. if (AsyncInstantiateLock)
  760. {
  761. yield return null;
  762. }
  763. if (!InstantiateList.Valid())
  764. {
  765. yield return null;
  766. }
  767. else
  768. {
  769. if (Time.deltaTime > 0.333f)
  770. {
  771. yield return null;
  772. }
  773. if (!InstantiateList[0].Key.Request.isDone)
  774. {
  775. yield return null;
  776. }
  777. else
  778. {
  779. InstantiateList[0].Key.Callback.SafeInvoke();
  780. RequestList.Remove(InstantiateList[0].Key.Request);
  781. UnityAction action = InstantiateList[0].Value;
  782. InstantiateList.RemoveAt(0);
  783. AsyncInstantiateLock = true;
  784. Auxiliary.Instance.DelayCall
  785. (
  786. () =>
  787. {
  788. if (!Logo.Complete)
  789. {
  790. action.SafeInvoke();
  791. AsyncInstantiateLock = false;
  792. }
  793. },
  794. 1
  795. );
  796. yield return null;
  797. }
  798. }
  799. }
  800. }
  801. }