PreloadManager.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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, true);
  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. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 1, false);
  68. PreloadManager.Instance.TryAdd(assetPath);
  69. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 2, false);
  70. PreloadManager.Instance.TryAdd(assetPath);
  71. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 3, false);
  72. PreloadManager.Instance.TryAdd(assetPath);
  73. }
  74. if (resType != ResType.Sprite)
  75. {
  76. assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 1);
  77. PreloadManager.Instance.TryAdd(assetPath);
  78. assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 2);
  79. PreloadManager.Instance.TryAdd(assetPath);
  80. assetPath = ResPathUtil.GetDressUpLayerAnimationResPath(dressUpCfg, 3);
  81. PreloadManager.Instance.TryAdd(assetPath);
  82. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 1, true);
  83. PreloadManager.Instance.TryAdd(assetPath);
  84. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 2, true);
  85. PreloadManager.Instance.TryAdd(assetPath);
  86. assetPath = ResPathUtil.GetDressUpLayerEffectResPath(dressUpCfg, 3, true);
  87. PreloadManager.Instance.TryAdd(assetPath);
  88. }
  89. }
  90. public void PreloadCardAnimationRes(int itemId)
  91. {
  92. ItemCfg cardCfg = ItemCfgArray.Instance.GetCfg(itemId);
  93. string assetPath = ResPathUtil.GetCardAnimationPath(cardCfg.res);
  94. PreloadManager.Instance.TryAdd(assetPath);
  95. }
  96. private void Update()
  97. {
  98. if(downloaderOperation != null)
  99. {
  100. if(downloaderOperation.IsDone)
  101. {
  102. foreach(var t in locationsLoading)
  103. {
  104. LoadManager.Instance.SetResDownloaded(t);
  105. }
  106. downloaderOperation = null;
  107. locationsLoading = null;
  108. }
  109. }
  110. if(downloaderOperation == null && waitList.Count > 0)
  111. {
  112. waitList.Reverse();//看了yooasset源码,疑似他是从后往前加载,这里翻转一下
  113. locationsLoading = waitList.ToArray();
  114. downloaderOperation = YooAssets.CreateBundleDownloader(locationsLoading, 1, 3);
  115. downloaderOperation.BeginDownload();
  116. waitList.Clear();
  117. }
  118. }
  119. }
  120. }