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. Effect,
  17. Audio,
  18. Scene,
  19. Config,
  20. Discard,
  21. }
  22. public enum ObjType
  23. {
  24. Null,
  25. Firework,
  26. LightwallUI,
  27. PlayerPink,
  28. PlayerBlond,
  29. PlayerBrown,
  30. Page,
  31. Flower,
  32. Garden,
  33. Tutorial,
  34. Canvas,
  35. GroupA,
  36. GroupB,
  37. GroupC,
  38. GroupD,
  39. EventSystem,
  40. MainCamera,
  41. HudText,
  42. InfoItem,
  43. SkillItem,
  44. SignItem,
  45. FlowerItem,
  46. AchieveItem,
  47. Music,
  48. Star,
  49. DropGold,
  50. DropDiamond,
  51. BeeRed,
  52. BeeBlue,
  53. BeeWhite,
  54. BeePurple,
  55. BeeYellow,
  56. ButterflyRed,
  57. ButterflyBlue,
  58. ButterflyWhite,
  59. ButterflyPurple,
  60. ButterflyYellow,
  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);
  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. Slot slot = ManaGarden.SlotList[Random.Range(0, 9) + Garden.CurPage*9];
  429. Vector3 pos = slot.transform.position;
  430. pos.z = -0.35f + Random.Range(-0.01f, 0.01f);
  431. Transform tra = Get("Star", Folder.UI, false, null, pos, ObjType.Star);
  432. Star star = tra.GetComponent<Star>();
  433. tra.parent = slot.transform;
  434. if (star == null)
  435. {
  436. star = tra.AddComponent<Star>();
  437. star.Initialize(pos.y);
  438. }
  439. else
  440. {
  441. star.Initialize(pos.y);
  442. }
  443. return star;
  444. }
  445. public static Drop GetDrop(ObjType objType)
  446. {
  447. ManaAudio.PlayClip(Clip.DropClip);
  448. Vector3 leftPos = Get("MiniLeft").position;
  449. Vector3 rightPos;
  450. if (ManaMiniGame.GameA)
  451. {
  452. rightPos = Get("MiniRight1").position;
  453. }
  454. else
  455. {
  456. rightPos = Get("MiniRight2").position;
  457. }
  458. Vector3 pos = Vector3.Lerp(leftPos, rightPos, Random.Range(0, 1f));
  459. Transform tra;
  460. if (objType == ObjType.DropGold)
  461. {
  462. tra = Get(objType.ToString(), Folder.UI, false, null, pos, objType, typeof(DropGold));
  463. }
  464. else if(objType == ObjType.DropDiamond)
  465. {
  466. tra = Get(objType.ToString(), Folder.UI, false, null, pos, objType, typeof(DropDiamond));
  467. }
  468. else
  469. {
  470. throw new Exception();
  471. }
  472. Drop drop = tra.GetComponent<Drop>();
  473. drop.RegistImmed();
  474. drop.ResetStatus();
  475. return drop;
  476. }
  477. public static Flower GetFlower(FlowerInfo flowerInfo, Slot slot, bool collider)
  478. {
  479. Transform tra = Get("Flower", Folder.Scene, false, slot.transform, false, ObjType.Flower, typeof(Flower));
  480. tra.localScale = new Vector3(1, 1, 1);
  481. Flower flower = tra.GetComponent<Flower>();
  482. flower.RegistImmed();
  483. flower.FlowerInfo = flowerInfo;
  484. flower.Slot = slot;
  485. flower.SetCollider(collider);
  486. return flower;
  487. }
  488. public static Flower GetFlower(FlowerInfo flowerInfo, Transform par)
  489. {
  490. Transform tra = Get("Flower", Folder.Scene, false, par, false, ObjType.Flower, typeof(Flower));
  491. tra.localScale = new Vector3(1, 1, 1);
  492. Flower flower = tra.GetComponent<Flower>();
  493. flower.RegistImmed();
  494. flower.FlowerInfo = flowerInfo;
  495. flower.SetCollider(false);
  496. return flower;
  497. }
  498. public static Text GetInfoItem()
  499. {
  500. Transform tra = Get("InfoItem", Folder.UI, false, Get("J_Info"), false, ObjType.InfoItem);
  501. tra.SetAsFirstSibling();
  502. Text text = tra.GetComponent<Text>();
  503. TweenRoot tween = text.CreateTweenGra(0, 1, 0.25f, true, true, Curve.EaseOutQuad);
  504. tween.OnBackwardFinish = () =>
  505. {
  506. Save(text);
  507. };
  508. return text;
  509. }
  510. 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)
  511. {
  512. Vector3 pos;
  513. if (scene)
  514. {
  515. pos = Camera.main.WorldToScreenPoint(posTra.position);
  516. }
  517. else
  518. {
  519. pos = posTra.position;
  520. }
  521. Transform tra = Get("HudText", Folder.UI, false, parTra, pos, ObjType.HudText, typeof(HudText));
  522. HudText hudText = tra.GetComponent<HudText>();
  523. hudText.Show(str, color, size, speed, time, stay);
  524. return hudText;
  525. }
  526. public static Transform GetElf(Flower flower, Vector4 offset, ObjType obj)
  527. {
  528. Transform tra;
  529. Bounds bounds = flower.FlowerIcon.bounds;
  530. Vector3 pos = flower.FlowerIcon.transform.position;
  531. Transform par = flower.transform;
  532. pos.z -= Random.Range(0.001f, 0.1f);
  533. tra = Get(obj.ToString(), Folder.Scene, false, par, pos, obj);
  534. Elf elf = tra.GetChild(0).GetComponent<Elf>();
  535. if (elf == null)
  536. {
  537. elf = tra.GetChild(0).AddScript<Elf>();
  538. }
  539. if (Random.Range(0f, 1f) <= 0.5f)
  540. {
  541. tra.SetEY(180);
  542. }
  543. else
  544. {
  545. tra.SetEY(0);
  546. }
  547. float offsetX = Mathf.Lerp(offset.x*bounds.extents.x, offset.y*bounds.extents.x, Random.Range(0f, 1f));
  548. float offsetY = Mathf.Lerp(offset.z, offset.w*bounds.extents.y, Random.Range(0f, 1f));
  549. tra.position += new Vector3(offsetX, offsetY, 0);
  550. elf.Flower = flower;
  551. elf.Animator.SetTrigger("Play");
  552. return tra;
  553. }
  554. public static Transform GetSkillItem(SkillRoot skillRoot)
  555. {
  556. Transform tra;
  557. if (skillRoot.SkillTab == SkillTab.Elf)
  558. {
  559. tra = Get("SkillItem", Folder.UI, false, Get("Fd_Grid"), false, ObjType.SkillItem);
  560. }
  561. else if (skillRoot.SkillTab == SkillTab.Store)
  562. {
  563. tra = Get("SkillItem", Folder.UI, false, Get("Fc_Grid"), false, ObjType.SkillItem);
  564. }
  565. else if (skillRoot.SkillTab == SkillTab.Magic)
  566. {
  567. tra = Get("SkillItem", Folder.UI, false, Get("Fb_Grid"), false, ObjType.SkillItem);
  568. }
  569. else if (skillRoot.SkillTab == SkillTab.Garden)
  570. {
  571. tra = Get("SkillItem", Folder.UI, false, Get("Fa_Grid"), false, ObjType.SkillItem);
  572. }
  573. else
  574. {
  575. throw new Exception();
  576. }
  577. skillRoot.SkillItem = tra;
  578. return tra;
  579. }
  580. public static Transform GetAchieveItem()
  581. {
  582. Transform tra = Get("AchieveItem", Folder.UI, false, ManaReso.Get("M_Grid"), false, ObjType.AchieveItem);
  583. return tra;
  584. }
  585. public static ParticleSystem GetFirework(Vector3 pos)
  586. {
  587. Transform tra = Get("Firework", Folder.Effect, false, null, pos, ObjType.Firework, typeof(Effect));
  588. ParticleSystem particle = tra.GetComponent<ParticleSystem>();
  589. particle.Play();
  590. return particle;
  591. }
  592. public static ParticleSystem GetLightwall()
  593. {
  594. Transform tra = Get("LightwallUI", Folder.Effect, false, Get("Canvas"), false, ObjType.LightwallUI, typeof(Effect));
  595. ParticleSystem particle = tra.GetComponent<ParticleSystem>();
  596. particle.Play();
  597. return particle;
  598. }
  599. public static void AsyncLoad(string goName, Folder folder, UnityAction callback = null)
  600. {
  601. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  602. AssetBundleRequest bundleRequest = Bundle.LoadAsync(goName, folder);
  603. AsyncRequest asyncRequest = new AsyncRequest();
  604. asyncRequest.Request = bundleRequest;
  605. asyncRequest.Callback = callback;
  606. kv.Key = asyncRequest;
  607. kv.Value = () =>
  608. {
  609. ObjDic.UniqueAdd(goName, bundleRequest.asset);
  610. };
  611. RequestList.Add(bundleRequest);
  612. InstantiateList.Add(kv);
  613. }
  614. public static void AsyncLoad(string goName, int amt, Folder folder, ObjType objType, bool ui = false, bool canvas = false, UnityAction callback = null)
  615. {
  616. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  617. AssetBundleRequest bundleRequest = Bundle.LoadAsync(goName, folder);
  618. AsyncRequest asyncRequest = new AsyncRequest();
  619. asyncRequest.Request = bundleRequest;
  620. asyncRequest.Callback = callback;
  621. kv.Key = asyncRequest;
  622. kv.Value = () =>
  623. {
  624. ObjDic.UniqueAdd(goName, bundleRequest.asset);
  625. if (objType != ObjType.Null)
  626. {
  627. ObjectPool.UniqueAdd(objType, new List<Transform>());
  628. }
  629. GameObject go;
  630. if (ui)
  631. {
  632. go = (GameObject)Instantiate(bundleRequest.asset, GameObject.Find("ManagerGame").transform.GetChild(0).GetChild(0));
  633. }
  634. else
  635. {
  636. go = (GameObject)Instantiate(bundleRequest.asset);
  637. }
  638. ObjPoolDic.Add(go, objType);
  639. go.name = go.name.Replace("(Clone)", "");
  640. Save(go);
  641. if (ui)
  642. {
  643. go.SetParent(GameObject.Find("ManagerGame").transform.GetChild(0).GetChild(0));
  644. }
  645. };
  646. RequestList.Add(bundleRequest);
  647. for (int i = 0; i < amt; i++)
  648. {
  649. InstantiateList.Add(kv);
  650. }
  651. }
  652. public static void AddAsyncLoad(string goName, Folder folder, UnityAction callback = null)
  653. {
  654. AsyncList.Add
  655. (
  656. () =>
  657. {
  658. AsyncLoad(goName, folder, callback);
  659. }
  660. );
  661. }
  662. public static void AddAsyncLoad(string goName, int amt, Folder folder, ObjType objType, bool ui = false, bool canvas = false, UnityAction callback = null)
  663. {
  664. AsyncList.Add
  665. (
  666. () =>
  667. {
  668. AsyncLoad(goName, amt, folder, objType, ui, canvas, callback);
  669. }
  670. );
  671. }
  672. public static void AddAsyncPlayer(string player)
  673. {
  674. AsyncList.Add
  675. (
  676. () =>
  677. {
  678. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  679. AssetBundleRequest bundleRequest = Bundle.LoadAsync("Player", Folder.Scene);
  680. AsyncRequest asyncRequest = new AsyncRequest();
  681. asyncRequest.Request = bundleRequest;
  682. asyncRequest.Callback = null;
  683. kv.Key = asyncRequest;
  684. kv.Value = () =>
  685. {
  686. GameObject go = (GameObject)Instantiate(bundleRequest.asset);
  687. go.name = go.name.Replace("(Clone)", "");
  688. ObjDic.UniqueAdd("Player", bundleRequest.asset);
  689. ObjType objType;
  690. if (player == "PlayerPink")
  691. {
  692. objType = ObjType.PlayerPink;
  693. ObjectPool.Add(objType, new List<Transform>());
  694. go.AddScript<Player>().BuildPink();
  695. ObjPoolDic.Add(go, objType);
  696. }
  697. else if (player == "PlayerBlond")
  698. {
  699. objType = ObjType.PlayerBlond;
  700. ObjectPool.Add(objType, new List<Transform>());
  701. go.AddScript<Player>().BuildBlond();
  702. ObjPoolDic.Add(go, objType);
  703. }
  704. else if(player == "PlayerBrown")
  705. {
  706. objType = ObjType.PlayerBrown;
  707. ObjectPool.Add(objType, new List<Transform>());
  708. go.AddScript<Player>().BuildBrown();
  709. ObjPoolDic.Add(go, objType);
  710. }
  711. else
  712. {
  713. throw new Exception();
  714. }
  715. Save(go);
  716. };
  717. RequestList.Add(bundleRequest);
  718. InstantiateList.Add(kv);
  719. }
  720. );
  721. }
  722. public void StopAsync()
  723. {
  724. StopCoroutine(CoroLoad);
  725. StopCoroutine(CoroInstantiate);
  726. }
  727. public static IEnumerator IAsyncLoad()
  728. {
  729. while (true)
  730. {
  731. if (RequestList.Count > 0)
  732. {
  733. yield return null;
  734. }
  735. else
  736. {
  737. if (AsyncList.Valid() && !AsyncLoadLock)
  738. {
  739. AsyncLoadLock = true;
  740. Auxiliary.Instance.DelayCall
  741. (
  742. () =>
  743. {
  744. AsyncLoadLock = false;
  745. AsyncList[0].SafeInvoke();
  746. AsyncList.RemoveAt(0);
  747. },
  748. 1
  749. );
  750. yield return null;
  751. }
  752. else
  753. {
  754. yield return null;
  755. }
  756. }
  757. }
  758. }
  759. public static IEnumerator IAsyncInstancitate()
  760. {
  761. while (true)
  762. {
  763. if (AsyncInstantiateLock)
  764. {
  765. yield return null;
  766. }
  767. if (!InstantiateList.Valid())
  768. {
  769. yield return null;
  770. }
  771. else
  772. {
  773. if (Time.deltaTime > 0.333f)
  774. {
  775. yield return null;
  776. }
  777. if (!InstantiateList[0].Key.Request.isDone)
  778. {
  779. yield return null;
  780. }
  781. else
  782. {
  783. InstantiateList[0].Key.Callback.SafeInvoke();
  784. RequestList.Remove(InstantiateList[0].Key.Request);
  785. UnityAction action = InstantiateList[0].Value;
  786. InstantiateList.RemoveAt(0);
  787. AsyncInstantiateLock = true;
  788. Auxiliary.Instance.DelayCall
  789. (
  790. () =>
  791. {
  792. if (!Logo.Complete)
  793. {
  794. action.SafeInvoke();
  795. AsyncInstantiateLock = false;
  796. }
  797. },
  798. 1
  799. );
  800. yield return null;
  801. }
  802. }
  803. }
  804. }
  805. }