PreloadManager.cs 4.9 KB

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