#region Copyright RenGuiYou. All rights reserved. //===================================================== // NeatlyFrameWork // Author: RenGuiyou // Feedback: mailto:750539605@qq.com //===================================================== #endregion using System; using System.IO; using UnityEngine; namespace Neatly.Load { public class AssetBundleLoader : Loader { AssetBundleCreateRequest m_ABCreateQuest; protected override void Init(string path, Action callback, bool sync = true) { var pathAtlasTp = NeatlyConfig.PATH_BUNDLE_ATLAS_TP; IsAtlas = path.IndexOf(pathAtlasTp, StringComparison.Ordinal) != -1; if (IsAtlas) { AtlasName = path.Replace(pathAtlasTp, string.Empty).SubstringTo("."); } base.Init(path, callback, sync); } public override void Load() { base.Load(); string m_Name = GetFilePath(Path); if (m_Sync) { AssetBundle assetBundle = null; try { Developer.StartLoadBundle(Path); assetBundle = AssetBundle.LoadFromFile(m_Name); Developer.EndLoadBundle(Path); if (IsAtlas) { Developer.StartLoadAtlas(Path); var gameObject = assetBundle.LoadAsset(AtlasName); AtlasInfo = gameObject.GetComponent(); AtlasInfo.Init(); Developer.EndLoadAtlas(Path); } } catch (Exception e) { Debug.LogException(e); } finally { OnLoadCompleted(assetBundle); } } else { m_ABCreateQuest = AssetBundle.LoadFromFileAsync(m_Name); } } public override void Update() { if (m_ABCreateQuest == null) { Debug.LogErrorFormat("[异步加载错误] Path:{0}", Path); return; } if (m_ABCreateQuest.isDone) { OnLoadCompleted(m_ABCreateQuest.assetBundle); } } private string GetFilePath(string name) { #if UNITY_STANDALONE_WIN return $"{NeatlyConfig.PATH_PACKAGE_BUNDLE}/{name}"; #endif for (int i = 0; i < NeatlyConfig.ASSETS_PATHS.Length; i++) { var fullPath = string.Format("{0}/{1}", NeatlyConfig.ASSETS_PATHS[i], name); if (File.Exists(fullPath)) { return fullPath; } } string inputPath = string.Format("{0}/{1}", NeatlyConfig.PATH_PACKAGE_BUNDLE, name); #if NOT_DECOMPRESS return inputPath; // #if UNITY_ANDROID // //找不到资源, 从包里复制 // string outputPath = string.Format("{0}/{1}", NeatlyConfig.PATH_UPDATE_BUNDLE, name); // FileModule.Instance.CopyFile(inputPath, outputPath); // return outputPath; // #else // return inputPath; // #endif #else //找不到资源, 从包里解压 string outputPath = string.Format("{0}/{1}", NeatlyConfig.PATH_UPDATE_BUNDLE, name); FileModule.Instance.Uncompress(inputPath, outputPath); return outputPath; #endif } } }