AssetDatabaseLoader.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #if UNITY_EDITOR
  2. using XGame.Framework.Asset.Addressable;
  3. using UnityEditor;
  4. using UnityEngine;
  5. namespace XGame.Framework.Asset
  6. {
  7. internal class AssetDatabaseLoader : BaseLoader, IAssetLoader
  8. {
  9. private IAddressableEditorHandler _addressablehandler;
  10. public AssetDatabaseLoader(IAddressableEditorHandler addressablehandler)
  11. {
  12. SimulateAsyncMono.Instance.Init();
  13. _addressablehandler = addressablehandler;
  14. //LoadAssetInfoManifest(AddressableClassify.BuiltIn);
  15. //LoadAssetInfoManifest(AddressableClassify.Product);
  16. //LoadAssetInfoManifest(AddressableClassify.Tests);
  17. //var manifestPath = $"Assets/KCAssetBundlesData/AtlasAlphaTextures/{Define.ALPHA_TEXTURE_MANIFEST}.asset";
  18. //var alphaManifest = AssetDatabase.LoadAssetAtPath<AlphaTextureManifest>(manifestPath);
  19. //if (alphaManifest != null)
  20. //{
  21. // alphaManifest.Init();
  22. // AssetCtrl.SetAlphaManifest(alphaManifest);
  23. //}
  24. }
  25. //private void LoadAssetInfoManifest(AddressableClassify classify)
  26. //{
  27. // string fileName;// = classify == AddressableClassifyOperation.BuiltIn ? Define.BUILTIN_ASSET_MANIFEST : Define.PRODUCT_ASSET_MANIFEST;
  28. // if (classify == AddressableClassify.BuiltIn)
  29. // {
  30. // fileName = Define.BUILTIN_ASSET_MANIFEST;
  31. // }
  32. // else if (classify == AddressableClassify.Tests)
  33. // {
  34. // fileName = "TestsManifest";
  35. // }
  36. // else
  37. // {
  38. // fileName = Define.PRODUCT_ASSET_MANIFEST;
  39. // }
  40. // var manifestPath = $"Assets/KCAssetBundlesData/Addressable/{fileName}.asset";
  41. // var assetManifest = AssetDatabase.LoadAssetAtPath<AddressableInfosSo>(manifestPath);
  42. // if (assetManifest != null)
  43. // {
  44. // AssetCtrl.AddManifest(classify, assetManifest);
  45. // }
  46. // else if (classify == AddressableClassify.Product)
  47. // {
  48. // Log.Error($"Can't find AddressableAssetInfoManifest. Path:{manifestPath}");
  49. // }
  50. //}
  51. public IAssetAsync LoadAsync<TResult>(string addressableName)
  52. {
  53. //if (!System.IO.File.Exists(assetPath))
  54. //{
  55. // Log.Error($"AssetDatabase LoadAsync Error. File is not exists. Type: {typeof(TResult)} Path:{assetPath}");
  56. //}
  57. //Assert.IsTrue(System.IO.File.Exists(assetPath), $"AssetDatabase LoadAsync Error. File is not exists. Type: {typeof(TResult)} Path:{assetPath}");
  58. return GetAssetAsync(addressableName, typeof(TResult));
  59. }
  60. public object LoadSync<TResult>(string addressableName)
  61. {
  62. //if (!System.IO.File.Exists(addressableName))
  63. //{
  64. // Log.Error($"AssetDatabase LoadSync Error. File is not exists.Type: {typeof(TResult)} Path:{addressableName}");
  65. // return null;
  66. //}
  67. //Assert.IsTrue(System.IO.File.Exists(assetPath), $"AssetDatabase LoadSync Error. File is not exists.Type: {typeof(TResult)} Path:{assetPath}");
  68. var assetPath = GetPathByName(addressableName);
  69. var type = typeof(TResult);
  70. var assetObj = type == typeof(Sprite) ? AssetDatabase.LoadAssetAtPath(assetPath, type) : AssetDatabase.LoadMainAssetAtPath(assetPath);
  71. return assetObj;
  72. }
  73. public IAssetAsync LoadSceneBundleAsync(string sceneName)
  74. {
  75. AssetDatabaseAsync async = new AssetDatabaseAsync()
  76. {
  77. AddressableName = sceneName,
  78. IsSprite = false,
  79. };
  80. AsyncPool.AddAsync(async);
  81. return async;
  82. }
  83. public void UnLoadSceneBundle(string sceneName)
  84. {
  85. }
  86. /// <summary>
  87. /// AssetDatabase加载模式下缺少KCAssetReferenceManifest
  88. /// 最好不要删资源,否则可能出现误删
  89. /// 这里删除主要是考虑到在AssetDatabase加载模式下使用Profiler
  90. /// </summary>
  91. /// <param name="addressableName"></param>
  92. /// <param name="source"></param>
  93. public void Unload(string addressableName, object source)
  94. {
  95. if (source is Object obj)
  96. {
  97. obj.Unload();
  98. }
  99. }
  100. public void UnloadUnusedAssets()
  101. {
  102. Resources.UnloadUnusedAssets();
  103. }
  104. protected override void OnDispose()
  105. {
  106. _addressablehandler = null;
  107. SimulateAsyncMono.Dispose();
  108. }
  109. private string GetPathByName(string addressableName)
  110. {
  111. var assetGUID = _addressablehandler.GetAssetGUID(addressableName);
  112. if (!string.IsNullOrEmpty(assetGUID))
  113. {
  114. return AssetDatabase.GUIDToAssetPath(assetGUID);
  115. }
  116. else
  117. {
  118. AssetsLog.Error($"AssetDatabaseLoader can't find guid. Name:{addressableName}");
  119. }
  120. return string.Empty;
  121. }
  122. private IAssetAsync GetAssetAsync(string addressableName, System.Type assetType)
  123. {
  124. var assetAsync = AsyncPool.GetAssetAsync(addressableName);
  125. if (assetAsync == null)
  126. {
  127. AssetDatabaseAsync async = new AssetDatabaseAsync
  128. {
  129. AddressableName = addressableName,
  130. Path = GetPathByName(addressableName),
  131. IsSprite = assetType == typeof(Sprite),
  132. };
  133. assetAsync = async;
  134. AsyncPool.AddAsync(assetAsync);
  135. }
  136. return assetAsync;
  137. }
  138. }
  139. }
  140. #endif