ManaReso.cs 25 KB

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