LoaderBuilder.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using XGame.Framework.Utils;
  2. using XGame.Framework.Asset.Addressable;
  3. using System;
  4. using UnityEngine;
  5. namespace XGame.Framework.Asset
  6. {
  7. internal partial class LoaderBuilder
  8. {
  9. private LoaderType _loadType;
  10. private AssetBundle _mainbundle;
  11. private bool _isBundleEncrypt;
  12. public LoaderBuilder()
  13. {
  14. #if UNITY_EDITOR
  15. var isSimluator = UnityEditor.EditorPrefs.GetBool(Define.SIMULATE_ASSETBUNDLE_EDITOR_KEY, true);
  16. _loadType = isSimluator ? LoaderType.AssetDatabase : LoaderType.AssetBundle;
  17. #else
  18. _loadType = LoaderType.AssetBundle;
  19. #endif
  20. InitMainbundle();
  21. }
  22. private void InitMainbundle()
  23. {
  24. if (_loadType == LoaderType.AssetBundle)
  25. {
  26. #if UNITY_EDITOR
  27. InitMainBundleEditor();
  28. #else
  29. _isBundleEncrypt = true;
  30. var bundlePath = FileUtils.GetAssetBundlePath(Define.MANIFEST_BUNDLE_FULLNAME);
  31. _mainbundle = AssetBundle.LoadFromFile(bundlePath, 0, Define.MANIFEST_BUNDLE_OFFSET);//"kcmanifest.bundle"
  32. #endif
  33. }
  34. }
  35. private IAddressableManifest LoadAddressableManifest()
  36. {
  37. if (_loadType == LoaderType.AssetDatabase)
  38. {
  39. #if UNITY_EDITOR
  40. return LoadAddressableManifestEditor();
  41. #endif
  42. }
  43. else if (_loadType == LoaderType.AssetBundle)
  44. {
  45. var manifest = new CSharpAddressableManifest();
  46. if (_mainbundle != null)
  47. {
  48. var builtInAssetSo = _mainbundle.LoadAsset<AddressableInfosSo>(Define.BUILTIN_ASSET_MANIFEST);
  49. manifest.Init(builtInAssetSo);
  50. var productAssetSo = _mainbundle.LoadAsset<AddressableInfosSo>(Define.PRODUCT_ASSET_MANIFEST);
  51. manifest.Init(productAssetSo);
  52. }
  53. return manifest;
  54. }
  55. return null;
  56. }
  57. private IAssetLoader CreateLoader(LoaderType loaderType, IAddressableManifest addressableManifest)
  58. {
  59. IAssetLoader loader = null;
  60. switch (loaderType)
  61. {
  62. case LoaderType.AssetDatabase:
  63. #if UNITY_EDITOR
  64. loader = new AssetDatabaseLoader(addressableManifest as IAddressableEditorHandler);
  65. #endif
  66. break;
  67. case LoaderType.AssetBundle:
  68. {
  69. var bundlesSo = _mainbundle?.LoadAsset<AssetBundleInfosSo>(Define.ASSET_BUNDLE_MANIFEST) ?? null;//"KCAssetBundleManifest"
  70. var bundleManifest = new CSharpAssetBundleManifest(bundlesSo);
  71. var referencesSo = _mainbundle?.LoadAsset<AssetReferenceInfosSo>(Define.ASSET_REFERENCE_MANIFEST) ?? null;
  72. var referenceManifest = new CSharpAssetReferencesManifest(referencesSo);
  73. var sceneInfosSo = _mainbundle?.LoadAsset<SceneInfosSo>(Define.SCENE_BUNDLE_MANIFEST) ?? null;
  74. var sceneInfosManifest = new CSharpSceneInfosManifest(sceneInfosSo);
  75. loader = new AssetBundleLoader(bundleManifest, referenceManifest, sceneInfosManifest)
  76. {
  77. IsBundleEncrypt = _isBundleEncrypt
  78. };
  79. }
  80. break;
  81. case LoaderType.Resources:
  82. loader = new ResourcesLoader(addressableManifest);
  83. break;
  84. case LoaderType.Binary:
  85. loader = new BinaryLoader();
  86. break;
  87. }
  88. return loader;
  89. }
  90. private void Release()
  91. {
  92. _mainbundle?.Unload(true);
  93. _mainbundle = null;
  94. }
  95. public IAssetLoader Build()
  96. {
  97. var addressableManifest = LoadAddressableManifest();
  98. var array = Enum.GetValues(typeof(LoaderType));
  99. var loaders = new IAssetLoader[array.Length];
  100. foreach (LoaderType enumVal in array)
  101. {
  102. if ((enumVal == LoaderType.AssetBundle && _loadType == LoaderType.AssetDatabase) ||
  103. (enumVal == LoaderType.AssetDatabase && _loadType == LoaderType.AssetBundle))
  104. {//创建Loader时AssetDatabase和AssetBundle二选一
  105. continue;
  106. }
  107. loaders[(int)enumVal] = CreateLoader(enumVal, addressableManifest);
  108. }
  109. var adapter = new AssetLoader(_loadType, loaders, addressableManifest);
  110. Release();
  111. return adapter;
  112. }
  113. }
  114. }