ManaReso.cs 24 KB

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