ManaReso.cs 24 KB

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