ManaReso.cs 25 KB

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