PreDownloadManager.cs 5.6 KB

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