PreDownloadManager.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. //负责提供获得卡牌和衣服部件时开启后台预下载的接口
  13. public class PreDownloadManager : SingletonMonoBase<PreDownloadManager>
  14. {
  15. private List<string> waitList = new List<string>();
  16. private ResourceDownloaderOperation downloaderOperation;
  17. private string[] locationsLoading;
  18. public void TryAdd(string location)
  19. {
  20. if (LoadManager.Instance.CheckResExsited(location)) return;
  21. if (!YooAssets.CheckResExist(location)) return;
  22. waitList.Add(location);
  23. LogUtil.LogEditor($"PreloadManager TryAdd {location}");
  24. }
  25. /// <summary>
  26. /// 预加载套装资源
  27. /// </summary>
  28. /// <param name="suitId"></param>
  29. /// <param name="resType">可以选择预加载静态图或者动画或者全部</param>
  30. /// <param name="excludeType">排除一些类型,由ConstDressUpItemType定义</param>
  31. /// <param name="includeOptional">是否包含可选部件</param>
  32. public void PreDownloadSuitRes(int suitId, ResType resType, int[] excludeType, bool includeOptional)
  33. {
  34. SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitId);
  35. if (suitCfg == null)
  36. {
  37. return;
  38. }
  39. //找到要穿的散件
  40. List<int> targetItemList = DressUpUtil.GetSuitItems(suitId, resType != ResType.Sprite, excludeType, includeOptional, false);
  41. foreach (var itemId in targetItemList)
  42. {
  43. PreDownloadDressUpRes(itemId, resType);
  44. }
  45. if (resType != ResType.Sprite)
  46. {
  47. if (!string.IsNullOrEmpty(suitCfg.aniRes))
  48. {
  49. string assetPath = ResPathUtil.GetDressUpAnimationPath(suitCfg.aniRes);
  50. PreDownloadManager.Instance.TryAdd(assetPath);
  51. assetPath = ResPathUtil.GetDressUpEffectPath(suitCfg.aniRes, true);
  52. PreDownloadManager.Instance.TryAdd(assetPath);
  53. }
  54. }
  55. }
  56. public void PreDownloadDressUpRes(int itemId, ResType resType = ResType.Sprite)
  57. {
  58. ItemCfg dressUpCfg = ItemCfgArray.Instance.GetCfg(itemId);
  59. string assetPath;
  60. if (resType != ResType.Animation)
  61. {
  62. assetPath = ResPathUtil.GetDressUpLayerSpriteResPath(dressUpCfg, 1);
  63. PreDownloadManager.Instance.TryAdd(assetPath);
  64. assetPath = ResPathUtil.GetDressUpLayerSpriteResPath(dressUpCfg, 2);
  65. PreDownloadManager.Instance.TryAdd(assetPath);
  66. assetPath = ResPathUtil.GetDressUpLayerSpriteResPath(dressUpCfg, 3);
  67. PreDownloadManager.Instance.TryAdd(assetPath);
  68. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 1, false);
  69. PreDownloadManager.Instance.TryAdd(assetPath);
  70. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 2, false);
  71. PreDownloadManager.Instance.TryAdd(assetPath);
  72. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 3, false);
  73. PreDownloadManager.Instance.TryAdd(assetPath);
  74. }
  75. if (resType != ResType.Sprite)
  76. {
  77. assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 1);
  78. PreDownloadManager.Instance.TryAdd(assetPath);
  79. assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 2);
  80. PreDownloadManager.Instance.TryAdd(assetPath);
  81. assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 3);
  82. PreDownloadManager.Instance.TryAdd(assetPath);
  83. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 1, true);
  84. PreDownloadManager.Instance.TryAdd(assetPath);
  85. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 2, true);
  86. PreDownloadManager.Instance.TryAdd(assetPath);
  87. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 3, true);
  88. PreDownloadManager.Instance.TryAdd(assetPath);
  89. }
  90. }
  91. public void PreDownloadCardAnimationRes(int itemId)
  92. {
  93. ItemCfg cardCfg = ItemCfgArray.Instance.GetCfg(itemId);
  94. string assetPath = ResPathUtil.GetCardAnimationPath(cardCfg.res);
  95. PreDownloadManager.Instance.TryAdd(assetPath);
  96. }
  97. private void Update()
  98. {
  99. if(downloaderOperation != null)
  100. {
  101. if(downloaderOperation.IsDone)
  102. {
  103. foreach(var t in locationsLoading)
  104. {
  105. LoadManager.Instance.SetResDownloaded(t);
  106. }
  107. downloaderOperation = null;
  108. locationsLoading = null;
  109. }
  110. }
  111. if(downloaderOperation == null && waitList.Count > 0)
  112. {
  113. waitList.Reverse();//看了yooasset源码,疑似他是从后往前加载,这里翻转一下
  114. locationsLoading = waitList.ToArray();
  115. downloaderOperation = YooAssets.CreateBundleDownloader(locationsLoading, 1, 3);
  116. downloaderOperation.BeginDownload();
  117. waitList.Clear();
  118. }
  119. }
  120. }
  121. }