ManaReso.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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 Save<T>(T t, bool aware = false) where T : Component
  151. {
  152. Save(t.gameObject);
  153. }
  154. public static void Save(GameObject go, bool aware = false)
  155. {
  156. ObjType objType;
  157. if (!ObjPoolDic.TryGetValue(go, out objType))
  158. {
  159. throw new Exception();
  160. }
  161. List<Transform> traList;
  162. if (ObjectPool.TryGetValue(objType, out traList))
  163. {
  164. if (traList.Contains(go.transform))
  165. {
  166. if (aware)
  167. {
  168. return;
  169. }
  170. else
  171. {
  172. throw new Exception();
  173. }
  174. }
  175. go.SetActive(false);
  176. traList.Add(go.transform);
  177. go.transform.SetParent(Get("ObjPool"));
  178. }
  179. else
  180. {
  181. throw new Exception();
  182. }
  183. }
  184. public static bool Contains<T>(T t) where T : Component
  185. {
  186. return Contains(t.gameObject);
  187. }
  188. public static bool Contains(GameObject go)
  189. {
  190. foreach (var kv in ObjectPool)
  191. {
  192. if (kv.Value.Contains(go.transform))
  193. {
  194. return true;
  195. }
  196. }
  197. return false;
  198. }
  199. public static Transform Get(ObjType objType)
  200. {
  201. List<Transform> traList;
  202. if (ObjectPool.TryGetValue(objType, out traList))
  203. {
  204. if (traList.Count > 0)
  205. {
  206. Transform tra = traList[0];
  207. tra.SetActive(true);
  208. traList.RemoveAt(0);
  209. return tra.transform;
  210. }
  211. }
  212. return null;
  213. }
  214. #endregion
  215. #region ShortCut
  216. public static void SetText(string goName)
  217. {
  218. Text text = Get<Text>(goName);
  219. text.text = Language.GetStr("UI", goName);
  220. }
  221. public static void SetText(string goName, string str)
  222. {
  223. Text text = Get<Text>(goName);
  224. text.text = str;
  225. }
  226. public static void SetSprite(string goName, Sprite sprite)
  227. {
  228. Image image = Get<Image>(goName);
  229. image.sprite = sprite;
  230. }
  231. public static void SetActive(string goName, bool active)
  232. {
  233. Get(goName).SetActive(active);
  234. }
  235. public static void SetButtonEvent(string goName, UnityAction onClick)
  236. {
  237. Button button = Get<Button>(goName);
  238. button.onClick = new Button.ButtonClickedEvent();
  239. button.onClick.AddListener(onClick);
  240. }
  241. public static void AddButtonEvent(string goName, UnityAction onClick)
  242. {
  243. Button button = Get<Button>(goName);
  244. button.onClick.AddListener(onClick);
  245. }
  246. public static void PushButtonEvent(string goName, UnityAction onClick)
  247. {
  248. Button button = Get<Button>(goName);
  249. Button.ButtonClickedEvent click = button.onClick;
  250. button.onClick = new Button.ButtonClickedEvent();
  251. button.onClick.AddListener(onClick);
  252. button.onClick.AddListener(click.Invoke);
  253. }
  254. public static void AddButtonEventOnetime(string goName, UnityAction onClick)
  255. {
  256. Button button = Get<Button>(goName);
  257. onClick += () =>
  258. {
  259. button.onClick.RemoveListener(onClick);
  260. };
  261. button.onClick.AddListener(onClick);
  262. }
  263. public static void PushButtonEventOnetime(string goName, UnityAction onClick)
  264. {
  265. Button button = Get<Button>(goName);
  266. onClick += () =>
  267. {
  268. button.onClick.RemoveListener(onClick);
  269. };
  270. Button.ButtonClickedEvent click = button.onClick;
  271. button.onClick = new Button.ButtonClickedEvent();
  272. button.onClick.AddListener(onClick);
  273. button.onClick.AddListener(click.Invoke);
  274. }
  275. #endregion
  276. public static T Load<T>(string goName, Folder folder, ObjType objType = ObjType.Null) where T : Object
  277. {
  278. Object obj;
  279. if (ObjDic.TryGetValue(goName, out obj))
  280. {
  281. if (objType != ObjType.Null)
  282. {
  283. ObjectPool.UniqueAdd(objType, new List<Transform>());
  284. }
  285. return (T) obj;
  286. }
  287. else
  288. {
  289. T t = Bundle.Load<T>(goName, folder);
  290. if (t == null)
  291. {
  292. throw new Exception(goName);
  293. }
  294. ObjDic.Add(goName, t);
  295. if (objType != ObjType.Null)
  296. {
  297. ObjectPool.UniqueAdd(objType, new List<Transform>());
  298. }
  299. return t;
  300. }
  301. }
  302. public static Sprite LoadSprite(string goName)
  303. {
  304. return Load<Sprite>(goName, Folder.UI);
  305. }
  306. public static Transform Get(string goName, Folder folder, bool compile, Transform par, bool worldSpace, ObjType objType = ObjType.Null, Type type = null)
  307. {
  308. Transform tra = Get(objType);
  309. if (tra == null)
  310. {
  311. GameObject go = Load<GameObject>(goName, folder, objType);
  312. go = Instantiate(go, par, worldSpace);
  313. go.name = go.name.Replace("(Clone)", "");
  314. if (compile)
  315. {
  316. Auxiliary.CompileDic(go.transform, TraDic);
  317. }
  318. if (objType != ObjType.Null)
  319. {
  320. ObjPoolDic.Add(go, objType);
  321. }
  322. if (type != null)
  323. {
  324. go.AddComponent(type);
  325. }
  326. return go.transform;
  327. }
  328. else
  329. {
  330. if (compile)
  331. {
  332. Auxiliary.CompileDic(tra, TraDic);
  333. }
  334. GameObject prefab = Load<GameObject>(goName, folder, objType);
  335. tra.SetParent(par);
  336. if (worldSpace)
  337. {
  338. tra.position = prefab.transform.position;
  339. }
  340. else
  341. {
  342. tra.localPosition = prefab.transform.position;
  343. }
  344. return tra;
  345. }
  346. }
  347. public static Transform Get(string goName, Folder folder, bool compile, Transform par, Vector3 pos, ObjType objType = ObjType.Null, Type type = null)
  348. {
  349. Transform tra = Get(objType);
  350. if (tra == null)
  351. {
  352. GameObject go = Load<GameObject>(goName, folder, objType);
  353. go = Instantiate(go, pos, Quaternion.identity, par);
  354. go.name = go.name.Replace("(Clone)", "");
  355. if (compile)
  356. {
  357. Auxiliary.CompileDic(go.transform, TraDic);
  358. }
  359. if (objType != ObjType.Null)
  360. {
  361. ObjPoolDic.Add(go, objType);
  362. }
  363. if (type != null)
  364. {
  365. go.AddComponent(type);
  366. }
  367. return go.transform;
  368. }
  369. else
  370. {
  371. if (compile)
  372. {
  373. Auxiliary.CompileDic(tra, TraDic);
  374. }
  375. tra.SetParent(par);
  376. tra.position = pos;
  377. return tra;
  378. }
  379. }
  380. public static Drop GetDrop(ObjType objType)
  381. {
  382. ManaAudio.PlayClip(Clip.DropClip);
  383. Vector3 pos = Vector3.Lerp(Get("MiniLeft").position, Get("MiniRight").position, Random.Range(0, 1f));
  384. Transform tra;
  385. if (objType == ObjType.DropGold)
  386. {
  387. tra = Get(objType.ToString(), Folder.UI, false, null, pos, objType, typeof(DropGold));
  388. }
  389. else if(objType == ObjType.DropDiamond)
  390. {
  391. tra = Get(objType.ToString(), Folder.UI, false, null, pos, objType, typeof(DropDiamond));
  392. }
  393. else
  394. {
  395. throw new Exception();
  396. }
  397. Drop drop = tra.GetComponent<Drop>();
  398. drop.RegistImmed();
  399. drop.ResetStatus();
  400. return drop;
  401. }
  402. public static Flower GetFlower(FlowerInfo flowerInfo, Slot slot, bool collider)
  403. {
  404. Transform tra = Get("Flower", Folder.Scene, false, slot.transform, false, ObjType.Flower, typeof(Flower));
  405. tra.localScale = new Vector3(1, 1, 1);
  406. Flower flower = tra.GetComponent<Flower>();
  407. flower.RegistImmed();
  408. flower.GoldBk.SetActive(false);
  409. flower.FlowerInfo = flowerInfo;
  410. flower.Slot = slot;
  411. flower.SetCollider(collider);
  412. return flower;
  413. }
  414. public static Flower GetFlower(FlowerInfo flowerInfo, Transform par)
  415. {
  416. Transform tra = Get("Flower", Folder.Scene, false, par, false, ObjType.Flower, typeof(Flower));
  417. tra.localScale = new Vector3(1, 1, 1);
  418. Flower flower = tra.GetComponent<Flower>();
  419. flower.RegistImmed();
  420. flower.GoldBk.SetActive(false);
  421. flower.FlowerInfo = flowerInfo;
  422. flower.SetCollider(false);
  423. return flower;
  424. }
  425. public static HudText GetHudText(string str, Color color, int size, Transform posTra, Transform parTra, bool scene, float speed = 7.5f, float time = 0.5f)
  426. {
  427. Vector3 pos;
  428. if (scene)
  429. {
  430. pos = Camera.main.WorldToScreenPoint(posTra.position);
  431. }
  432. else
  433. {
  434. pos = posTra.position;
  435. }
  436. Transform tra = Get("HudText", Folder.UI, false, parTra, pos, ObjType.HudText, typeof(HudText));
  437. HudText hudText = tra.GetComponent<HudText>();
  438. hudText.Show(str, color, size, speed, time);
  439. return hudText;
  440. }
  441. public static Transform GetElf(Flower flower, Vector4 offset, ObjType obj)
  442. {
  443. Transform tra;
  444. Bounds bounds = flower.FlowerIcon.bounds;
  445. Vector3 pos = flower.FlowerIcon.transform.position;
  446. Transform par = flower.transform;
  447. pos.z -= Random.Range(0.001f, 0.1f);
  448. tra = Get(obj.ToString(), Folder.Scene, false, par, pos, obj);
  449. Elf elf = tra.GetChild(0).GetComponent<Elf>();
  450. if (elf == null)
  451. {
  452. elf = tra.GetChild(0).AddScript<Elf>();
  453. }
  454. if (Random.Range(0f, 1f) <= 0.5f)
  455. {
  456. tra.SetEY(180);
  457. }
  458. else
  459. {
  460. tra.SetEY(0);
  461. }
  462. float offsetX = Mathf.Lerp(offset.x*bounds.extents.x, offset.y*bounds.extents.x, Random.Range(0f, 1f));
  463. float offsetY = Mathf.Lerp(offset.z, offset.w*bounds.extents.y, Random.Range(0f, 1f));
  464. tra.position += new Vector3(offsetX, offsetY, 0);
  465. elf.Flower = flower;
  466. elf.Animator.SetTrigger("Play");
  467. return tra;
  468. }
  469. public static Transform GetSkillItem(SkillRoot skillRoot)
  470. {
  471. Transform tra;
  472. if (skillRoot.SkillTab == SkillTab.Elf)
  473. {
  474. tra = Get("SkillItem", Folder.UI, false, Get("Fd_Grid"), false, ObjType.SkillItem);
  475. }
  476. else if (skillRoot.SkillTab == SkillTab.Store)
  477. {
  478. tra = Get("SkillItem", Folder.UI, false, Get("Fc_Grid"), false, ObjType.SkillItem);
  479. }
  480. else if (skillRoot.SkillTab == SkillTab.Magic)
  481. {
  482. tra = Get("SkillItem", Folder.UI, false, Get("Fb_Grid"), false, ObjType.SkillItem);
  483. }
  484. else if (skillRoot.SkillTab == SkillTab.Garden)
  485. {
  486. tra = Get("SkillItem", Folder.UI, false, Get("Fa_Grid"), false, ObjType.SkillItem);
  487. }
  488. else
  489. {
  490. throw new Exception();
  491. }
  492. skillRoot.SkillItem = tra;
  493. return tra;
  494. }
  495. public static Transform GetAchieveItem()
  496. {
  497. Transform tra = Get("AchieveItem", Folder.UI, false, ManaReso.Get("M_Grid"), false, ObjType.AchieveItem);
  498. return tra;
  499. }
  500. public static ParticleSystem GetFirework(Vector3 pos)
  501. {
  502. Transform tra = Get("Firework", Folder.Effect, false, null, pos, ObjType.Firework, typeof(Effect));
  503. ParticleSystem particle = tra.GetComponent<ParticleSystem>();
  504. particle.Play();
  505. return particle;
  506. }
  507. public static ParticleSystem GetLightwall()
  508. {
  509. Transform tra = Get("LightwallUI", Folder.Effect, false, Get("Canvas"), false, ObjType.LightwallUI, typeof(Effect));
  510. ParticleSystem particle = tra.GetComponent<ParticleSystem>();
  511. particle.Play();
  512. return particle;
  513. }
  514. public static void AsyncLoad(string goName, Folder folder, UnityAction callback = null)
  515. {
  516. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  517. AssetBundleRequest bundleRequest = Bundle.LoadAsync(goName, folder);
  518. //if (folder == Folder.UI)
  519. //{
  520. // bundleRequest = Bundle.UI.LoadAssetAsync(goName);
  521. //}
  522. //else if (folder == Folder.Audio)
  523. //{
  524. // bundleRequest = Bundle.Audio.LoadAssetAsync(goName);
  525. //}
  526. //else if (folder == Folder.Config)
  527. //{
  528. // bundleRequest = Bundle.Config.LoadAssetAsync(goName);
  529. //}
  530. //else if (folder == Folder.Effect)
  531. //{
  532. // bundleRequest = Bundle.Effect.LoadAssetAsync(goName);
  533. //}
  534. //else if (folder == Folder.Scene)
  535. //{
  536. // bundleRequest = Bundle.Scene.LoadAssetAsync(goName);
  537. //}
  538. //else
  539. //{
  540. // throw new Exception();
  541. //}
  542. AsyncRequest asyncRequest = new AsyncRequest();
  543. asyncRequest.Request = bundleRequest;
  544. asyncRequest.Callback = callback;
  545. kv.Key = asyncRequest;
  546. kv.Value = () =>
  547. {
  548. ObjDic.UniqueAdd(goName, bundleRequest.asset);
  549. };
  550. RequestList.Add(bundleRequest);
  551. InstantiateList.Add(kv);
  552. }
  553. public static void AsyncLoad(string goName, int amt, Folder folder, ObjType objType, bool ui = false, bool canvas = false, UnityAction callback = null, Type type = null)
  554. {
  555. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  556. AssetBundleRequest bundleRequest = Bundle.LoadAsync(goName, folder);
  557. //if (folder == Folder.UI)
  558. //{
  559. // bundleRequest = Bundle.UI.LoadAssetAsync(goName);
  560. //}
  561. //else if (folder == Folder.Audio)
  562. //{
  563. // bundleRequest = Bundle.Audio.LoadAssetAsync(goName);
  564. //}
  565. //else if (folder == Folder.Config)
  566. //{
  567. // bundleRequest = Bundle.Config.LoadAssetAsync(goName);
  568. //}
  569. //else if (folder == Folder.Effect)
  570. //{
  571. // bundleRequest = Bundle.Effect.LoadAssetAsync(goName);
  572. //}
  573. //else if (folder == Folder.Scene)
  574. //{
  575. // bundleRequest = Bundle.Scene.LoadAssetAsync(goName);
  576. //}
  577. //else
  578. //{
  579. // throw new Exception();
  580. //}
  581. AsyncRequest asyncRequest = new AsyncRequest();
  582. asyncRequest.Request = bundleRequest;
  583. asyncRequest.Callback = callback;
  584. kv.Key = asyncRequest;
  585. kv.Value = () =>
  586. {
  587. ObjDic.UniqueAdd(goName, bundleRequest.asset);
  588. if (objType != ObjType.Null)
  589. {
  590. ObjectPool.UniqueAdd(objType, new List<Transform>());
  591. }
  592. GameObject go;
  593. if (ui)
  594. {
  595. go = (GameObject)Instantiate(bundleRequest.asset, GameObject.Find("ManagerGame").transform.GetChild(0).GetChild(0));
  596. }
  597. else
  598. {
  599. go = (GameObject)Instantiate(bundleRequest.asset);
  600. }
  601. ObjPoolDic.Add(go, objType);
  602. if (type != null)
  603. {
  604. go.AddComponent(type);
  605. }
  606. go.name = go.name.Replace("(Clone)", "");
  607. Save(go);
  608. if (ui)
  609. {
  610. go.SetParent(GameObject.Find("ManagerGame").transform.GetChild(0).GetChild(0));
  611. }
  612. };
  613. RequestList.Add(bundleRequest);
  614. for (int i = 0; i < amt; i++)
  615. {
  616. InstantiateList.Add(kv);
  617. }
  618. }
  619. public static void AddAsyncLoad(string goName, Folder folder, UnityAction callback = null)
  620. {
  621. AsyncList.Add
  622. (
  623. () =>
  624. {
  625. AsyncLoad(goName, folder, callback);
  626. }
  627. );
  628. }
  629. public static void AddAsyncLoad(string goName, int amt, Folder folder, ObjType objType, bool ui = false, bool canvas = false, UnityAction callback = null, Type type = null)
  630. {
  631. AsyncList.Add
  632. (
  633. () =>
  634. {
  635. AsyncLoad(goName, amt, folder, objType, ui, canvas, callback, type);
  636. }
  637. );
  638. }
  639. public static void AddAsyncPlayer(string player)
  640. {
  641. AsyncList.Add
  642. (
  643. () =>
  644. {
  645. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  646. AssetBundleRequest bundleRequest = Bundle.LoadAsync("Player", Folder.Scene);
  647. //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. ObjPoolDic.Add(go, 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. ObjPoolDic.Add(go, 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. ObjPoolDic.Add(go, 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 && !AsyncLoadLock)
  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 && !AsyncLoadLock)
  711. {
  712. AsyncLoadLock = true;
  713. Auxiliary.Instance.DelayCall
  714. (
  715. () =>
  716. {
  717. AsyncLoadLock = false;
  718. AsyncList[0].SafeInvoke();
  719. AsyncList.RemoveAt(0);
  720. },
  721. 1
  722. );
  723. yield return null;
  724. }
  725. else
  726. {
  727. yield return null;
  728. }
  729. }
  730. }
  731. }
  732. public static IEnumerator IAsyncInstancitate()
  733. {
  734. while (true)
  735. {
  736. if (AsyncInstantiateLock)
  737. {
  738. yield return null;
  739. }
  740. if (!InstantiateList.Valid())
  741. {
  742. yield return null;
  743. }
  744. else
  745. {
  746. if (Time.deltaTime > 0.333f)
  747. {
  748. yield return null;
  749. }
  750. if (!InstantiateList[0].Key.Request.isDone)
  751. {
  752. yield return null;
  753. }
  754. else
  755. {
  756. InstantiateList[0].Key.Callback.SafeInvoke();
  757. RequestList.Remove(InstantiateList[0].Key.Request);
  758. UnityAction action = InstantiateList[0].Value;
  759. InstantiateList.RemoveAt(0);
  760. AsyncInstantiateLock = true;
  761. Auxiliary.Instance.DelayCall
  762. (
  763. () =>
  764. {
  765. action.SafeInvoke();
  766. AsyncInstantiateLock = false;
  767. },
  768. 1
  769. );
  770. yield return null;
  771. }
  772. }
  773. }
  774. }
  775. public void Read()
  776. {
  777. StartCoroutine(IRead1());
  778. StartCoroutine(IRead2());
  779. StartCoroutine(IRead3());
  780. }
  781. public static IEnumerator IRead1()
  782. {
  783. WWW www = new WWW(Application.streamingAssetsPath + "/Icon/ShareIcon.png");
  784. yield return www;
  785. if (string.IsNullOrEmpty(www.error))
  786. {
  787. Get<RawImage>("RawImage").texture = www.texture;
  788. ManaDebug.Log("1 good");
  789. }
  790. else
  791. {
  792. ManaDebug.Log("1 fail");
  793. }
  794. }
  795. public static IEnumerator IRead2()
  796. {
  797. WWW www = new WWW("file://" + Application.streamingAssetsPath + "/Icon/ShareIcon.png");
  798. yield return www;
  799. if (string.IsNullOrEmpty(www.error))
  800. {
  801. Get<RawImage>("RawImage").texture = www.texture;
  802. ManaDebug.Log("2 good");
  803. }
  804. else
  805. {
  806. ManaDebug.Log("2 fail");
  807. }
  808. }
  809. public static IEnumerator IRead3()
  810. {
  811. WWW www = new WWW("file:///" + Application.streamingAssetsPath + "/Icon/ShareIcon.png");
  812. yield return www;
  813. if (string.IsNullOrEmpty(www.error))
  814. {
  815. Get<RawImage>("RawImage").texture = www.texture;
  816. ManaDebug.Log("3 good");
  817. }
  818. else
  819. {
  820. ManaDebug.Log("3 fail");
  821. }
  822. }
  823. }