AssetLoader.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using XGame.Framework.i18n;
  2. using XGame.Framework.Quality;
  3. using XGame.Framework.Asset.Addressable;
  4. using System;
  5. namespace XGame.Framework.Asset
  6. {
  7. internal class AssetLoader : IAssetLoader, IDisposable, IAddressableHandle
  8. {
  9. private LoaderType _loadType;
  10. private IAssetLoader[] _loaders;
  11. private IAddressableManifest _addressableManifest;
  12. public AssetLoader(LoaderType loadType, IAssetLoader[] loaders, IAddressableManifest addressableManifest)
  13. {
  14. _loadType = loadType;
  15. _loaders = loaders;
  16. _addressableManifest = addressableManifest;
  17. }
  18. public IAssetAsync LoadAsync<TResult>(string addressableName)
  19. {
  20. //addressableName = assetInfoManifest.AssetPathToAddressableName(addressableName);
  21. return GetLoader(addressableName).LoadAsync<TResult>(addressableName);
  22. }
  23. public object LoadSync<TResult>(string addressableName)
  24. {
  25. //addressableName = assetInfoManifest.AssetPathToAddressableName(addressableName);
  26. return GetLoader(addressableName).LoadSync<TResult>(addressableName);
  27. }
  28. void IDisposable.Dispose()
  29. {
  30. foreach (var loader in _loaders)
  31. {
  32. (loader as IDisposable)?.Dispose();
  33. }
  34. _loaders = null;
  35. (_addressableManifest as IDisposable)?.Dispose();
  36. _addressableManifest = null;
  37. }
  38. private IAssetLoader GetLoader(string addressableName)
  39. {
  40. if (_addressableManifest.IsInResourcesByName(addressableName))
  41. {
  42. return _loaders[(int)LoaderType.Resources];
  43. }
  44. else if (!_addressableManifest.ContainsName(addressableName))
  45. {
  46. var text = " ";
  47. if (!string.IsNullOrEmpty(addressableName) && addressableName.Contains(text))
  48. {
  49. text = "addressableName 包含空格. ";
  50. }
  51. AssetsLog.Error($"找不到资源信息.{text}Name:{addressableName}");
  52. }
  53. return _loaders[(int)_loadType];
  54. }
  55. public void Unload(string addressableName, object source)
  56. {
  57. //addressableName = assetInfoManifest.AssetPathToAddressableName(addressableName);
  58. GetLoader(addressableName).Unload(addressableName, source);
  59. }
  60. public void UnloadUnusedAssets()
  61. {
  62. foreach (var loader in _loaders)
  63. {
  64. loader?.UnloadUnusedAssets();
  65. }
  66. }
  67. public IAssetAsync LoadSceneBundleAsync(string sceneName)
  68. {
  69. return _loaders[(int)_loadType].LoadSceneBundleAsync(sceneName);
  70. }
  71. public void UnLoadSceneBundle(string sceneName)
  72. {
  73. _loaders[(int)_loadType].UnLoadSceneBundle(sceneName);
  74. }
  75. /// <summary>
  76. /// 根据ConvertOptions转换对应多语言或者LOD版本资源
  77. /// </summary>
  78. /// <param name="options"></param>
  79. /// <returns></returns>
  80. string IAddressableHandle.ConvertAddressableName(ConvertOptions options)
  81. {
  82. options.addressableName = options.addressableName?.ToLower();
  83. if ((options.langType != LanguageType.NONE ||
  84. options.quality.IsLodQuality()) &&
  85. TryConvertAddressableName(ref options, out var name))
  86. {
  87. return name;
  88. }
  89. return options.addressableName;
  90. }
  91. string IAddressableHandle.GetAlphaTextureName(string spriteName)
  92. {
  93. throw new NotImplementedException();
  94. }
  95. /// <summary>
  96. /// 尝试转换指定资源的对应多语言或者LOD版本资源
  97. /// </summary>
  98. /// <param name="options"></param>
  99. /// <param name="addressableName"></param>
  100. /// <returns></returns>
  101. private bool TryConvertAddressableName(ref ConvertOptions options, out string addressableName)
  102. {
  103. addressableName = options.addressableName;
  104. var result = false;
  105. if (_addressableManifest.TryGetLodAddressableName(addressableName, options.quality, out var lodName))
  106. {
  107. //先取Lod资源
  108. addressableName = lodName;
  109. result = true;
  110. }
  111. if (_addressableManifest.TryGetI18nAddressableName(addressableName, options.langType, out var i18nName))
  112. {
  113. //再取多语言资源
  114. addressableName = i18nName;
  115. result = true;
  116. }
  117. if (result)
  118. {
  119. return true;
  120. }
  121. addressableName = string.Empty;
  122. return false;
  123. }
  124. }
  125. }