ResourceManager.cs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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 Object = UnityEngine.Object;
  10. using Random = UnityEngine.Random;
  11. using Component = UnityEngine.Component;
  12. public enum Folder
  13. {
  14. Default,
  15. UI,
  16. Atlas,
  17. Atlas2,
  18. Effect,
  19. Audio,
  20. Scene,
  21. Config,
  22. Discard,
  23. PlazaRoom,
  24. }
  25. public enum ObjType
  26. {
  27. Null,
  28. Firework,
  29. LightwallUI,
  30. Player,
  31. SlotPage,
  32. Flower,
  33. Garden,
  34. Tutorial,
  35. CloseItem,
  36. DressRoom,
  37. Canvas,
  38. EventSystem,
  39. MainCamera,
  40. ChestTimer,
  41. NickName,
  42. MessageBox,
  43. HudText,
  44. MailItem,
  45. ApplicantItem,
  46. GardenInfoItem,
  47. PrivateMessageItem,
  48. PlazaroomMemberItem,
  49. PlazaroomInfoItem,
  50. FriendItem,
  51. SkillItem,
  52. SignItem,
  53. RankItem,
  54. PlazaRoomItem,
  55. FlowerItem,
  56. AchieveItem,
  57. CommentItem,
  58. ShowComment,
  59. FashionShowCloseItem,
  60. FashionShowCloseBox,
  61. Music,
  62. Star,
  63. FlyGold,
  64. ADChest,
  65. PlazaRoomChest,
  66. DropGold,
  67. DropDiamond,
  68. PlazaRoomStar,
  69. Bee,
  70. Beetle,
  71. Butterfly,
  72. Dragonfly,
  73. }
  74. public class AsyncRequest
  75. {
  76. public UnityAction Callback;
  77. public AssetBundleRequest Request;
  78. }
  79. public class ResourceManager : Regist
  80. {
  81. #region Config
  82. public static bool AsyncLoadFlag;
  83. public static bool AsyncInstantiateFlag;
  84. public static Coroutine AsyncLoadRoutine;
  85. public static Coroutine InstantiateRoutine;
  86. public static List<UnityAction> AsyncList = new List<UnityAction>();
  87. public static List<AssetBundleRequest> RequestList = new List<AssetBundleRequest>();
  88. public static List<KV<AsyncRequest, UnityAction>> InstantiateList = new List<KV<AsyncRequest, UnityAction>>();
  89. private static string UnityDefaultSuffix = "(Clone)";
  90. public static Dictionary<string, Sprite> SpriteDictionary = new Dictionary<string, Sprite>();
  91. public static Dictionary<string, Object> ObjectDictionary = new Dictionary<string, Object>();
  92. public static Dictionary<string, Transform> TransformDictionary = new Dictionary<string, Transform>();
  93. public static Dictionary<ObjType, List<Transform>> ObjectPool = new Dictionary<ObjType, List<Transform>>();
  94. public static Dictionary<GameObject, ObjType> ObjectPoolDictionary = new Dictionary<GameObject, ObjType>();
  95. public static ResourceManager Instance;
  96. #endregion
  97. public override bool InitAtOnce()
  98. {
  99. if (base.InitAtOnce())
  100. {
  101. return true;
  102. }
  103. Instance = this;
  104. Transform objPool = new GameObject(ResourceLabel.ObjPool).transform;
  105. objPool.parent = transform;
  106. objPool.SetActive(false);
  107. TransformDictionary.Add(objPool.name, objPool);
  108. AsyncLoadRoutine = StartCoroutine(IAsyncLoad());
  109. InstantiateRoutine = StartCoroutine(IAsyncInstancitate());
  110. return false;
  111. }
  112. #region TransformDictionary
  113. public static T Get<T>(string goName, bool warn = true)
  114. {
  115. Transform tra;
  116. if (TransformDictionary.TryGetValue(goName, out tra))
  117. {
  118. T t = tra.GetComponent<T>();
  119. if (t == null)
  120. {
  121. throw new Exception(goName);
  122. }
  123. return t;
  124. }
  125. else
  126. {
  127. if (warn)
  128. {
  129. throw new Exception(goName);
  130. }
  131. else
  132. {
  133. return default(T);
  134. }
  135. }
  136. }
  137. public static T[] Gets<T>(string goName, bool warn = true)
  138. {
  139. Transform tra;
  140. if (TransformDictionary.TryGetValue(goName, out tra))
  141. {
  142. T[] t = tra.GetComponentsInChildren<T>();
  143. if (t == null)
  144. {
  145. throw new Exception(goName);
  146. }
  147. return t;
  148. }
  149. else
  150. {
  151. if (warn)
  152. {
  153. throw new Exception(goName);
  154. }
  155. else
  156. {
  157. return default(T[]);
  158. }
  159. }
  160. }
  161. public static Transform Get(string goName, bool warn = true)
  162. {
  163. Transform tra;
  164. if (TransformDictionary.TryGetValue(goName, out tra))
  165. {
  166. return tra;
  167. }
  168. else
  169. {
  170. if (warn)
  171. {
  172. throw new Exception(goName);
  173. }
  174. else
  175. {
  176. return null;
  177. }
  178. }
  179. }
  180. #endregion
  181. #region ObjPool
  182. public static void Save<T>(T t, bool warn = false) where T : Component
  183. {
  184. Save(t.gameObject, warn);
  185. }
  186. public static void Save(GameObject go, bool warn = false)
  187. {
  188. ObjType objType;
  189. if (!ObjectPoolDictionary.TryGetValue(go, out objType))
  190. {
  191. throw new Exception();
  192. }
  193. List<Transform> traList;
  194. if (ObjectPool.TryGetValue(objType, out traList))
  195. {
  196. if (traList.Contains(go.transform))
  197. {
  198. if (warn)
  199. {
  200. return;
  201. }
  202. else
  203. {
  204. throw new Exception();
  205. }
  206. }
  207. go.SetActive(false);
  208. traList.Add(go.transform);
  209. go.transform.SetParent(Get(ResourceLabel.ObjPool));
  210. }
  211. else
  212. {
  213. throw new Exception();
  214. }
  215. }
  216. public static bool Contains<T>(T t) where T : Component
  217. {
  218. return Contains(t.gameObject);
  219. }
  220. public static bool Contains(GameObject go)
  221. {
  222. foreach (var kv in ObjectPool)
  223. {
  224. if (kv.Value.Contains(go.transform))
  225. {
  226. return true;
  227. }
  228. }
  229. return false;
  230. }
  231. public static Transform Get(ObjType objType)
  232. {
  233. List<Transform> traList;
  234. if (ObjectPool.TryGetValue(objType, out traList))
  235. {
  236. for (int i = 0; i < traList.Count; i++)
  237. {
  238. Transform tra = traList[i];
  239. if (tra == null)
  240. {
  241. traList.RemoveAt(i);
  242. i--;
  243. continue;
  244. }
  245. tra.SetActive(true);
  246. traList.RemoveAt(0);
  247. return tra.transform;
  248. }
  249. //if (traList.Count > 0)
  250. //{
  251. // Transform tra = traList[0];
  252. // tra.SetActive(true);
  253. // traList.RemoveAt(0);
  254. // return tra.transform;
  255. //}
  256. }
  257. return null;
  258. }
  259. #endregion
  260. #region ShortCut
  261. public static Text SetText(string goName)
  262. {
  263. Text text = Get<Text>(goName);
  264. text.text = Language.GetStr(LanguageLabel.CombineLanguageLabel(LanguageLabel.UI, goName));
  265. return text;
  266. }
  267. public static Text SetText(string goName, string str)
  268. {
  269. Text text = Get<Text>(goName);
  270. text.text = str;
  271. return text;
  272. }
  273. public static Image SetSprite(string goName, Sprite sprite)
  274. {
  275. Image image = Get<Image>(goName);
  276. image.sprite = sprite;
  277. return image;
  278. }
  279. public static Transform SetActive(string goName, bool active)
  280. {
  281. Transform tra = Get(goName);
  282. tra.SetActive(active);
  283. return tra;
  284. }
  285. public static Button SetButtonEvent(string goName, UnityAction onClick)
  286. {
  287. Button button = Get<Button>(goName);
  288. button.onClick = new Button.ButtonClickedEvent();
  289. button.onClick.AddListener(onClick);
  290. return button;
  291. }
  292. public static Button AddButtonEvent(string goName, UnityAction onClick)
  293. {
  294. Button button = Get<Button>(goName);
  295. button.onClick.AddListener(onClick);
  296. return button;
  297. }
  298. public static Button PushButtonEvent(string goName, UnityAction onClick)
  299. {
  300. Button button = Get<Button>(goName);
  301. Button.ButtonClickedEvent click = button.onClick;
  302. button.onClick = new Button.ButtonClickedEvent();
  303. button.onClick.AddListener(onClick);
  304. button.onClick.AddListener(click.Invoke);
  305. return button;
  306. }
  307. public static Button AddButtonEventOnetime(string goName, UnityAction onClick)
  308. {
  309. Button button = Get<Button>(goName);
  310. onClick += () =>
  311. {
  312. button.onClick.RemoveListener(onClick);
  313. };
  314. button.onClick.AddListener(onClick);
  315. return button;
  316. }
  317. public static Button PushButtonEventOnetime(string goName, UnityAction onClick)
  318. {
  319. Button button = Get<Button>(goName);
  320. onClick += () =>
  321. {
  322. button.onClick.RemoveListener(onClick);
  323. };
  324. Button.ButtonClickedEvent click = button.onClick;
  325. button.onClick = new Button.ButtonClickedEvent();
  326. button.onClick.AddListener(onClick);
  327. button.onClick.AddListener(click.Invoke);
  328. return button;
  329. }
  330. #endregion
  331. public static T Load<T>(string goName, Folder folder, ObjType objType = ObjType.Null) where T : Object
  332. {
  333. Object obj;
  334. if (ObjectDictionary.TryGetValue(goName, out obj))
  335. {
  336. if (objType != ObjType.Null)
  337. {
  338. ObjectPool.UniqueAdd(objType, new List<Transform>());
  339. }
  340. return (T) obj;
  341. }
  342. else
  343. {
  344. T t = Bundle.Load<T>(goName, folder);
  345. if (t == null)
  346. {
  347. throw new Exception(goName + " " + folder);
  348. }
  349. ObjectDictionary.Add(goName, t);
  350. if (objType != ObjType.Null)
  351. {
  352. ObjectPool.UniqueAdd(objType, new List<Transform>());
  353. }
  354. return t;
  355. }
  356. }
  357. public static Sprite LoadSprite(string goName, Folder folder)
  358. {
  359. if (SpriteDictionary.ContainsKey(goName))
  360. {
  361. return SpriteDictionary[goName];
  362. }
  363. else
  364. {
  365. return Load<Sprite>(goName, folder);
  366. }
  367. }
  368. public static Transform Get(string goName, Folder folder, bool compile, Transform par, bool worldSpace, ObjType objType = ObjType.Null, Type type = null)
  369. {
  370. Transform tra = Get(objType);
  371. if (tra == null)
  372. {
  373. GameObject go = Load<GameObject>(goName, folder, objType);
  374. go = Instantiate(go, par, worldSpace);
  375. go.name = go.name.Replace(UnityDefaultSuffix, "");
  376. if (compile)
  377. {
  378. Auxiliary.CompileDic(go.transform, TransformDictionary);
  379. }
  380. if (objType != ObjType.Null)
  381. {
  382. ObjectPoolDictionary.Add(go, objType);
  383. }
  384. if (type != null)
  385. {
  386. go.AddComponent(type);
  387. }
  388. return go.transform;
  389. }
  390. else
  391. {
  392. if (compile)
  393. {
  394. Auxiliary.CompileDic(tra, TransformDictionary);
  395. }
  396. GameObject prefab = Load<GameObject>(goName, folder, objType);
  397. tra.SetParent(par);
  398. if (worldSpace)
  399. {
  400. tra.position = prefab.transform.position;
  401. }
  402. else
  403. {
  404. tra.localPosition = prefab.transform.position;
  405. }
  406. if (type != null)
  407. {
  408. if (tra.GetComponent(type) == null)
  409. {
  410. tra.AddComponent(type);
  411. }
  412. }
  413. return tra;
  414. }
  415. }
  416. public static Transform Get(string goName, Folder folder, bool compile, Transform par, Vector3 pos, ObjType objType = ObjType.Null, Type type = null)
  417. {
  418. Transform tra = Get(objType);
  419. if (tra == null)
  420. {
  421. GameObject go = Load<GameObject>(goName, folder, objType);
  422. go = Instantiate(go, pos, Quaternion.identity, par);
  423. go.name = go.name.Replace(UnityDefaultSuffix, "");
  424. if (compile)
  425. {
  426. Auxiliary.CompileDic(go.transform, TransformDictionary);
  427. }
  428. if (objType != ObjType.Null)
  429. {
  430. ObjectPoolDictionary.Add(go, objType);
  431. }
  432. if (type != null)
  433. {
  434. go.AddComponent(type);
  435. }
  436. return go.transform;
  437. }
  438. else
  439. {
  440. if (compile)
  441. {
  442. Auxiliary.CompileDic(tra, TransformDictionary);
  443. }
  444. tra.SetParent(par);
  445. tra.position = pos;
  446. if (type != null)
  447. {
  448. if (tra.GetComponent(type) == null)
  449. {
  450. tra.AddComponent(type);
  451. }
  452. }
  453. return tra;
  454. }
  455. }
  456. private static float StarMinOffset = -0.0001f;
  457. private static float StarMaxOffset = -0.001f;
  458. public static Star GetStar()
  459. {
  460. int slotIndex = Random.Range(0, GardenManager.TotalSlotInOnePage);
  461. bool forceLeft = slotIndex == GardenManager.RightTopSlotIndex || slotIndex == GardenManager.RightDownSlotIndex;
  462. bool forceRight = slotIndex == GardenManager.LeftTopSlotIndex || slotIndex == GardenManager.LeftDownSlotIndex;
  463. slotIndex += Garden.CurrentPage* GardenManager.TotalSlotInOnePage;
  464. Slot slot = GardenManager.SlotList[slotIndex];
  465. Vector3 pos = slot.transform.position;
  466. Transform tra = Get(ResourceLabel.Star, Folder.Scene, false, null, pos, ObjType.Star);
  467. Star star = tra.GetComponent<Star>();
  468. tra.parent = slot.transform;
  469. tra.SetLZ(-0.3f + Random.Range(StarMinOffset, StarMaxOffset));
  470. if (star == null)
  471. {
  472. star = tra.AddComponent<Star>();
  473. star.Init(pos.y, forceLeft, forceRight);
  474. }
  475. else
  476. {
  477. star.Init(pos.y, forceLeft, forceRight);
  478. }
  479. return star;
  480. }
  481. public static ADChest GetADChest()
  482. {
  483. Transform tra = Get(ResourceLabel.ADChest, Folder.Scene, false, Get(GardenLabel.SlotPage), true, ObjType.ADChest);
  484. ADChest chest = tra.GetComponentInChildren<ADChest>(true);
  485. if (chest == null)
  486. {
  487. chest = tra.GetChild(0).AddComponent<ADChest>();
  488. chest.Init(tra.position.y, false, false);
  489. }
  490. else
  491. {
  492. chest.Init(tra.position.y, false, false);
  493. }
  494. return chest;
  495. }
  496. public static PlazaRoomChest GetPlazaRoomChest(Vector3 position)
  497. {
  498. Transform tra = Get(ResourceLabel.PlazaRoomChest, Folder.Scene, false, Get(ResourceLabel.PlazaRoom), position, ObjType.PlazaRoomChest);
  499. PlazaRoomChest chest = tra.GetComponentInChildren<PlazaRoomChest>(true);
  500. if (chest == null)
  501. {
  502. chest = tra.GetChild(0).AddComponent<PlazaRoomChest>();
  503. }
  504. return chest;
  505. }
  506. public static Drop GetDrop(ObjType objType)
  507. {
  508. AudioManager.PlayClip(AudioLabel.Minigame_DropAward);
  509. Vector3 leftPos = Get(GardenLabel.Minigame_DropAward_LeftBorder).position;
  510. Vector3 rightPos;
  511. if (MiniGameManager.miniGameType == MiniGameType.Memory)
  512. {
  513. rightPos = Get(GardenLabel.MemoryMinigame_DropAward_RightBorder).position;
  514. }
  515. else
  516. {
  517. rightPos = Get(GardenLabel.FindMinigame_DropAward_RightBorder).position;
  518. }
  519. Vector3 pos = Vector3.Lerp(leftPos, rightPos, Random.Range(0, 1f));
  520. Transform tra;
  521. if (objType == ObjType.DropGold)
  522. {
  523. tra = Get(ResourceLabel.DropGold, Folder.Scene, false, null, pos, objType, typeof(DropGold));
  524. }
  525. else if(objType == ObjType.DropDiamond)
  526. {
  527. tra = Get(ResourceLabel.DropDiamond, Folder.Scene, false, null, pos, objType, typeof(DropDiamond));
  528. }
  529. else
  530. {
  531. throw new Exception();
  532. }
  533. Drop drop = tra.GetComponent<Drop>();
  534. drop.InitAtOnce();
  535. drop.ResetStatus();
  536. return drop;
  537. }
  538. public static Flower GetFlower(FlowerInfo flowerInfo, Slot slot, bool collider)
  539. {
  540. Transform tra = Get(ResourceLabel.Flower, Folder.Scene, false, slot.transform, false, ObjType.Flower, typeof(Flower));
  541. tra.localScale = new Vector3(1, 1, 1);
  542. Flower flower = tra.GetComponent<Flower>();
  543. flower.InitAtOnce();
  544. flower.FlowerInfo = flowerInfo;
  545. flower.Slot = slot;
  546. flower.SetCollider(collider);
  547. return flower;
  548. }
  549. public static Flower GetFlower(FlowerInfo flowerInfo, Transform par)
  550. {
  551. Transform tra = Get(ResourceLabel.Flower, Folder.Scene, false, par, false, ObjType.Flower, typeof(Flower));
  552. tra.localScale = new Vector3(1, 1, 1);
  553. Flower flower = tra.GetComponent<Flower>();
  554. flower.InitAtOnce();
  555. flower.FlowerInfo = flowerInfo;
  556. flower.SetCollider(false);
  557. return flower;
  558. }
  559. public static TextPlus GetInfoItem(string infoItemName, Transform parent, ObjType objType)
  560. {
  561. Transform tra = Get(infoItemName, Folder.UI, false, parent, false, objType);
  562. TextPlus text = tra.GetComponent<TextPlus>();
  563. TweenRoot tween = text.CreateTweenGra(0, 1, 0.25f, true, true, Curve.EaseOutQuad);
  564. tween.OnBackwardFinish = () =>
  565. {
  566. Save(text);
  567. };
  568. return text;
  569. }
  570. public static HudText GetHudText(string str, Color color, int size, Transform posTra, Transform parTra, bool scene, float speed = 7.5f, float time = 0.5f, float stay = 0.5f)
  571. {
  572. Vector3 pos;
  573. if (scene)
  574. {
  575. pos = Camera.main.WorldToScreenPoint(posTra.position);
  576. }
  577. else
  578. {
  579. pos = posTra.position;
  580. }
  581. Transform tra = Get(ResourceLabel.HudText, Folder.UI, false, parTra, pos, ObjType.HudText, typeof(HudText));
  582. HudText hudText = tra.GetComponent<HudText>();
  583. hudText.Show(str, color, size, speed, time, stay);
  584. return hudText;
  585. }
  586. public static Ranktem GetRanktem(string rank, string praise, string serialNumber)
  587. {
  588. Transform tra = Get(ResourceLabel.RankItem, Folder.UI, false, Get(CanvasLabel.S_Grid), false, ObjType.RankItem, typeof(Ranktem));
  589. Ranktem ranktem = tra.GetComponent<Ranktem>();
  590. ranktem.InitAtOnce();
  591. ranktem.Reset(rank, praise, serialNumber);
  592. return ranktem;
  593. }
  594. public static CommentItem GetCommentItem(string nickname, string serialNumber, string content)
  595. {
  596. Transform tra = Get(ResourceLabel.CommentItem, Folder.UI, false, Get(CanvasLabel.Q_Grid), false, ObjType.CommentItem, typeof(CommentItem));
  597. CommentItem commentItem = tra.GetComponent<CommentItem>();
  598. commentItem.InitAtOnce();
  599. AccountData accountData = new AccountData(nickname, serialNumber);
  600. commentItem.Reset(accountData, content);
  601. return commentItem;
  602. }
  603. public static ShowComment GetShowComment(ShowCommentData data)
  604. {
  605. Transform tra = Get(ResourceLabel.CommentItem, Folder.UI, false, Get(CanvasLabel.Ph_Grid), false,
  606. ObjType.ShowComment, typeof(ShowComment));
  607. ShowComment comment = tra.GetComponent<ShowComment>();
  608. comment.InitAtOnce();
  609. comment.Reset(data.accountData, data.content);
  610. return comment;
  611. }
  612. private static char ElfSeperator = '_';
  613. private static float ElfMinOffset = 0.001f;
  614. private static float ElfMaxOffset = 0.1f;
  615. public static Transform GetElf(Flower flower, Vector4 offset, ElfType elfType)
  616. {
  617. Transform tra;
  618. Bounds bounds = flower.FlowerIcon.bounds;
  619. Vector3 pos = flower.FlowerIcon.transform.position + new Vector3(0, 0, -5f);
  620. Transform par = flower.transform;
  621. ObjType obj = elfType.ToString().Split(ElfSeperator)[0].ToEnum<ObjType>();
  622. pos.z -= Random.Range(ElfMinOffset, ElfMaxOffset);
  623. tra = Get(obj.ToString(), Folder.Scene, false, par, pos, obj);
  624. Elf elf = tra.GetChild(0).GetComponent<Elf>();
  625. if (elf == null)
  626. {
  627. elf = tra.GetChild(0).AddScript<Elf>();
  628. }
  629. tra.GetComponentInChildren<SpriteRenderer>().sprite = LoadSprite(elfType.ToString(), Folder.Atlas2);
  630. if (Random.Range(0f, 1f) <= 0.5f)
  631. {
  632. tra.SetEY(180);
  633. }
  634. else
  635. {
  636. tra.SetEY(0);
  637. }
  638. float offsetX = Mathf.Lerp(offset.x*bounds.extents.x, offset.y*bounds.extents.x, Random.Range(0f, 1f));
  639. float offsetY = Mathf.Lerp(offset.z, offset.w*bounds.extents.y, Random.Range(0f, 1f));
  640. tra.position += new Vector3(offsetX, offsetY, 0);
  641. elf.ParentFlower = flower;
  642. elf.Animator.SetTrigger("Play");
  643. return tra;
  644. }
  645. public static Transform GetSkillItem(SkillRoot skillRoot)
  646. {
  647. Transform tra;
  648. if (skillRoot.SkillTab == SkillTab.Elf)
  649. {
  650. tra = Get(ResourceLabel.SkillItem, Folder.UI, false, Get(CanvasLabel.Fd_Grid), false, ObjType.SkillItem);
  651. }
  652. else if (skillRoot.SkillTab == SkillTab.Store)
  653. {
  654. tra = Get(ResourceLabel.SkillItem, Folder.UI, false, Get(CanvasLabel.Fc_Grid), false, ObjType.SkillItem);
  655. }
  656. else if (skillRoot.SkillTab == SkillTab.Magic)
  657. {
  658. tra = Get(ResourceLabel.SkillItem, Folder.UI, false, Get(CanvasLabel.Fb_Grid), false, ObjType.SkillItem);
  659. }
  660. else if (skillRoot.SkillTab == SkillTab.Garden)
  661. {
  662. tra = Get(ResourceLabel.SkillItem, Folder.UI, false, Get(CanvasLabel.Fa_Grid), false, ObjType.SkillItem);
  663. }
  664. else
  665. {
  666. throw new Exception();
  667. }
  668. skillRoot.SkillItem = tra;
  669. return tra;
  670. }
  671. public static Transform GetAchieveItem()
  672. {
  673. Transform tra = Get(ResourceLabel.AchieveItem, Folder.UI, false, Get(CanvasLabel.M_Grid), false, ObjType.AchieveItem);
  674. return tra;
  675. }
  676. public static ParticleSystem GetFirework(Vector3 pos)
  677. {
  678. Transform tra = Get(ResourceLabel.Firework, Folder.Effect, false, null, pos, ObjType.Firework, typeof(Effect));
  679. ParticleSystem particle = tra.GetComponent<ParticleSystem>();
  680. particle.Play();
  681. return particle;
  682. }
  683. public static ParticleSystem GetLightwall()
  684. {
  685. Transform tra = Get(ResourceLabel.LightwallUI, Folder.Effect, false, Get(CanvasLabel.Canvas), false, ObjType.LightwallUI, typeof(Effect));
  686. ParticleSystem particle = tra.GetComponent<ParticleSystem>();
  687. if(particle != null)
  688. particle.Play();
  689. return particle;
  690. }
  691. public static void AsyncLoad<T>(string goName, Folder folder, UnityAction callback = null)
  692. {
  693. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  694. AssetBundleRequest bundleRequest = Bundle.LoadAsync<T>(goName, folder);
  695. AsyncRequest asyncRequest = new AsyncRequest();
  696. asyncRequest.Request = bundleRequest;
  697. asyncRequest.Callback = callback;
  698. kv.Key = asyncRequest;
  699. kv.Value = () =>
  700. {
  701. ObjectDictionary.UniqueAdd(goName, bundleRequest.asset);
  702. };
  703. RequestList.Add(bundleRequest);
  704. InstantiateList.Add(kv);
  705. }
  706. public static void AsyncLoad(string goName, int amt, Folder folder, ObjType objType, bool ui = false, bool canvas = false, UnityAction callback = null)
  707. {
  708. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  709. AssetBundleRequest bundleRequest = Bundle.LoadAsync<object>(goName, folder);
  710. AsyncRequest asyncRequest = new AsyncRequest();
  711. asyncRequest.Request = bundleRequest;
  712. asyncRequest.Callback = callback;
  713. kv.Key = asyncRequest;
  714. kv.Value = () =>
  715. {
  716. ObjectDictionary.UniqueAdd(goName, bundleRequest.asset);
  717. if (objType != ObjType.Null)
  718. {
  719. ObjectPool.UniqueAdd(objType, new List<Transform>());
  720. }
  721. GameObject go;
  722. if (ui)
  723. {
  724. go = (GameObject)Instantiate(bundleRequest.asset, GameObject.Find(LogoSceneLabel.ManagerGame).transform.GetChild(0).GetChild(0));
  725. }
  726. else
  727. {
  728. go = (GameObject)Instantiate(bundleRequest.asset);
  729. }
  730. ObjectPoolDictionary.Add(go, objType);
  731. go.name = go.name.Replace(UnityDefaultSuffix, "");
  732. Save(go);
  733. if (ui)
  734. {
  735. go.SetParent(GameObject.Find(LogoSceneLabel.ManagerGame).transform.GetChild(0).GetChild(0));
  736. }
  737. };
  738. RequestList.Add(bundleRequest);
  739. for (int i = 0; i < amt; i++)
  740. {
  741. InstantiateList.Add(kv);
  742. }
  743. }
  744. public static void AddAsyncLoad<T>(string goName, Folder folder, UnityAction callback = null)
  745. {
  746. AsyncList.Add
  747. (
  748. () =>
  749. {
  750. AsyncLoad<T>(goName, folder, callback);
  751. }
  752. );
  753. }
  754. public static void AddAsyncLoad(string goName, int amt, Folder folder, ObjType objType, bool ui = false, bool canvas = false, UnityAction callback = null)
  755. {
  756. AsyncList.Add
  757. (
  758. () =>
  759. {
  760. AsyncLoad(goName, amt, folder, objType, ui, canvas, callback);
  761. }
  762. );
  763. }
  764. public void StopAllAsync()
  765. {
  766. StopCoroutine(AsyncLoadRoutine);
  767. StopCoroutine(InstantiateRoutine);
  768. }
  769. public static IEnumerator IAsyncLoad()
  770. {
  771. while (true)
  772. {
  773. if (RequestList.Count > 0)
  774. {
  775. yield return null;
  776. }
  777. else
  778. {
  779. if (AsyncList.Valid() && !AsyncLoadFlag)
  780. {
  781. AsyncLoadFlag = true;
  782. Auxiliary.Instance.DelayCall
  783. (
  784. () =>
  785. {
  786. AsyncLoadFlag = false;
  787. AsyncList[0].SafeInvoke();
  788. AsyncList.RemoveAt(0);
  789. },
  790. 1
  791. );
  792. yield return null;
  793. }
  794. else
  795. {
  796. yield return null;
  797. }
  798. }
  799. }
  800. }
  801. public static IEnumerator IAsyncInstancitate()
  802. {
  803. while (true)
  804. {
  805. if (AsyncInstantiateFlag)
  806. {
  807. yield return null;
  808. }
  809. if (!InstantiateList.Valid())
  810. {
  811. yield return null;
  812. }
  813. else
  814. {
  815. if (Time.deltaTime > 0.333f)
  816. {
  817. yield return null;
  818. }
  819. if (!InstantiateList[0].Key.Request.isDone)
  820. {
  821. yield return null;
  822. }
  823. else
  824. {
  825. InstantiateList[0].Key.Callback.SafeInvoke();
  826. RequestList.Remove(InstantiateList[0].Key.Request);
  827. UnityAction action = InstantiateList[0].Value;
  828. InstantiateList.RemoveAt(0);
  829. AsyncInstantiateFlag = true;
  830. Auxiliary.Instance.DelayCall
  831. (
  832. () =>
  833. {
  834. if (!Logo.Complete)
  835. {
  836. action.SafeInvoke();
  837. AsyncInstantiateFlag = false;
  838. }
  839. },
  840. 1
  841. );
  842. yield return null;
  843. }
  844. }
  845. }
  846. }
  847. }