ManaReso.cs 25 KB

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