ManaReso.cs 24 KB

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