LoaderBuildAsync.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using XGame.Framework.Utils;
  2. using XGame.Framework.Asset.Addressable;
  3. using System;
  4. using UnityEngine;
  5. using XGame.Framework.Asyncs;
  6. using UnityEngine.Networking;
  7. namespace XGame.Framework.Asset
  8. {
  9. internal partial class LoaderBuildAsync : Async, IAsyncResultable<IAssetLoader>
  10. {
  11. private LoaderType _loadType;
  12. private AssetBundle _mainbundle;
  13. private bool _isBundleEncrypt;
  14. public IAssetLoader Result { get; private set; }
  15. public LoaderBuildAsync()
  16. {
  17. #if UNITY_EDITOR
  18. var isSimluator = UnityEditor.EditorPrefs.GetBool(Define.SIMULATE_ASSETBUNDLE_EDITOR_KEY, true);
  19. _loadType = isSimluator ? LoaderType.AssetDatabase : LoaderType.AssetBundle;
  20. #else
  21. _loadType = LoaderType.AssetBundle;
  22. #endif
  23. }
  24. // private void InitMainbundle()
  25. // {
  26. // if (_loadType == LoaderType.AssetBundle)
  27. // {
  28. //#if UNITY_EDITOR
  29. // InitMainBundleEditor();
  30. //#else
  31. // _isBundleEncrypt = true;
  32. // var bundlePath = FileUtils.GetAssetBundlePath(Define.MANIFEST_BUNDLE_FULLNAME);
  33. // _mainbundle = AssetBundle.LoadFromFile(bundlePath, 0, Define.MANIFEST_BUNDLE_OFFSET);//"kcmanifest.bundle"
  34. //#endif
  35. // }
  36. // }
  37. private IAddressableManifest LoadAddressableManifest()
  38. {
  39. if (_loadType == LoaderType.AssetDatabase)
  40. {
  41. #if UNITY_EDITOR
  42. return LoadAddressableManifestEditor();
  43. #endif
  44. }
  45. else if (_loadType == LoaderType.AssetBundle)
  46. {
  47. var manifest = new CSharpAddressableManifest();
  48. if (_mainbundle != null)
  49. {
  50. var builtInAssetSo = _mainbundle.LoadAsset<AddressableInfosSo>(Define.BUILTIN_ASSET_MANIFEST);
  51. manifest.Init(builtInAssetSo);
  52. var productAssetSo = _mainbundle.LoadAsset<AddressableInfosSo>(Define.PRODUCT_ASSET_MANIFEST);
  53. manifest.Init(productAssetSo);
  54. }
  55. return manifest;
  56. }
  57. return null;
  58. }
  59. private IAssetLoader CreateLoader(LoaderType loaderType, IAddressableManifest addressableManifest)
  60. {
  61. IAssetLoader loader = null;
  62. switch (loaderType)
  63. {
  64. case LoaderType.AssetDatabase:
  65. #if UNITY_EDITOR
  66. loader = new AssetDatabaseLoader(addressableManifest as IAddressableEditorHandler);
  67. #endif
  68. break;
  69. case LoaderType.AssetBundle:
  70. {
  71. var bundlesSo = _mainbundle?.LoadAsset<AssetBundleInfosSo>(Define.ASSET_BUNDLE_MANIFEST) ?? null;//"KCAssetBundleManifest"
  72. var bundleManifest = new CSharpAssetBundleManifest(bundlesSo);
  73. var referencesSo = _mainbundle?.LoadAsset<AssetReferenceInfosSo>(Define.ASSET_REFERENCE_MANIFEST) ?? null;
  74. var referenceManifest = new CSharpAssetReferencesManifest(referencesSo);
  75. var sceneInfosSo = _mainbundle?.LoadAsset<SceneInfosSo>(Define.SCENE_BUNDLE_MANIFEST) ?? null;
  76. var sceneInfosManifest = new CSharpSceneInfosManifest(sceneInfosSo);
  77. loader = new AssetBundleLoader(bundleManifest, referenceManifest, sceneInfosManifest)
  78. {
  79. IsBundleEncrypt = _isBundleEncrypt
  80. };
  81. }
  82. break;
  83. case LoaderType.Resources:
  84. loader = new ResourcesLoader(addressableManifest);
  85. break;
  86. case LoaderType.Binary:
  87. loader = new BinaryLoader();
  88. break;
  89. }
  90. return loader;
  91. }
  92. private void Release()
  93. {
  94. _mainbundle?.Unload(true);
  95. _mainbundle = null;
  96. }
  97. private IAssetLoader Build()
  98. {
  99. var addressableManifest = LoadAddressableManifest();
  100. var array = Enum.GetValues(typeof(LoaderType));
  101. var loaders = new IAssetLoader[array.Length];
  102. foreach (LoaderType enumVal in array)
  103. {
  104. if ((enumVal == LoaderType.AssetBundle && _loadType == LoaderType.AssetDatabase) ||
  105. (enumVal == LoaderType.AssetDatabase && _loadType == LoaderType.AssetBundle))
  106. {//创建Loader时AssetDatabase和AssetBundle二选一
  107. continue;
  108. }
  109. loaders[(int)enumVal] = CreateLoader(enumVal, addressableManifest);
  110. }
  111. var adapter = new AssetLoader(_loadType, loaders, addressableManifest);
  112. Release();
  113. return adapter;
  114. }
  115. public void Start()
  116. {
  117. if (_loadType == LoaderType.AssetBundle)
  118. {
  119. #if UNITY_EDITOR
  120. InitMainBundleEditor();
  121. Result = Build();
  122. Completed();
  123. #elif UNITY_WEBGL || UNITY_WEIXINMINIGAME
  124. var bundlePath = FileUtils.GetAssetBundlePath(Define.MANIFEST_BUNDLE_FULLNAME);
  125. var request = UnityWebRequestAssetBundle.GetAssetBundle(bundlePath);
  126. var op = request.SendWebRequest();
  127. op.completed += (_) =>
  128. {
  129. if (!request.isDone || !op.isDone)
  130. {
  131. Debug.LogError($"Assetbundle error: manifest加载失败. request:{request.isDone} op:{op.isDone} Path:{bundlePath}");
  132. Completed();
  133. return;
  134. }
  135. var bundle = DownloadHandlerAssetBundle.GetContent(request);
  136. if (bundle == null)
  137. {
  138. Debug.LogError($"Assetbundle error: mainfest为空. {bundlePath}");
  139. Completed();
  140. return;
  141. }
  142. _mainbundle = bundle;
  143. Result = Build();
  144. Completed();
  145. };
  146. #else
  147. _isBundleEncrypt = true;
  148. var bundlePath = FileUtils.GetAssetBundlePath(Define.MANIFEST_BUNDLE_FULLNAME);
  149. _mainbundle = AssetBundle.LoadFromFile(bundlePath, 0, Define.MANIFEST_BUNDLE_OFFSET);//"kcmanifest.bundle"
  150. Result = Build();
  151. Completed();
  152. #endif
  153. }
  154. else
  155. {
  156. Result = Build();
  157. Completed();
  158. }
  159. }
  160. }
  161. }