ManaReso.cs 24 KB

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