ResourceManager.cs 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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. if (traList.Count > 0)
  237. {
  238. Transform tra = traList[0];
  239. tra.SetActive(true);
  240. traList.RemoveAt(0);
  241. return tra.transform;
  242. }
  243. }
  244. return null;
  245. }
  246. #endregion
  247. #region ShortCut
  248. public static Text SetText(string goName)
  249. {
  250. Text text = Get<Text>(goName);
  251. text.text = Language.GetStr(LanguageLabel.CombineLanguageLabel(LanguageLabel.UI, goName));
  252. return text;
  253. }
  254. public static Text SetText(string goName, string str)
  255. {
  256. Text text = Get<Text>(goName);
  257. text.text = str;
  258. return text;
  259. }
  260. public static Image SetSprite(string goName, Sprite sprite)
  261. {
  262. Image image = Get<Image>(goName);
  263. image.sprite = sprite;
  264. return image;
  265. }
  266. public static Transform SetActive(string goName, bool active)
  267. {
  268. Transform tra = Get(goName);
  269. tra.SetActive(active);
  270. return tra;
  271. }
  272. public static Button SetButtonEvent(string goName, UnityAction onClick)
  273. {
  274. Button button = Get<Button>(goName);
  275. button.onClick = new Button.ButtonClickedEvent();
  276. button.onClick.AddListener(onClick);
  277. return button;
  278. }
  279. public static Button AddButtonEvent(string goName, UnityAction onClick)
  280. {
  281. Button button = Get<Button>(goName);
  282. button.onClick.AddListener(onClick);
  283. return button;
  284. }
  285. public static Button PushButtonEvent(string goName, UnityAction onClick)
  286. {
  287. Button button = Get<Button>(goName);
  288. Button.ButtonClickedEvent click = button.onClick;
  289. button.onClick = new Button.ButtonClickedEvent();
  290. button.onClick.AddListener(onClick);
  291. button.onClick.AddListener(click.Invoke);
  292. return button;
  293. }
  294. public static Button AddButtonEventOnetime(string goName, UnityAction onClick)
  295. {
  296. Button button = Get<Button>(goName);
  297. onClick += () =>
  298. {
  299. button.onClick.RemoveListener(onClick);
  300. };
  301. button.onClick.AddListener(onClick);
  302. return button;
  303. }
  304. public static Button PushButtonEventOnetime(string goName, UnityAction onClick)
  305. {
  306. Button button = Get<Button>(goName);
  307. onClick += () =>
  308. {
  309. button.onClick.RemoveListener(onClick);
  310. };
  311. Button.ButtonClickedEvent click = button.onClick;
  312. button.onClick = new Button.ButtonClickedEvent();
  313. button.onClick.AddListener(onClick);
  314. button.onClick.AddListener(click.Invoke);
  315. return button;
  316. }
  317. #endregion
  318. public static T Load<T>(string goName, Folder folder, ObjType objType = ObjType.Null) where T : Object
  319. {
  320. Object obj;
  321. if (ObjectDictionary.TryGetValue(goName, out obj))
  322. {
  323. if (objType != ObjType.Null)
  324. {
  325. ObjectPool.UniqueAdd(objType, new List<Transform>());
  326. }
  327. return (T) obj;
  328. }
  329. else
  330. {
  331. T t = Bundle.Load<T>(goName, folder);
  332. if (t == null)
  333. {
  334. throw new Exception(goName + " " + folder);
  335. }
  336. ObjectDictionary.Add(goName, t);
  337. if (objType != ObjType.Null)
  338. {
  339. ObjectPool.UniqueAdd(objType, new List<Transform>());
  340. }
  341. return t;
  342. }
  343. }
  344. public static Sprite LoadSprite(string goName, Folder folder)
  345. {
  346. if (SpriteDictionary.ContainsKey(goName))
  347. {
  348. return SpriteDictionary[goName];
  349. }
  350. else
  351. {
  352. return Load<Sprite>(goName, folder);
  353. }
  354. }
  355. public static Transform Get(string goName, Folder folder, bool compile, Transform par, bool worldSpace, ObjType objType = ObjType.Null, Type type = null)
  356. {
  357. Transform tra = Get(objType);
  358. if (tra == null)
  359. {
  360. GameObject go = Load<GameObject>(goName, folder, objType);
  361. go = Instantiate(go, par, worldSpace);
  362. go.name = go.name.Replace(UnityDefaultSuffix, "");
  363. if (compile)
  364. {
  365. Auxiliary.CompileDic(go.transform, TransformDictionary);
  366. }
  367. if (objType != ObjType.Null)
  368. {
  369. ObjectPoolDictionary.Add(go, objType);
  370. }
  371. if (type != null)
  372. {
  373. go.AddComponent(type);
  374. }
  375. return go.transform;
  376. }
  377. else
  378. {
  379. if (compile)
  380. {
  381. Auxiliary.CompileDic(tra, TransformDictionary);
  382. }
  383. GameObject prefab = Load<GameObject>(goName, folder, objType);
  384. tra.SetParent(par);
  385. if (worldSpace)
  386. {
  387. tra.position = prefab.transform.position;
  388. }
  389. else
  390. {
  391. tra.localPosition = prefab.transform.position;
  392. }
  393. if (type != null)
  394. {
  395. if (tra.GetComponent(type) == null)
  396. {
  397. tra.AddComponent(type);
  398. }
  399. }
  400. return tra;
  401. }
  402. }
  403. public static Transform Get(string goName, Folder folder, bool compile, Transform par, Vector3 pos, ObjType objType = ObjType.Null, Type type = null)
  404. {
  405. Transform tra = Get(objType);
  406. if (tra == null)
  407. {
  408. GameObject go = Load<GameObject>(goName, folder, objType);
  409. go = Instantiate(go, pos, Quaternion.identity, par);
  410. go.name = go.name.Replace(UnityDefaultSuffix, "");
  411. if (compile)
  412. {
  413. Auxiliary.CompileDic(go.transform, TransformDictionary);
  414. }
  415. if (objType != ObjType.Null)
  416. {
  417. ObjectPoolDictionary.Add(go, objType);
  418. }
  419. if (type != null)
  420. {
  421. go.AddComponent(type);
  422. }
  423. return go.transform;
  424. }
  425. else
  426. {
  427. if (compile)
  428. {
  429. Auxiliary.CompileDic(tra, TransformDictionary);
  430. }
  431. tra.SetParent(par);
  432. tra.position = pos;
  433. if (type != null)
  434. {
  435. if (tra.GetComponent(type) == null)
  436. {
  437. tra.AddComponent(type);
  438. }
  439. }
  440. return tra;
  441. }
  442. }
  443. private static float StarMinOffset = -0.0001f;
  444. private static float StarMaxOffset = -0.001f;
  445. public static Star GetStar()
  446. {
  447. int slotIndex = Random.Range(0, GardenManager.TotalSlotInOnePage);
  448. bool forceLeft = slotIndex == GardenManager.RightTopSlotIndex || slotIndex == GardenManager.RightDownSlotIndex;
  449. bool forceRight = slotIndex == GardenManager.LeftTopSlotIndex || slotIndex == GardenManager.LeftDownSlotIndex;
  450. slotIndex += Garden.CurrentPage* GardenManager.TotalSlotInOnePage;
  451. Slot slot = GardenManager.SlotList[slotIndex];
  452. Vector3 pos = slot.transform.position;
  453. Transform tra = Get(ResourceLabel.Star, Folder.Scene, false, null, pos, ObjType.Star);
  454. Star star = tra.GetComponent<Star>();
  455. tra.parent = slot.transform;
  456. tra.SetLZ(-0.3f + Random.Range(StarMinOffset, StarMaxOffset));
  457. if (star == null)
  458. {
  459. star = tra.AddComponent<Star>();
  460. star.Init(pos.y, forceLeft, forceRight);
  461. }
  462. else
  463. {
  464. star.Init(pos.y, forceLeft, forceRight);
  465. }
  466. return star;
  467. }
  468. public static ADChest GetADChest()
  469. {
  470. Transform tra = Get(ResourceLabel.ADChest, Folder.Scene, false, Get(GardenLabel.SlotPage), true, ObjType.ADChest);
  471. ADChest chest = tra.GetComponentInChildren<ADChest>(true);
  472. if (chest == null)
  473. {
  474. chest = tra.GetChild(0).AddComponent<ADChest>();
  475. chest.Init(tra.position.y, false, false);
  476. }
  477. else
  478. {
  479. chest.Init(tra.position.y, false, false);
  480. }
  481. return chest;
  482. }
  483. public static PlazaRoomChest GetPlazaRoomChest(Vector3 position)
  484. {
  485. Transform tra = Get(ResourceLabel.PlazaRoomChest, Folder.Scene, false, Get(ResourceLabel.PlazaRoom), position, ObjType.PlazaRoomChest);
  486. PlazaRoomChest chest = tra.GetComponentInChildren<PlazaRoomChest>(true);
  487. if (chest == null)
  488. {
  489. chest = tra.GetChild(0).AddComponent<PlazaRoomChest>();
  490. }
  491. return chest;
  492. }
  493. public static Drop GetDrop(ObjType objType)
  494. {
  495. AudioManager.PlayClip(AudioLabel.Minigame_DropAward);
  496. Vector3 leftPos = Get(GardenLabel.Minigame_DropAward_LeftBorder).position;
  497. Vector3 rightPos;
  498. if (MiniGameManager.miniGameType == MiniGameType.Memory)
  499. {
  500. rightPos = Get(GardenLabel.MemoryMinigame_DropAward_RightBorder).position;
  501. }
  502. else
  503. {
  504. rightPos = Get(GardenLabel.FindMinigame_DropAward_RightBorder).position;
  505. }
  506. Vector3 pos = Vector3.Lerp(leftPos, rightPos, Random.Range(0, 1f));
  507. Transform tra;
  508. if (objType == ObjType.DropGold)
  509. {
  510. tra = Get(ResourceLabel.DropGold, Folder.Scene, false, null, pos, objType, typeof(DropGold));
  511. }
  512. else if(objType == ObjType.DropDiamond)
  513. {
  514. tra = Get(ResourceLabel.DropDiamond, Folder.Scene, false, null, pos, objType, typeof(DropDiamond));
  515. }
  516. else
  517. {
  518. throw new Exception();
  519. }
  520. Drop drop = tra.GetComponent<Drop>();
  521. drop.InitAtOnce();
  522. drop.ResetStatus();
  523. return drop;
  524. }
  525. public static Flower GetFlower(FlowerInfo flowerInfo, Slot slot, bool collider)
  526. {
  527. Transform tra = Get(ResourceLabel.Flower, Folder.Scene, false, slot.transform, false, ObjType.Flower, typeof(Flower));
  528. tra.localScale = new Vector3(1, 1, 1);
  529. Flower flower = tra.GetComponent<Flower>();
  530. flower.InitAtOnce();
  531. flower.FlowerInfo = flowerInfo;
  532. flower.Slot = slot;
  533. flower.SetCollider(collider);
  534. return flower;
  535. }
  536. public static Flower GetFlower(FlowerInfo flowerInfo, Transform par)
  537. {
  538. Transform tra = Get(ResourceLabel.Flower, Folder.Scene, false, par, false, ObjType.Flower, typeof(Flower));
  539. tra.localScale = new Vector3(1, 1, 1);
  540. Flower flower = tra.GetComponent<Flower>();
  541. flower.InitAtOnce();
  542. flower.FlowerInfo = flowerInfo;
  543. flower.SetCollider(false);
  544. return flower;
  545. }
  546. public static TextPlus GetInfoItem(string infoItemName, Transform parent, ObjType objType)
  547. {
  548. Transform tra = Get(infoItemName, Folder.UI, false, parent, false, objType);
  549. TextPlus text = tra.GetComponent<TextPlus>();
  550. TweenRoot tween = text.CreateTweenGra(0, 1, 0.25f, true, true, Curve.EaseOutQuad);
  551. tween.OnBackwardFinish = () =>
  552. {
  553. Save(text);
  554. };
  555. return text;
  556. }
  557. 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)
  558. {
  559. Vector3 pos;
  560. if (scene)
  561. {
  562. pos = Camera.main.WorldToScreenPoint(posTra.position);
  563. }
  564. else
  565. {
  566. pos = posTra.position;
  567. }
  568. Transform tra = Get(ResourceLabel.HudText, Folder.UI, false, parTra, pos, ObjType.HudText, typeof(HudText));
  569. HudText hudText = tra.GetComponent<HudText>();
  570. hudText.Show(str, color, size, speed, time, stay);
  571. return hudText;
  572. }
  573. public static Ranktem GetRanktem(string rank, string praise, string serialNumber)
  574. {
  575. Transform tra = Get(ResourceLabel.RankItem, Folder.UI, false, Get(CanvasLabel.S_Grid), false, ObjType.RankItem, typeof(Ranktem));
  576. Ranktem ranktem = tra.GetComponent<Ranktem>();
  577. ranktem.InitAtOnce();
  578. ranktem.Reset(rank, praise, serialNumber);
  579. return ranktem;
  580. }
  581. public static CommentItem GetCommentItem(string nickname, string serialNumber, string content)
  582. {
  583. Transform tra = Get(ResourceLabel.CommentItem, Folder.UI, false, Get(CanvasLabel.Q_Grid), false, ObjType.CommentItem, typeof(CommentItem));
  584. CommentItem commentItem = tra.GetComponent<CommentItem>();
  585. commentItem.InitAtOnce();
  586. AccountData accountData = new AccountData(nickname, serialNumber);
  587. commentItem.Reset(accountData, content);
  588. return commentItem;
  589. }
  590. public static ShowComment GetShowComment(ShowCommentData data)
  591. {
  592. Transform tra = Get(ResourceLabel.ShowComment, Folder.UI, false, Get(CanvasLabel.Ph_Grid), false,
  593. ObjType.ShowComment, typeof(ShowComment));
  594. ShowComment comment = tra.GetComponent<ShowComment>();
  595. comment.InitAtOnce();
  596. comment.Reset(data.accountData, data.content);
  597. return comment;
  598. }
  599. private static char ElfSeperator = '_';
  600. private static float ElfMinOffset = 0.001f;
  601. private static float ElfMaxOffset = 0.1f;
  602. public static Transform GetElf(Flower flower, Vector4 offset, ElfType elfType)
  603. {
  604. Transform tra;
  605. Bounds bounds = flower.FlowerIcon.bounds;
  606. Vector3 pos = flower.FlowerIcon.transform.position + new Vector3(0, 0, -5f);
  607. Transform par = flower.transform;
  608. ObjType obj = elfType.ToString().Split(ElfSeperator)[0].ToEnum<ObjType>();
  609. pos.z -= Random.Range(ElfMinOffset, ElfMaxOffset);
  610. tra = Get(obj.ToString(), Folder.Scene, false, par, pos, obj);
  611. Elf elf = tra.GetChild(0).GetComponent<Elf>();
  612. if (elf == null)
  613. {
  614. elf = tra.GetChild(0).AddScript<Elf>();
  615. }
  616. tra.GetComponentInChildren<SpriteRenderer>().sprite = LoadSprite(elfType.ToString(), Folder.Atlas2);
  617. if (Random.Range(0f, 1f) <= 0.5f)
  618. {
  619. tra.SetEY(180);
  620. }
  621. else
  622. {
  623. tra.SetEY(0);
  624. }
  625. float offsetX = Mathf.Lerp(offset.x*bounds.extents.x, offset.y*bounds.extents.x, Random.Range(0f, 1f));
  626. float offsetY = Mathf.Lerp(offset.z, offset.w*bounds.extents.y, Random.Range(0f, 1f));
  627. tra.position += new Vector3(offsetX, offsetY, 0);
  628. elf.ParentFlower = flower;
  629. elf.Animator.SetTrigger("Play");
  630. return tra;
  631. }
  632. public static Transform GetSkillItem(SkillRoot skillRoot)
  633. {
  634. Transform tra;
  635. if (skillRoot.SkillTab == SkillTab.Elf)
  636. {
  637. tra = Get(ResourceLabel.SkillItem, Folder.UI, false, Get(CanvasLabel.Fd_Grid), false, ObjType.SkillItem);
  638. }
  639. else if (skillRoot.SkillTab == SkillTab.Store)
  640. {
  641. tra = Get(ResourceLabel.SkillItem, Folder.UI, false, Get(CanvasLabel.Fc_Grid), false, ObjType.SkillItem);
  642. }
  643. else if (skillRoot.SkillTab == SkillTab.Magic)
  644. {
  645. tra = Get(ResourceLabel.SkillItem, Folder.UI, false, Get(CanvasLabel.Fb_Grid), false, ObjType.SkillItem);
  646. }
  647. else if (skillRoot.SkillTab == SkillTab.Garden)
  648. {
  649. tra = Get(ResourceLabel.SkillItem, Folder.UI, false, Get(CanvasLabel.Fa_Grid), false, ObjType.SkillItem);
  650. }
  651. else
  652. {
  653. throw new Exception();
  654. }
  655. skillRoot.SkillItem = tra;
  656. return tra;
  657. }
  658. public static Transform GetAchieveItem()
  659. {
  660. Transform tra = Get(ResourceLabel.AchieveItem, Folder.UI, false, Get(CanvasLabel.M_Grid), false, ObjType.AchieveItem);
  661. return tra;
  662. }
  663. public static ParticleSystem GetFirework(Vector3 pos)
  664. {
  665. Transform tra = Get(ResourceLabel.Firework, Folder.Effect, false, null, pos, ObjType.Firework, typeof(Effect));
  666. ParticleSystem particle = tra.GetComponent<ParticleSystem>();
  667. particle.Play();
  668. return particle;
  669. }
  670. public static ParticleSystem GetLightwall()
  671. {
  672. Transform tra = Get(ResourceLabel.LightwallUI, Folder.Effect, false, Get(CanvasLabel.Canvas), false, ObjType.LightwallUI, typeof(Effect));
  673. ParticleSystem particle = tra.GetComponent<ParticleSystem>();
  674. if(particle != null)
  675. particle.Play();
  676. return particle;
  677. }
  678. public static void AsyncLoad<T>(string goName, Folder folder, UnityAction callback = null)
  679. {
  680. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  681. AssetBundleRequest bundleRequest = Bundle.LoadAsync<T>(goName, folder);
  682. AsyncRequest asyncRequest = new AsyncRequest();
  683. asyncRequest.Request = bundleRequest;
  684. asyncRequest.Callback = callback;
  685. kv.Key = asyncRequest;
  686. kv.Value = () =>
  687. {
  688. ObjectDictionary.UniqueAdd(goName, bundleRequest.asset);
  689. };
  690. RequestList.Add(bundleRequest);
  691. InstantiateList.Add(kv);
  692. }
  693. public static void AsyncLoad(string goName, int amt, Folder folder, ObjType objType, bool ui = false, bool canvas = false, UnityAction callback = null)
  694. {
  695. KV<AsyncRequest, UnityAction> kv = new KV<AsyncRequest, UnityAction>();
  696. AssetBundleRequest bundleRequest = Bundle.LoadAsync<object>(goName, folder);
  697. AsyncRequest asyncRequest = new AsyncRequest();
  698. asyncRequest.Request = bundleRequest;
  699. asyncRequest.Callback = callback;
  700. kv.Key = asyncRequest;
  701. kv.Value = () =>
  702. {
  703. ObjectDictionary.UniqueAdd(goName, bundleRequest.asset);
  704. if (objType != ObjType.Null)
  705. {
  706. ObjectPool.UniqueAdd(objType, new List<Transform>());
  707. }
  708. GameObject go;
  709. if (ui)
  710. {
  711. go = (GameObject)Instantiate(bundleRequest.asset, GameObject.Find(LogoSceneLabel.ManagerGame).transform.GetChild(0).GetChild(0));
  712. }
  713. else
  714. {
  715. go = (GameObject)Instantiate(bundleRequest.asset);
  716. }
  717. ObjectPoolDictionary.Add(go, objType);
  718. go.name = go.name.Replace(UnityDefaultSuffix, "");
  719. Save(go);
  720. if (ui)
  721. {
  722. go.SetParent(GameObject.Find(LogoSceneLabel.ManagerGame).transform.GetChild(0).GetChild(0));
  723. }
  724. };
  725. RequestList.Add(bundleRequest);
  726. for (int i = 0; i < amt; i++)
  727. {
  728. InstantiateList.Add(kv);
  729. }
  730. }
  731. public static void AddAsyncLoad<T>(string goName, Folder folder, UnityAction callback = null)
  732. {
  733. AsyncList.Add
  734. (
  735. () =>
  736. {
  737. AsyncLoad<T>(goName, folder, callback);
  738. }
  739. );
  740. }
  741. public static void AddAsyncLoad(string goName, int amt, Folder folder, ObjType objType, bool ui = false, bool canvas = false, UnityAction callback = null)
  742. {
  743. AsyncList.Add
  744. (
  745. () =>
  746. {
  747. AsyncLoad(goName, amt, folder, objType, ui, canvas, callback);
  748. }
  749. );
  750. }
  751. public void StopAllAsync()
  752. {
  753. StopCoroutine(AsyncLoadRoutine);
  754. StopCoroutine(InstantiateRoutine);
  755. }
  756. public static IEnumerator IAsyncLoad()
  757. {
  758. while (true)
  759. {
  760. if (RequestList.Count > 0)
  761. {
  762. yield return null;
  763. }
  764. else
  765. {
  766. if (AsyncList.Valid() && !AsyncLoadFlag)
  767. {
  768. AsyncLoadFlag = true;
  769. Auxiliary.Instance.DelayCall
  770. (
  771. () =>
  772. {
  773. AsyncLoadFlag = false;
  774. AsyncList[0].SafeInvoke();
  775. AsyncList.RemoveAt(0);
  776. },
  777. 1
  778. );
  779. yield return null;
  780. }
  781. else
  782. {
  783. yield return null;
  784. }
  785. }
  786. }
  787. }
  788. public static IEnumerator IAsyncInstancitate()
  789. {
  790. while (true)
  791. {
  792. if (AsyncInstantiateFlag)
  793. {
  794. yield return null;
  795. }
  796. if (!InstantiateList.Valid())
  797. {
  798. yield return null;
  799. }
  800. else
  801. {
  802. if (Time.deltaTime > 0.333f)
  803. {
  804. yield return null;
  805. }
  806. if (!InstantiateList[0].Key.Request.isDone)
  807. {
  808. yield return null;
  809. }
  810. else
  811. {
  812. InstantiateList[0].Key.Callback.SafeInvoke();
  813. RequestList.Remove(InstantiateList[0].Key.Request);
  814. UnityAction action = InstantiateList[0].Value;
  815. InstantiateList.RemoveAt(0);
  816. AsyncInstantiateFlag = true;
  817. Auxiliary.Instance.DelayCall
  818. (
  819. () =>
  820. {
  821. if (!Logo.Complete)
  822. {
  823. action.SafeInvoke();
  824. AsyncInstantiateFlag = false;
  825. }
  826. },
  827. 1
  828. );
  829. yield return null;
  830. }
  831. }
  832. }
  833. }
  834. }