ManaReso.cs 25 KB

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