PreloadManager.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using GFGGame.Launcher;
  2. using System.Collections.Generic;
  3. using YooAsset;
  4. namespace GFGGame
  5. {
  6. public enum ResType
  7. {
  8. Sprite,
  9. Animation,
  10. Both
  11. }
  12. public class PreloadManager : SingletonMonoBase<PreloadManager>
  13. {
  14. private List<string> waitList = new List<string>();
  15. private ResourceDownloaderOperation downloaderOperation;
  16. private string[] locationsLoading;
  17. public void TryAdd(string location)
  18. {
  19. if (LoadManager.Instance.CheckResExsited(location)) return;
  20. if (!YooAssets.CheckResExist(location)) return;
  21. waitList.Add(location);
  22. LogUtil.LogEditor($"PreloadManager TryAdd {location}");
  23. }
  24. public void AddSuit(int suitId, ResType resType, int[] excludeType, bool includeOptional)
  25. {
  26. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitId);
  27. if (suitCfg == null)
  28. {
  29. return;
  30. }
  31. //找到要穿的散件
  32. List<int> targetItemList = DressUpUtil.GetSuitItems(suitId, resType != ResType.Sprite, excludeType, includeOptional, false);
  33. foreach (var itemId in targetItemList)
  34. {
  35. PreloadDressUpRes(itemId, resType);
  36. }
  37. if (resType != ResType.Sprite)
  38. {
  39. if (!string.IsNullOrEmpty(suitCfg.aniRes))
  40. {
  41. string assetPath = ResPathUtil.GetDressUpAnimationPath(suitCfg.aniRes);
  42. PreloadManager.Instance.TryAdd(assetPath);
  43. assetPath = ResPathUtil.GetDressUpEffectPath(suitCfg.aniRes);
  44. PreloadManager.Instance.TryAdd(assetPath);
  45. }
  46. }
  47. }
  48. public void PreloadDressUpRes(int itemId, ResType resType = ResType.Sprite)
  49. {
  50. ItemCfg dressUpCfg = ItemCfgArray.Instance.GetCfg(itemId);
  51. string assetPath;
  52. if (resType != ResType.Animation)
  53. {
  54. assetPath = ResPathUtil.GetDressUpLayerSpriteResPath(dressUpCfg, 1);
  55. PreloadManager.Instance.TryAdd(assetPath);
  56. assetPath = ResPathUtil.GetDressUpLayerSpriteResPath(dressUpCfg, 2);
  57. PreloadManager.Instance.TryAdd(assetPath);
  58. assetPath = ResPathUtil.GetDressUpLayerSpriteResPath(dressUpCfg, 3);
  59. PreloadManager.Instance.TryAdd(assetPath);
  60. }
  61. if (resType != ResType.Sprite)
  62. {
  63. assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 1);
  64. PreloadManager.Instance.TryAdd(assetPath);
  65. assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 2);
  66. PreloadManager.Instance.TryAdd(assetPath);
  67. assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 3);
  68. PreloadManager.Instance.TryAdd(assetPath);
  69. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 1);
  70. PreloadManager.Instance.TryAdd(assetPath);
  71. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 2);
  72. PreloadManager.Instance.TryAdd(assetPath);
  73. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 3);
  74. PreloadManager.Instance.TryAdd(assetPath);
  75. }
  76. }
  77. public void PreloadCardAnimationRes(int itemId)
  78. {
  79. ItemCfg cardCfg = ItemCfgArray.Instance.GetCfg(itemId);
  80. string assetPath = ResPathUtil.GetCardAnimationPath(cardCfg.res);
  81. PreloadManager.Instance.TryAdd(assetPath);
  82. }
  83. private void Update()
  84. {
  85. if(downloaderOperation != null)
  86. {
  87. if(downloaderOperation.IsDone)
  88. {
  89. foreach(var t in locationsLoading)
  90. {
  91. LoadManager.Instance.SetResDownloaded(t);
  92. }
  93. downloaderOperation = null;
  94. locationsLoading = null;
  95. }
  96. }
  97. if(downloaderOperation == null && waitList.Count > 0)
  98. {
  99. waitList.Reverse();//看了yooasset源码,疑似他是从后往前加载,这里翻转一下
  100. locationsLoading = waitList.ToArray();
  101. downloaderOperation = YooAssets.CreateBundleDownloader(locationsLoading, 1, 3);
  102. downloaderOperation.BeginDownload();
  103. waitList.Clear();
  104. }
  105. }
  106. }
  107. }