ManaReso.cs 25 KB

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