// 初始化AB public static void InitABVersion() { // 实例化 LocalVersionList = new Dictionary <int, VersionInfo>(); // 需要的文件夹 if (!Directory.Exists(ABHelper.AppTempCachePath)) { Directory.CreateDirectory(ABHelper.AppTempCachePath); } // 本地发包时的版本号 TextAsset resInfo = Resources.Load <TextAsset>(ABHelper.GetFileNameWithoutSuffix(ABHelper.OriginalVersionName)); if (null == resInfo) { throw new Exception("本地资源版本号不可为空!!!"); } // 初始时的版号 OriginalVersionId = new VersionNum(resInfo.text.Replace("\r", "")); CurVersionId = OriginalVersionId; // 本地更新地址 LocalStorgePath = string.Format("{0}/{1}", ABHelper.AppTempCachePath, OriginalVersionId.Id1A2); if (!Directory.Exists(LocalStorgePath)) { Directory.CreateDirectory(LocalStorgePath); } // 获取本地的所有版本 DirectoryInfo dirInfo = new DirectoryInfo(LocalStorgePath); DirectoryInfo[] dirInfos = dirInfo.GetDirectories(); // 遍历本地所有版号 foreach (DirectoryInfo info in dirInfos) { int id3rd = int.Parse(info.Name); if (id3rd < OriginalVersionId.Id3rd) { continue; } VersionNum versionId = new VersionNum(OriginalVersionId.Id1st, OriginalVersionId.Id2nd, id3rd); if (!LoadVersionInfo(versionId)) { continue; } if (null == CurVersionId || versionId.Id3rd > CurVersionId.Id3rd) { CurVersionId = LocalVersionList[versionId.Id3rd].VersionId; } } // 现在在用的版本号 SetCursVersionNum(CurVersionId); }
private void DownloadExceptionFile() { // 下载弹框确认 onHandleState(new ABHelper.VersionArgs(ABHelper.EVersionState.DownloadConfirm, (float)downloadSize, (str) => { Action download = () => { // 初始化 ABDownload.Instance.InitDownload(); foreach (var pair in exceptionList) { // 注册下载事件 ABDownload.Instance.downloadResult = FixResult; ABDownload.Instance.downloadCountProcess = DownloadPorcess; bool fromVersionPath = true; string relativePath = ABVersion.CurVersionInfo.GetAbRelativePath(pair.Key, ref fromVersionPath); string fileSuffix = relativePath.Contains(".") ? relativePath.Substring(relativePath.LastIndexOf(".")) : ""; string fileName = ABHelper.GetFileNameWithoutSuffix(relativePath); string remoteUrl = string.Format("{0}{1}", ABVersion.RemoteUrlPlatform, relativePath); string localUrl = string.Format("{0}/{1}", ABVersion.LocalStorgePath, ABHelper.GetFileFolderPath(relativePath)); ABDownload.Instance.CreateDownloadTask(remoteUrl, localUrl, fileName, fileSuffix, int.Parse(pair.Value[2]), false); } ABDownload.Instance.BeginDownload(); }; // 检测是否为移动数据 if (Application.internetReachability != NetworkReachability.ReachableViaLocalAreaNetwork) { // wifi状态检测 onHandleState(new ABHelper.VersionArgs(ABHelper.EVersionState.DownloadWifi, (str2) => { download(); } )); } else { download(); } })); }
private ABLoadTask NewLoadTask(string abPath, string abName, Type type, Action <UnityEngine.Object> complete = null, Action <float> progress = null, bool isScene = false) { abPath = abPath.ToLower(); abName = (null == abName) ? ABHelper.GetFileNameWithoutSuffix(abPath) : abName.ToLower(); // 新建任务 ABLoadTask task = null; if (m_loadTaskRecycle.ContainsKey(abPath) && m_loadTaskRecycle[abPath].Count > 0) { task = m_loadTaskRecycle[abPath][0]; m_loadTaskRecycle[abPath].RemoveAt(0); } else { task = new ABLoadTask(); } task.Reset(abPath, abName, type, complete, progress, isScene); return(task); }
private ABLoadTask NewLoadTask(string abPath, string abName, Type type, Action <string, UnityEngine.Object> complete = null, Action <float> progress = null, bool isScene = false) { bool fromNativePath = true; string abRealPath = ABVersion.CurVersionInfo.GetABFullPath(abPath, ref fromNativePath); abPath = abPath.ToLower(); abName = (null == abName) ? ABHelper.GetFileNameWithoutSuffix(abPath) : abName.ToLower(); // 新建任务 ABLoadTask task = null; if (m_loadTaskRecycle.Count > 0) { task = m_loadTaskRecycle[0]; m_loadTaskRecycle.RemoveAt(0); } else { task = new ABLoadTask(); } task.Reset(abPath, abName, abRealPath, type, complete, progress, isScene); return(task); }
private static void CreatPacker(BuildTarget platform) { ClearAssetBundlesName(); AssetDatabase.Refresh(); CurVersionManifestList = new Dictionary <string, List <string> >(); List <string> folderBundlePathList = new List <string>(); List <string> fileBundlePathList = new List <string>(); List <string> sceneBundlePathList = new List <string>(); List <string> luaPathList = new List <string>(); foreach (string path in BundleFolderPath) { string fullPath = ResFolder + path; folderBundlePathList.AddRange(ABHelper.GetAllFilesPathInDir(fullPath)); } foreach (string path in BundleFilePath) { string fullPath = ResFolder + path; fileBundlePathList.AddRange(ABHelper.GetAllFilesPathInDir(fullPath)); } foreach (string path in BundleScenePath) { string fullPath = AssetFolder + path; sceneBundlePathList.AddRange(ABHelper.GetAllFilesPathInDir(fullPath)); } foreach (string path in BundleLuaPath) { string fullPath = AssetFolder + path; luaPathList.AddRange(ABHelper.GetAllFilesPathInDir(fullPath)); } foreach (string path in folderBundlePathList) { if (!IsNeedFileRes(path)) { continue; } // "Assets/Resources/UI/Tips/Tips.bytes" string assetPath = path.Replace("\\", "/"); string abName = ABHelper.GetFileFolderPath(assetPath.Replace(ResFolder, "")) + ".ab"; AssetImporter.GetAtPath(assetPath).assetBundleName = CreatFileUrlMd5(abName); } foreach (string path in fileBundlePathList) { if (!IsNeedFileRes(path)) { continue; } string assetPath = path.Replace("\\", "/"); string abName = ABHelper.GetFileFullPathWithoutFtype(assetPath.Replace(ResFolder, "")) + ".ab"; AssetImporter.GetAtPath(assetPath).assetBundleName = CreatFileUrlMd5(abName); // 依赖文件 string[] dependPaths = AssetDatabase.GetDependencies(assetPath); if (dependPaths.Length > 0) { foreach (string path1 in dependPaths) { if (path1.Contains(ResFolder) && assetPath != path1) { assetPath = assetPath.Replace(ResFolder, ""); if (!CurVersionManifestList.ContainsKey(assetPath)) { CurVersionManifestList.Add(assetPath, new List <string>()); } CurVersionManifestList[assetPath].Add(path1.Replace(ResFolder, "")); } } } } foreach (string path in sceneBundlePathList) { if (!path.EndsWith(".unity")) { continue; } string assetPath = path.Replace("\\", "/"); string abName = ABHelper.GetFileFullPathWithoutFtype(assetPath.Replace(ResFolder, "") + ".ab"); AssetImporter.GetAtPath(assetPath).assetBundleName = CreatFileUrlMd5(abName); // 依赖文件 string[] dependPaths = AssetDatabase.GetDependencies(assetPath); if (dependPaths.Length > 0) { foreach (string path1 in dependPaths) { if (path1.Contains(ResFolder) && assetPath != path1) { assetPath = assetPath.Replace(AssetFolder, ""); if (!CurVersionManifestList.ContainsKey(assetPath)) { CurVersionManifestList.Add(assetPath, new List <string>()); } CurVersionManifestList[assetPath].Add(path1.Replace(ResFolder, "")); } } } } foreach (string path in luaPathList) { if (!path.EndsWith(".lua")) { continue; } string filePath = path.Replace("\\", "/"); CopyLuaFiles(filePath); } System.GC.Collect(); BuildPipeline.BuildAssetBundles(PlatformABExportPath, BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle, platform); ClearAssetBundlesName(); AssetDatabase.Refresh(); // ab的依赖文件 string fileUrl = CreatFileUrlMd5(ABHelper.ManifestFileName); ABHelper.WriteManifestFile(PlatformABExportPath + "/" + fileUrl, CurVersionManifestList); // 删除不用的manifest List <string> allFiles = ABHelper.GetAllFilesPathInDir(PlatformABExportPath); foreach (string path in allFiles) { if (path.EndsWith(".manifest")) { File.Delete(path); } if (ABHelper.GetFileNameWithoutSuffix(path.Replace("\\", "/")) == PlatformName) { File.Delete(path); } } // 写入文件 StringBuilder txt = new StringBuilder(); foreach (var pair in CurVersionFileUrlMd5) { txt.Append(pair.Value + ":" + pair.Key + "\r"); } ABHelper.WriteFileByBytes(PlatformABExportPath + "/" + ABHelper.VersionFileName, Encoding.UTF8.GetBytes(txt.ToString().TrimEnd().ToLower())); }