ManaReso.cs 25 KB

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