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