ManaReso.cs 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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. Atlas,
  18. Effect,
  19. Audio,
  20. Scene,
  21. Config,
  22. }
  23. public enum ObjType
  24. {
  25. Null,
  26. Firework,
  27. LightwallUI,
  28. PlayerPink,
  29. PlayerBlond,
  30. PlayerBrown,
  31. Page,
  32. Flower,
  33. Garden,
  34. Tutorial,
  35. Canvas,
  36. GroupA,
  37. GroupB,
  38. GroupC,
  39. GroupD,
  40. EventSystem,
  41. MainCamera,
  42. HudText,
  43. SkillItem,
  44. SignItem,
  45. FlowerItem,
  46. AchieveItem,
  47. Music,
  48. DropGold,
  49. DropDiamond,
  50. BeeRed,
  51. BeeBlue,
  52. BeeWhite,
  53. BeePurple,
  54. BeeYellow,
  55. ButterflyRed,
  56. ButterflyBlue,
  57. ButterflyWhite,
  58. ButterflyPurple,
  59. ButterflyYellow,
  60. }
  61. public class AsyncRequest
  62. {
  63. public UnityAction Callback;
  64. public AssetBundleRequest Request;
  65. }
  66. public class ManaReso : Regist
  67. {
  68. #region 变量
  69. public static bool AsyncLoadLock;
  70. public static bool AsyncInstantiateLock;
  71. public static Coroutine CoroAsync;
  72. public static Coroutine CoroInstantiate;
  73. public static ManaReso Instance;
  74. public static UnityAction OnAsyncLoadFinish;
  75. public static List<UnityAction> AsyncList = new List<UnityAction>();
  76. public static List<AssetBundleRequest> RequestList = new List<AssetBundleRequest>();
  77. public static List<KV<AsyncRequest, UnityAction>> InstantiateList = new List<KV<AsyncRequest, UnityAction>>();
  78. public static Dictionary<string, Object> ObjDic = new Dictionary<string, Object>();
  79. public static Dictionary<string, Transform> TraDic = new Dictionary<string, Transform>();
  80. public static Dictionary<GameObject, ObjType> ObjPoolDic = new Dictionary<GameObject, ObjType>();
  81. public static Dictionary<ObjType, List<Transform>> ObjectPool = new Dictionary<ObjType, List<Transform>>();
  82. #endregion
  83. public override void RegistImmed()
  84. {
  85. if (RegistFlag)
  86. {
  87. return;
  88. }
  89. else
  90. {
  91. RegistFlag = true;
  92. }
  93. Instance = this;
  94. Transform objPool = new GameObject("ObjPool").transform;
  95. objPool.parent = transform;
  96. objPool.SetActive(false);
  97. TraDic.Add(objPool.name, objPool);
  98. CoroAsync = StartCoroutine(IAsyncLoad());
  99. CoroInstantiate = StartCoroutine(IAsyncInstancitate());
  100. }
  101. #region TraDic
  102. public static T Get<T>(string goName)
  103. {
  104. Transform tra;
  105. if (TraDic.TryGetValue(goName, out tra))
  106. {
  107. T t = tra.GetComponent<T>();
  108. if (t == null)
  109. {
  110. throw new Exception(goName);
  111. }
  112. return t;
  113. }
  114. else
  115. {
  116. throw new Exception(goName);
  117. }
  118. }
  119. public static T[] Gets<T>(string goName)
  120. {
  121. Transform tra;
  122. if (TraDic.TryGetValue(goName, out tra))
  123. {
  124. T[] ts = tra.GetComponentsInChildren<T>();
  125. if (ts == null)
  126. {
  127. throw new Exception(goName);
  128. }
  129. return ts;
  130. }
  131. else
  132. {
  133. throw new Exception(goName);
  134. }
  135. }
  136. public static Transform Get(string goName)
  137. {
  138. Transform tra;
  139. if (TraDic.TryGetValue(goName, out tra))
  140. {
  141. return tra;
  142. }
  143. else
  144. {
  145. throw new Exception(goName);
  146. }
  147. }
  148. #endregion
  149. #region ObjPool
  150. public static void Release()
  151. {
  152. //ObjDic = new Dictionary<string, Object>();
  153. //Bundle.UI.Unload(true);
  154. //Bundle.Effect.Unload(true);
  155. //Bundle.Audio.Unload(true);
  156. //Bundle.Scene.Unload(true);
  157. //Bundle.Config.Unload(true);
  158. }
  159. public static void Save<T>(T t, bool aware = false) where T : Component
  160. {
  161. Save(t.gameObject);
  162. }
  163. public static void Save(GameObject go, bool aware = false)
  164. {
  165. ObjType objType;
  166. if (!ObjPoolDic.TryGetValue(go, out objType))
  167. {
  168. throw new Exception();
  169. }
  170. List<Transform> traList;
  171. if (ObjectPool.TryGetValue(objType, out traList))
  172. {
  173. if (traList.Contains(go.transform))
  174. {
  175. if (aware)
  176. {
  177. return;
  178. }
  179. else
  180. {
  181. throw new Exception();
  182. }
  183. }
  184. go.SetActive(false);
  185. traList.Add(go.transform);
  186. go.transform.SetParent(Get("ObjPool"));
  187. }
  188. else
  189. {
  190. throw new Exception();
  191. }
  192. }
  193. public static bool Contains<T>(T t) where T : Component
  194. {
  195. return Contains(t.gameObject);
  196. }
  197. public static bool Contains(GameObject go)
  198. {
  199. foreach (var kv in ObjectPool)
  200. {
  201. if (kv.Value.Contains(go.transform))
  202. {
  203. return true;
  204. }
  205. }
  206. return false;
  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. if (objType != ObjType.Null)
  291. {
  292. ObjectPool.UniqueAdd(objType, new List<Transform>());
  293. }
  294. return (T) obj;
  295. }
  296. else
  297. {
  298. T t = Bundle.Load<T>(goName, folder);
  299. if (t == null)
  300. {
  301. throw new Exception(goName);
  302. }
  303. ObjDic.Add(goName, t);
  304. if (objType != ObjType.Null)
  305. {
  306. ObjectPool.UniqueAdd(objType, new List<Transform>());
  307. }
  308. return t;
  309. }
  310. }
  311. public static Sprite LoadSprite(string goName)
  312. {
  313. return Load<Sprite>(goName, Folder.UI);
  314. }
  315. public static Transform Get(string goName, Folder folder, bool compile, Transform par, bool worldSpace, ObjType objType = ObjType.Null, Type type = null)
  316. {
  317. Transform tra = Get(objType);
  318. if (tra == null)
  319. {
  320. GameObject go = Load<GameObject>(goName, folder, objType);
  321. go = Instantiate(go, par, worldSpace);
  322. go.name = go.name.Replace("(Clone)", "");
  323. if (compile)
  324. {
  325. Auxiliary.CompileDic(go.transform, TraDic);
  326. }
  327. if (objType != ObjType.Null)
  328. {
  329. ObjPoolDic.Add(go, objType);
  330. }
  331. if (type != null)
  332. {
  333. go.AddComponent(type);
  334. }
  335. return go.transform;
  336. }
  337. else
  338. {
  339. if (compile)
  340. {
  341. Auxiliary.CompileDic(tra, TraDic);
  342. }
  343. GameObject prefab = Load<GameObject>(goName, folder, objType);
  344. tra.SetParent(par);
  345. if (worldSpace)
  346. {
  347. tra.position = prefab.transform.position;
  348. }
  349. else
  350. {
  351. tra.localPosition = prefab.transform.position;
  352. }
  353. return tra;
  354. }
  355. }
  356. public static Transform Get(string goName, Folder folder, bool compile, Transform par, Vector3 pos, ObjType objType = ObjType.Null, Type type = null)
  357. {
  358. Transform tra = Get(objType);
  359. if (tra == null)
  360. {
  361. GameObject go = Load<GameObject>(goName, folder, objType);
  362. go = Instantiate(go, pos, Quaternion.identity, par);
  363. go.name = go.name.Replace("(Clone)", "");
  364. if (compile)
  365. {
  366. Auxiliary.CompileDic(go.transform, TraDic);
  367. }
  368. if (objType != ObjType.Null)
  369. {
  370. ObjPoolDic.Add(go, objType);
  371. }
  372. if (type != null)
  373. {
  374. go.AddComponent(type);
  375. }
  376. return go.transform;
  377. }
  378. else
  379. {
  380. if (compile)
  381. {
  382. Auxiliary.CompileDic(tra, TraDic);
  383. }
  384. tra.SetParent(par);
  385. tra.position = pos;
  386. return tra;
  387. }
  388. }
  389. public static Drop GetDrop(ObjType objType)
  390. {
  391. ManaAudio.PlayClip(Clip.DropClip);
  392. Vector3 pos = Vector3.Lerp(Get("MiniLeft").position, Get("MiniRight").position, Random.Range(0, 1f));
  393. Transform tra;
  394. if (objType == ObjType.DropGold)
  395. {
  396. tra = Get(objType.ToString(), Folder.UI, false, null, pos, objType, typeof(DropGold));
  397. }
  398. else if(objType == ObjType.DropDiamond)
  399. {
  400. tra = Get(objType.ToString(), Folder.UI, false, null, pos, objType, typeof(DropDiamond));
  401. }
  402. else
  403. {
  404. throw new Exception();
  405. }
  406. Drop drop = tra.GetComponent<Drop>();
  407. drop.RegistImmed();
  408. drop.ResetStatus();
  409. return drop;
  410. }
  411. public static Flower GetFlower(FlowerInfo flowerInfo, Slot slot, bool collider)
  412. {
  413. Transform tra = Get("Flower", Folder.Scene, false, slot.transform, false, ObjType.Flower, typeof(Flower));
  414. tra.localScale = new Vector3(1, 1, 1);
  415. Flower flower = tra.GetComponent<Flower>();
  416. flower.RegistImmed();
  417. flower.FlowerInfo = flowerInfo;
  418. flower.Slot = slot;
  419. flower.SetCollider(collider);
  420. return flower;
  421. }
  422. public static Flower GetFlower(FlowerInfo flowerInfo, Transform par)
  423. {
  424. Transform tra = Get("Flower", Folder.Scene, false, par, false, ObjType.Flower, typeof(Flower));
  425. tra.localScale = new Vector3(1, 1, 1);
  426. Flower flower = tra.GetComponent<Flower>();
  427. flower.RegistImmed();
  428. flower.FlowerInfo = flowerInfo;
  429. flower.SetCollider(false);
  430. return flower;
  431. }
  432. public static HudText GetHudText(string str, Color color, int size, Transform posTra, Transform parTra, bool scene, float speed = 7.5f, float time = 0.5f)
  433. {
  434. Vector3 pos;
  435. if (scene)
  436. {
  437. pos = Camera.main.WorldToScreenPoint(posTra.position);
  438. }
  439. else
  440. {
  441. pos = posTra.position;
  442. }
  443. Transform tra = Get("HudText", Folder.UI, false, parTra, pos, ObjType.HudText, typeof(HudText));
  444. HudText hudText = tra.GetComponent<HudText>();
  445. hudText.Show(str, color, size, speed, time);
  446. return hudText;
  447. }
  448. public static Transform GetElf(Flower flower, Vector4 offset, ObjType obj)
  449. {
  450. Transform tra;
  451. Bounds bounds = flower.FlowerIcon.bounds;
  452. Vector3 pos = flower.FlowerIcon.transform.position;
  453. Transform par = flower.transform;
  454. pos.z -= Random.Range(0.001f, 0.1f);
  455. tra = Get(obj.ToString(), Folder.Scene, false, par, pos, obj);
  456. Elf elf = tra.GetChild(0).GetComponent<Elf>();
  457. if (elf == null)
  458. {
  459. elf = tra.GetChild(0).AddScript<Elf>();
  460. }
  461. if (Random.Range(0f, 1f) <= 0.5f)
  462. {
  463. tra.SetEY(180);
  464. }
  465. else
  466. {
  467. tra.SetEY(0);
  468. }
  469. float offsetX = Mathf.Lerp(offset.x*bounds.extents.x, offset.y*bounds.extents.x, Random.Range(0f, 1f));
  470. float offsetY = Mathf.Lerp(offset.z, offset.w*bounds.extents.y, Random.Range(0f, 1f));
  471. tra.position += new Vector3(offsetX, offsetY, 0);
  472. elf.Flower = flower;
  473. elf.Animator.SetTrigger("Play");
  474. return tra;
  475. }
  476. public static Transform GetSkillItem(SkillRoot skillRoot)
  477. {
  478. Transform tra;
  479. if (skillRoot.SkillTab == SkillTab.Elf)
  480. {
  481. tra = Get("SkillItem", Folder.UI, false, Get("Fd_Grid"), false, ObjType.SkillItem);
  482. }
  483. else if (skillRoot.SkillTab == SkillTab.Store)
  484. {
  485. tra = Get("SkillItem", Folder.UI, false, Get("Fc_Grid"), false, ObjType.SkillItem);
  486. }
  487. else if (skillRoot.SkillTab == SkillTab.Magic)
  488. {
  489. tra = Get("SkillItem", Folder.UI, false, Get("Fb_Grid"), false, ObjType.SkillItem);
  490. }
  491. else if (skillRoot.SkillTab == SkillTab.Garden)
  492. {
  493. tra = Get("SkillItem", Folder.UI, false, Get("Fa_Grid"), false, ObjType.SkillItem);
  494. }
  495. else
  496. {
  497. throw new Exception();
  498. }
  499. skillRoot.SkillItem = tra;
  500. return tra;
  501. }
  502. public static Transform GetAchieveItem()
  503. {
  504. Transform tra = Get("AchieveItem", Folder.UI, false, ManaReso.Get("M_Grid"), false, ObjType.AchieveItem);
  505. return tra;
  506. }
  507. public static ParticleSystem GetFirework(Vector3 pos)
  508. {
  509. Transform tra = Get("Firework", Folder.Effect, false, null, pos, ObjType.Firework, typeof(Effect));
  510. ParticleSystem particle = tra.GetComponent<ParticleSystem>();
  511. particle.Play();
  512. return particle;
  513. }
  514. public static ParticleSystem GetLightwall()
  515. {
  516. Transform tra = Get("LightwallUI", Folder.Effect, false, Get("Canvas"), false, ObjType.LightwallUI, typeof(Effect));
  517. ParticleSystem particle = tra.GetComponent<ParticleSystem>();
  518. particle.Play();
  519. return particle;
  520. }
  521. public static void AsyncLoad(string goName, Folder folder, UnityAction callback = null)
  522. {
  523. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  524. AssetBundleRequest bundleRequest = Bundle.LoadAsync(goName, folder);
  525. //if (folder == Folder.UI)
  526. //{
  527. // bundleRequest = Bundle.UI.LoadAssetAsync(goName);
  528. //}
  529. //else if (folder == Folder.Audio)
  530. //{
  531. // bundleRequest = Bundle.Audio.LoadAssetAsync(goName);
  532. //}
  533. //else if (folder == Folder.Config)
  534. //{
  535. // bundleRequest = Bundle.Config.LoadAssetAsync(goName);
  536. //}
  537. //else if (folder == Folder.Effect)
  538. //{
  539. // bundleRequest = Bundle.Effect.LoadAssetAsync(goName);
  540. //}
  541. //else if (folder == Folder.Scene)
  542. //{
  543. // bundleRequest = Bundle.Scene.LoadAssetAsync(goName);
  544. //}
  545. //else
  546. //{
  547. // throw new Exception();
  548. //}
  549. AsyncRequest asyncRequest = new AsyncRequest();
  550. asyncRequest.Request = bundleRequest;
  551. asyncRequest.Callback = callback;
  552. kv.Key = asyncRequest;
  553. kv.Value = () =>
  554. {
  555. ObjDic.UniqueAdd(goName, bundleRequest.asset);
  556. };
  557. RequestList.Add(bundleRequest);
  558. InstantiateList.Add(kv);
  559. }
  560. public static void AsyncLoad(string goName, int amt, Folder folder, ObjType objType, bool ui = false, bool canvas = false, UnityAction callback = null, Type type = null)
  561. {
  562. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  563. AssetBundleRequest bundleRequest = Bundle.LoadAsync(goName, folder);
  564. //if (folder == Folder.UI)
  565. //{
  566. // bundleRequest = Bundle.UI.LoadAssetAsync(goName);
  567. //}
  568. //else if (folder == Folder.Audio)
  569. //{
  570. // bundleRequest = Bundle.Audio.LoadAssetAsync(goName);
  571. //}
  572. //else if (folder == Folder.Config)
  573. //{
  574. // bundleRequest = Bundle.Config.LoadAssetAsync(goName);
  575. //}
  576. //else if (folder == Folder.Effect)
  577. //{
  578. // bundleRequest = Bundle.Effect.LoadAssetAsync(goName);
  579. //}
  580. //else if (folder == Folder.Scene)
  581. //{
  582. // bundleRequest = Bundle.Scene.LoadAssetAsync(goName);
  583. //}
  584. //else
  585. //{
  586. // throw new Exception();
  587. //}
  588. AsyncRequest asyncRequest = new AsyncRequest();
  589. asyncRequest.Request = bundleRequest;
  590. asyncRequest.Callback = callback;
  591. kv.Key = asyncRequest;
  592. kv.Value = () =>
  593. {
  594. ObjDic.UniqueAdd(goName, bundleRequest.asset);
  595. if (objType != ObjType.Null)
  596. {
  597. ObjectPool.UniqueAdd(objType, new List<Transform>());
  598. }
  599. GameObject go;
  600. if (ui)
  601. {
  602. go = (GameObject)Instantiate(bundleRequest.asset, GameObject.Find("ManagerGame").transform.GetChild(0).GetChild(0));
  603. }
  604. else
  605. {
  606. go = (GameObject)Instantiate(bundleRequest.asset);
  607. }
  608. ObjPoolDic.Add(go, objType);
  609. if (type != null)
  610. {
  611. go.AddComponent(type);
  612. }
  613. go.name = go.name.Replace("(Clone)", "");
  614. Save(go);
  615. if (ui)
  616. {
  617. go.SetParent(GameObject.Find("ManagerGame").transform.GetChild(0).GetChild(0));
  618. }
  619. };
  620. RequestList.Add(bundleRequest);
  621. for (int i = 0; i < amt; i++)
  622. {
  623. InstantiateList.Add(kv);
  624. }
  625. }
  626. public static void AddAsyncLoad(string goName, Folder folder, UnityAction callback = null)
  627. {
  628. AsyncList.Add
  629. (
  630. () =>
  631. {
  632. AsyncLoad(goName, folder, callback);
  633. }
  634. );
  635. }
  636. public static void AddAsyncLoad(string goName, int amt, Folder folder, ObjType objType, bool ui = false, bool canvas = false, UnityAction callback = null, Type type = null)
  637. {
  638. AsyncList.Add
  639. (
  640. () =>
  641. {
  642. AsyncLoad(goName, amt, folder, objType, ui, canvas, callback, type);
  643. }
  644. );
  645. }
  646. public static void AddAsyncPlayer(string player)
  647. {
  648. AsyncList.Add
  649. (
  650. () =>
  651. {
  652. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  653. AssetBundleRequest bundleRequest = Bundle.LoadAsync("Player", Folder.Scene);
  654. //Bundle.Scene.LoadAssetAsync("Player");
  655. AsyncRequest asyncRequest = new AsyncRequest();
  656. asyncRequest.Request = bundleRequest;
  657. asyncRequest.Callback = null;
  658. kv.Key = asyncRequest;
  659. kv.Value = () =>
  660. {
  661. GameObject go = (GameObject)Instantiate(bundleRequest.asset);
  662. go.name = go.name.Replace("(Clone)", "");
  663. ObjDic.UniqueAdd("Player", bundleRequest.asset);
  664. ObjType objType;
  665. if (player == "PlayerPink")
  666. {
  667. objType = ObjType.PlayerPink;
  668. ObjectPool.Add(objType, new List<Transform>());
  669. go.AddScript<Player>().BuildPink();
  670. ObjPoolDic.Add(go, objType);
  671. }
  672. else if (player == "PlayerBlond")
  673. {
  674. objType = ObjType.PlayerBlond;
  675. ObjectPool.Add(objType, new List<Transform>());
  676. go.AddScript<Player>().BuildBlond();
  677. ObjPoolDic.Add(go, objType);
  678. }
  679. else if(player == "PlayerBrown")
  680. {
  681. objType = ObjType.PlayerBrown;
  682. ObjectPool.Add(objType, new List<Transform>());
  683. go.AddScript<Player>().BuildBrown();
  684. ObjPoolDic.Add(go, objType);
  685. }
  686. else
  687. {
  688. throw new Exception();
  689. }
  690. Save(go);
  691. };
  692. RequestList.Add(bundleRequest);
  693. InstantiateList.Add(kv);
  694. }
  695. );
  696. }
  697. public void StopAsync()
  698. {
  699. StopCoroutine(CoroAsync);
  700. StopCoroutine(CoroInstantiate);
  701. }
  702. public static IEnumerator IAsyncLoad()
  703. {
  704. while (true)
  705. {
  706. if (RequestList.Count == 0 && !AsyncLoadLock)
  707. {
  708. OnAsyncLoadFinish.SafeInvoke();
  709. OnAsyncLoadFinish = null;
  710. }
  711. if (RequestList.Count > 0)
  712. {
  713. yield return null;
  714. }
  715. else
  716. {
  717. if (AsyncList.Count > 0 && !AsyncLoadLock)
  718. {
  719. AsyncLoadLock = true;
  720. Auxiliary.Instance.DelayCall
  721. (
  722. () =>
  723. {
  724. AsyncLoadLock = false;
  725. AsyncList[0].SafeInvoke();
  726. AsyncList.RemoveAt(0);
  727. },
  728. 1
  729. );
  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. public void Read()
  783. {
  784. StartCoroutine(IRead1());
  785. StartCoroutine(IRead2());
  786. StartCoroutine(IRead3());
  787. }
  788. public static IEnumerator IRead1()
  789. {
  790. WWW www = new WWW(Application.streamingAssetsPath + "/Icon/ShareIcon.png");
  791. yield return www;
  792. if (string.IsNullOrEmpty(www.error))
  793. {
  794. Get<RawImage>("RawImage").texture = www.texture;
  795. ManaDebug.Log("1 good");
  796. }
  797. else
  798. {
  799. ManaDebug.Log("1 fail");
  800. }
  801. }
  802. public static IEnumerator IRead2()
  803. {
  804. WWW www = new WWW("file://" + Application.streamingAssetsPath + "/Icon/ShareIcon.png");
  805. yield return www;
  806. if (string.IsNullOrEmpty(www.error))
  807. {
  808. Get<RawImage>("RawImage").texture = www.texture;
  809. ManaDebug.Log("2 good");
  810. }
  811. else
  812. {
  813. ManaDebug.Log("2 fail");
  814. }
  815. }
  816. public static IEnumerator IRead3()
  817. {
  818. WWW www = new WWW("file:///" + Application.streamingAssetsPath + "/Icon/ShareIcon.png");
  819. yield return www;
  820. if (string.IsNullOrEmpty(www.error))
  821. {
  822. Get<RawImage>("RawImage").texture = www.texture;
  823. ManaDebug.Log("3 good");
  824. }
  825. else
  826. {
  827. ManaDebug.Log("3 fail");
  828. }
  829. }
  830. }