#if UNITY_EDITOR using XGame.Framework.Asset.Addressable; using UnityEditor; using UnityEngine; namespace XGame.Framework.Asset { internal class AssetDatabaseLoader : BaseLoader, IAssetLoader { private IAddressableEditorHandler _addressablehandler; public AssetDatabaseLoader(IAddressableEditorHandler addressablehandler) { SimulateAsyncMono.Instance.Init(); _addressablehandler = addressablehandler; //LoadAssetInfoManifest(AddressableClassify.BuiltIn); //LoadAssetInfoManifest(AddressableClassify.Product); //LoadAssetInfoManifest(AddressableClassify.Tests); //var manifestPath = $"Assets/KCAssetBundlesData/AtlasAlphaTextures/{Define.ALPHA_TEXTURE_MANIFEST}.asset"; //var alphaManifest = AssetDatabase.LoadAssetAtPath(manifestPath); //if (alphaManifest != null) //{ // alphaManifest.Init(); // AssetCtrl.SetAlphaManifest(alphaManifest); //} } //private void LoadAssetInfoManifest(AddressableClassify classify) //{ // string fileName;// = classify == AddressableClassifyOperation.BuiltIn ? Define.BUILTIN_ASSET_MANIFEST : Define.PRODUCT_ASSET_MANIFEST; // if (classify == AddressableClassify.BuiltIn) // { // fileName = Define.BUILTIN_ASSET_MANIFEST; // } // else if (classify == AddressableClassify.Tests) // { // fileName = "TestsManifest"; // } // else // { // fileName = Define.PRODUCT_ASSET_MANIFEST; // } // var manifestPath = $"Assets/KCAssetBundlesData/Addressable/{fileName}.asset"; // var assetManifest = AssetDatabase.LoadAssetAtPath(manifestPath); // if (assetManifest != null) // { // AssetCtrl.AddManifest(classify, assetManifest); // } // else if (classify == AddressableClassify.Product) // { // Log.Error($"Can't find AddressableAssetInfoManifest. Path:{manifestPath}"); // } //} public IAssetAsync LoadAsync(string addressableName) { //if (!System.IO.File.Exists(assetPath)) //{ // Log.Error($"AssetDatabase LoadAsync Error. File is not exists. Type: {typeof(TResult)} Path:{assetPath}"); //} //Assert.IsTrue(System.IO.File.Exists(assetPath), $"AssetDatabase LoadAsync Error. File is not exists. Type: {typeof(TResult)} Path:{assetPath}"); return GetAssetAsync(addressableName, typeof(TResult)); } public object LoadSync(string addressableName) { //if (!System.IO.File.Exists(addressableName)) //{ // Log.Error($"AssetDatabase LoadSync Error. File is not exists.Type: {typeof(TResult)} Path:{addressableName}"); // return null; //} //Assert.IsTrue(System.IO.File.Exists(assetPath), $"AssetDatabase LoadSync Error. File is not exists.Type: {typeof(TResult)} Path:{assetPath}"); var assetPath = GetPathByName(addressableName); var type = typeof(TResult); var assetObj = type == typeof(Sprite) ? AssetDatabase.LoadAssetAtPath(assetPath, type) : AssetDatabase.LoadMainAssetAtPath(assetPath); return assetObj; } public IAssetAsync LoadSceneBundleAsync(string sceneName) { AssetDatabaseAsync async = new AssetDatabaseAsync() { AddressableName = sceneName, IsSprite = false, }; AsyncPool.AddAsync(async); return async; } public void UnLoadSceneBundle(string sceneName) { } /// /// AssetDatabase加载模式下缺少KCAssetReferenceManifest /// 最好不要删资源,否则可能出现误删 /// 这里删除主要是考虑到在AssetDatabase加载模式下使用Profiler /// /// /// public void Unload(string addressableName, object source) { if (source is Object obj) { obj.Unload(); } } public void UnloadUnusedAssets() { Resources.UnloadUnusedAssets(); } protected override void OnDispose() { _addressablehandler = null; SimulateAsyncMono.Dispose(); } private string GetPathByName(string addressableName) { var assetGUID = _addressablehandler.GetAssetGUID(addressableName); if (!string.IsNullOrEmpty(assetGUID)) { return AssetDatabase.GUIDToAssetPath(assetGUID); } else { AssetsLog.Error($"AssetDatabaseLoader can't find guid. Name:{addressableName}"); } return string.Empty; } private IAssetAsync GetAssetAsync(string addressableName, System.Type assetType) { var assetAsync = AsyncPool.GetAssetAsync(addressableName); if (assetAsync == null) { AssetDatabaseAsync async = new AssetDatabaseAsync { AddressableName = addressableName, Path = GetPathByName(addressableName), IsSprite = assetType == typeof(Sprite), }; assetAsync = async; AsyncPool.AddAsync(assetAsync); } return assetAsync; } } } #endif