ManaReso.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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. Transform tra = Get("Chest", Folder.Scene, false, Get("GardenPage"), true, ObjType.Chest);
  451. Chest chest = tra.GetComponent<Chest>();
  452. if (chest == null)
  453. {
  454. chest = tra.AddComponent<Chest>();
  455. chest.Initialize(tra.position.y, false, false);
  456. }
  457. else
  458. {
  459. chest.Initialize(tra.position.y, false, false);
  460. }
  461. return chest;
  462. }
  463. public static Drop GetDrop(ObjType objType)
  464. {
  465. ManaAudio.PlayClip(Clip.DropClip);
  466. Vector3 leftPos = Get("MiniLeft").position;
  467. Vector3 rightPos;
  468. if (ManaMiniGame.GameA || ManaMiniGame.GameC)
  469. {
  470. rightPos = Get("MiniRight1").position;
  471. }
  472. else
  473. {
  474. rightPos = Get("MiniRight2").position;
  475. }
  476. Vector3 pos = Vector3.Lerp(leftPos, rightPos, Random.Range(0, 1f));
  477. Transform tra;
  478. if (objType == ObjType.DropGold)
  479. {
  480. tra = Get(objType.ToString(), Folder.Scene, false, null, pos, objType, typeof(DropGold));
  481. }
  482. else if(objType == ObjType.DropDiamond)
  483. {
  484. tra = Get(objType.ToString(), Folder.Scene, false, null, pos, objType, typeof(DropDiamond));
  485. }
  486. else
  487. {
  488. throw new Exception();
  489. }
  490. Drop drop = tra.GetComponent<Drop>();
  491. drop.RegistImmed();
  492. drop.ResetStatus();
  493. return drop;
  494. }
  495. public static Flower GetFlower(FlowerInfo flowerInfo, Slot slot, bool collider)
  496. {
  497. Transform tra = Get("Flower", Folder.Scene, false, slot.transform, false, ObjType.Flower, typeof(Flower));
  498. tra.localScale = new Vector3(1, 1, 1);
  499. Flower flower = tra.GetComponent<Flower>();
  500. flower.RegistImmed();
  501. flower.FlowerInfo = flowerInfo;
  502. flower.Slot = slot;
  503. //flower.FlowerIcon.SetActive(true);
  504. //flower.FlowerIcon.SetAlpha(1);
  505. flower.SetShadow();
  506. flower.SetCollider(collider);
  507. return flower;
  508. }
  509. public static Flower GetFlower(FlowerInfo flowerInfo, Transform par)
  510. {
  511. Transform tra = Get("Flower", Folder.Scene, false, par, false, ObjType.Flower, typeof(Flower));
  512. tra.localScale = new Vector3(1, 1, 1);
  513. Flower flower = tra.GetComponent<Flower>();
  514. flower.RegistImmed();
  515. flower.FlowerInfo = flowerInfo;
  516. //flower.FlowerIcon.SetActive(true);
  517. //flower.FlowerIcon.SetAlpha(1);
  518. flower.SetShadow();
  519. flower.SetCollider(false);
  520. return flower;
  521. }
  522. public static Text GetInfoItem()
  523. {
  524. Transform tra = Get("InfoItem", Folder.UI, false, Get("J_Info"), false, ObjType.InfoItem);
  525. tra.SetAsFirstSibling();
  526. Text text = tra.GetComponent<Text>();
  527. TweenRoot tween = text.CreateTweenGra(0, 1, 0.25f, true, true, Curve.EaseOutQuad);
  528. tween.OnBackwardFinish = () =>
  529. {
  530. Save(text);
  531. };
  532. return text;
  533. }
  534. 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)
  535. {
  536. Vector3 pos;
  537. if (scene)
  538. {
  539. pos = Camera.main.WorldToScreenPoint(posTra.position);
  540. }
  541. else
  542. {
  543. pos = posTra.position;
  544. }
  545. Transform tra = Get("HudText", Folder.UI, false, parTra, pos, ObjType.HudText, typeof(HudText));
  546. HudText hudText = tra.GetComponent<HudText>();
  547. hudText.Show(str, color, size, speed, time, stay);
  548. return hudText;
  549. }
  550. public static CommentItem GetCommentItem(string serialNumber, string content)
  551. {
  552. Transform tra = Get("CommentItem", Folder.UI, false, Get("Q_Grid"), false, ObjType.CommentItem, typeof(CommentItem));
  553. CommentItem commentItem = tra.GetComponent<CommentItem>();
  554. commentItem.RegistImmed();
  555. commentItem.Reset(serialNumber, content);
  556. return commentItem;
  557. }
  558. public static Transform GetElf(Flower flower, Vector4 offset, ElfType elfType)
  559. {
  560. Transform tra;
  561. Bounds bounds = flower.FlowerIcon.bounds;
  562. Vector3 pos = flower.FlowerIcon.transform.position + new Vector3(0, 0, -5f);
  563. Transform par = flower.transform;
  564. ObjType obj = elfType.ToString().Split('_')[0].ToEnum<ObjType>();
  565. pos.z -= Random.Range(0.001f, 0.1f);
  566. tra = Get(obj.ToString(), Folder.Scene, false, par, pos, obj);
  567. Elf elf = tra.GetChild(0).GetComponent<Elf>();
  568. if (elf == null)
  569. {
  570. elf = tra.GetChild(0).AddScript<Elf>();
  571. }
  572. tra.GetComponentInChildren<SpriteRenderer>().sprite = LoadSprite(elfType.ToString(), Folder.Atlas2);
  573. if (Random.Range(0f, 1f) <= 0.5f)
  574. {
  575. tra.SetEY(180);
  576. }
  577. else
  578. {
  579. tra.SetEY(0);
  580. }
  581. float offsetX = Mathf.Lerp(offset.x*bounds.extents.x, offset.y*bounds.extents.x, Random.Range(0f, 1f));
  582. float offsetY = Mathf.Lerp(offset.z, offset.w*bounds.extents.y, Random.Range(0f, 1f));
  583. tra.position += new Vector3(offsetX, offsetY, 0);
  584. elf.Flower = flower;
  585. elf.Animator.SetTrigger("Play");
  586. return tra;
  587. }
  588. public static Transform GetSkillItem(SkillRoot skillRoot)
  589. {
  590. Transform tra;
  591. if (skillRoot.SkillTab == SkillTab.Elf)
  592. {
  593. tra = Get("SkillItem", Folder.UI, false, Get("Fd_Grid"), false, ObjType.SkillItem);
  594. }
  595. else if (skillRoot.SkillTab == SkillTab.Store)
  596. {
  597. tra = Get("SkillItem", Folder.UI, false, Get("Fc_Grid"), false, ObjType.SkillItem);
  598. }
  599. else if (skillRoot.SkillTab == SkillTab.Magic)
  600. {
  601. tra = Get("SkillItem", Folder.UI, false, Get("Fb_Grid"), false, ObjType.SkillItem);
  602. }
  603. else if (skillRoot.SkillTab == SkillTab.Garden)
  604. {
  605. tra = Get("SkillItem", Folder.UI, false, Get("Fa_Grid"), false, ObjType.SkillItem);
  606. }
  607. else
  608. {
  609. throw new Exception();
  610. }
  611. skillRoot.SkillItem = tra;
  612. return tra;
  613. }
  614. public static Transform GetAchieveItem()
  615. {
  616. Transform tra = Get("AchieveItem", Folder.UI, false, ManaReso.Get("M_Grid"), false, ObjType.AchieveItem);
  617. return tra;
  618. }
  619. public static ParticleSystem GetFirework(Vector3 pos)
  620. {
  621. Transform tra = Get("Firework", Folder.Effect, false, null, pos, ObjType.Firework, typeof(Effect));
  622. ParticleSystem particle = tra.GetComponent<ParticleSystem>();
  623. particle.Play();
  624. return particle;
  625. }
  626. public static ParticleSystem GetLightwall()
  627. {
  628. Transform tra = Get("LightwallUI", Folder.Effect, false, Get("Canvas"), false, ObjType.LightwallUI, typeof(Effect));
  629. ParticleSystem particle = tra.GetComponent<ParticleSystem>();
  630. if(particle != null)
  631. particle.Play();
  632. return particle;
  633. }
  634. public static void AsyncLoad<T>(string goName, Folder folder, UnityAction callback = null)
  635. {
  636. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  637. AssetBundleRequest bundleRequest = Bundle.LoadAsync<T>(goName, folder);
  638. AsyncRequest asyncRequest = new AsyncRequest();
  639. asyncRequest.Request = bundleRequest;
  640. asyncRequest.Callback = callback;
  641. kv.Key = asyncRequest;
  642. kv.Value = () =>
  643. {
  644. ObjDic.UniqueAdd(goName, bundleRequest.asset);
  645. };
  646. RequestList.Add(bundleRequest);
  647. InstantiateList.Add(kv);
  648. }
  649. public static void AsyncLoad(string goName, int amt, Folder folder, ObjType objType, bool ui = false, bool canvas = false, UnityAction callback = null)
  650. {
  651. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  652. AssetBundleRequest bundleRequest = Bundle.LoadAsync<object>(goName, folder);
  653. AsyncRequest asyncRequest = new AsyncRequest();
  654. asyncRequest.Request = bundleRequest;
  655. asyncRequest.Callback = callback;
  656. kv.Key = asyncRequest;
  657. kv.Value = () =>
  658. {
  659. ObjDic.UniqueAdd(goName, bundleRequest.asset);
  660. if (objType != ObjType.Null)
  661. {
  662. ObjectPool.UniqueAdd(objType, new List<Transform>());
  663. }
  664. GameObject go;
  665. if (ui)
  666. {
  667. go = (GameObject)Instantiate(bundleRequest.asset, GameObject.Find("ManagerGame").transform.GetChild(0).GetChild(0));
  668. }
  669. else
  670. {
  671. go = (GameObject)Instantiate(bundleRequest.asset);
  672. }
  673. ObjPoolDic.Add(go, objType);
  674. go.name = go.name.Replace("(Clone)", "");
  675. Save(go);
  676. if (ui)
  677. {
  678. go.SetParent(GameObject.Find("ManagerGame").transform.GetChild(0).GetChild(0));
  679. }
  680. };
  681. RequestList.Add(bundleRequest);
  682. for (int i = 0; i < amt; i++)
  683. {
  684. InstantiateList.Add(kv);
  685. }
  686. }
  687. public static void AddAsyncLoad<T>(string goName, Folder folder, UnityAction callback = null)
  688. {
  689. AsyncList.Add
  690. (
  691. () =>
  692. {
  693. AsyncLoad<T>(goName, folder, callback);
  694. }
  695. );
  696. }
  697. public static void AddAsyncLoad(string goName, int amt, Folder folder, ObjType objType, bool ui = false, bool canvas = false, UnityAction callback = null)
  698. {
  699. AsyncList.Add
  700. (
  701. () =>
  702. {
  703. AsyncLoad(goName, amt, folder, objType, ui, canvas, callback);
  704. }
  705. );
  706. }
  707. public void StopAsync()
  708. {
  709. StopCoroutine(CoroLoad);
  710. StopCoroutine(CoroInstantiate);
  711. }
  712. public static IEnumerator IAsyncLoad()
  713. {
  714. while (true)
  715. {
  716. if (RequestList.Count > 0)
  717. {
  718. yield return null;
  719. }
  720. else
  721. {
  722. if (AsyncList.Valid() && !AsyncLoadLock)
  723. {
  724. AsyncLoadLock = true;
  725. Auxiliary.Instance.DelayCall
  726. (
  727. () =>
  728. {
  729. AsyncLoadLock = false;
  730. AsyncList[0].SafeInvoke();
  731. AsyncList.RemoveAt(0);
  732. },
  733. 1
  734. );
  735. yield return null;
  736. }
  737. else
  738. {
  739. yield return null;
  740. }
  741. }
  742. }
  743. }
  744. public static IEnumerator IAsyncInstancitate()
  745. {
  746. while (true)
  747. {
  748. if (AsyncInstantiateLock)
  749. {
  750. yield return null;
  751. }
  752. if (!InstantiateList.Valid())
  753. {
  754. yield return null;
  755. }
  756. else
  757. {
  758. if (Time.deltaTime > 0.333f)
  759. {
  760. yield return null;
  761. }
  762. if (!InstantiateList[0].Key.Request.isDone)
  763. {
  764. yield return null;
  765. }
  766. else
  767. {
  768. InstantiateList[0].Key.Callback.SafeInvoke();
  769. RequestList.Remove(InstantiateList[0].Key.Request);
  770. UnityAction action = InstantiateList[0].Value;
  771. InstantiateList.RemoveAt(0);
  772. AsyncInstantiateLock = true;
  773. Auxiliary.Instance.DelayCall
  774. (
  775. () =>
  776. {
  777. if (!Logo.Complete)
  778. {
  779. action.SafeInvoke();
  780. AsyncInstantiateLock = false;
  781. }
  782. },
  783. 1
  784. );
  785. yield return null;
  786. }
  787. }
  788. }
  789. }
  790. }