/// <summary> /// 加载资源 /// </summary> /// <param name="resourcePath"></param> /// <param name="assetName"></param> /// <param name="type"></param> /// <returns></returns> public static UnityEngine.Object LoadObject(string resourcePath, string assetName, Type type) { if (ABHelper.IgnoreHotfix || null == ABVersion.CurVersionInfo || !ABVersion.CurVersionInfo.IsValid) { return(null); } UnityEngine.Object obj = null; // 检测是否从ab加载 string abPath = resourcePath + ".ab"; TrySyncLoadFromAB(abPath, assetName, type, out obj); if (null == obj) { abPath = ABHelper.GetFileFolderPath(resourcePath) + ".ab"; TrySyncLoadFromAB(abPath, assetName, type, out obj); } return(obj); }
/// <summary> /// 异步加载资源 /// </summary> /// <param name="resourcePath"></param> /// <param name="assetName"></param> /// <param name="type"></param> /// <param name="complete"></param> /// <param name="progress"></param> /// <returns></returns> public static bool AsyncLoadObject(string resourcePath, string assetName, Type type, Action <string, UnityEngine.Object> complete, Action <float> progress = null) { if (ABHelper.IgnoreHotfix || null == ABVersion.CurVersionInfo || !ABVersion.CurVersionInfo.IsValid) { return(false); } bool loadFromAb = false; // 检测是否从ab加载 string abPath = resourcePath + ".ab"; loadFromAb = TryAsyncLoadFromAB(abPath, assetName, type, complete, progress); if (!loadFromAb) { abPath = ABHelper.GetFileFolderPath(resourcePath) + ".ab"; loadFromAb = TryAsyncLoadFromAB(abPath, assetName, type, complete, progress); } return(loadFromAb); }
/// <summary> /// fgui独立管理ab,接口单独拎出来! /// </summary> public static void LoadUI(string uipath) { AssetBundle ab = null; // 检测是否存在ab if (null != ABVersion.CurVersionInfo && ABVersion.CurVersionInfo.IsValid) { string abname = ABHelper.GetFileFolderPath(uipath) + ".ab"; string uiFullPath = ABVersion.CurVersionInfo.GetABFullPath(abname); if (!string.IsNullOrEmpty(uiFullPath)) { ab = AssetBundle.LoadFromFile(uiFullPath); } } UIPackage pkg = UIPackage.GetByName(uipath); if (null == pkg) { if (null == ab) { pkg = UIPackage.AddPackage(uipath); } else { pkg = UIPackage.AddPackage(ab); } } else { if (null == ab) { pkg.ReloadAssets(); } else { pkg.ReloadAssets(ab); } } }
/// <summary> /// 获取assetBundle /// </summary> /// <param name="path"></param> /// <returns></returns> public static AssetBundle GetUIAssetBundle(string path) { if (ABHelper.IgnoreHotfix || null == ABVersion.CurVersionInfo || !ABVersion.CurVersionInfo.IsValid) { return(null); } AssetBundle ab = null; // 检测是否存在ab bool fromNativePath = true; string abname = ABHelper.GetFileFolderPath(path).ToLower() + ".ab"; string uiFullPath = ABVersion.CurVersionInfo.GetABFullPath(abname, ref fromNativePath); if (!string.IsNullOrEmpty(uiFullPath)) { Debug.Log("加载ab:" + abname); ab = AssetBundle.LoadFromFile(uiFullPath); } return(ab); }
public static void AsyncLoad(string resourcePath, string assetName, Type type, Action <UnityEngine.Object> complete, Action <float> progress = null) { bool loadFromAb = false; // 检测是否存在ab if (null != ABVersion.CurVersionInfo && ABVersion.CurVersionInfo.IsValid) { // 检测是否从ab加载 string abPath = resourcePath + ".ab"; loadFromAb = TryAsyncLoadFromAB(abPath, assetName, type, complete, progress); if (!loadFromAb) { abPath = ABHelper.GetFileFolderPath(resourcePath) + ".ab"; loadFromAb = TryAsyncLoadFromAB(abPath, assetName, type, complete, progress); } } if (!loadFromAb) { UnityEngine.Object obj = Resources.Load(resourcePath, type); complete(obj); } }
// 加载资源 public static UnityEngine.Object Load(string resourcePath, string assetName, Type type) { UnityEngine.Object obj = null; // 检测是否存在ab if (null != ABVersion.CurVersionInfo && ABVersion.CurVersionInfo.IsValid) { // 检测是否从ab加载 string abPath = resourcePath + ".ab"; TrySyncLoadFromAB(abPath, assetName, type, out obj); if (null == obj) { abPath = ABHelper.GetFileFolderPath(resourcePath) + ".ab"; TrySyncLoadFromAB(abPath, assetName, type, out obj); } } if (null == obj) { obj = Resources.Load(resourcePath, type); } return(obj); }
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; string relativePath = ABVersion.CurVersionInfo.GetAbRelativePath(pair.Key); 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 static bool CreatABPacker(BuildTarget platform) { List <string> updateFileList = new List <string>(); List <string> updateScriptList = new List <string>(); Dictionary <string, string> lastVersionMd5List = ABHelper.ReadMd5File(PlatformABExportPath + "/" + (CurVersionNum - 1) + "/" + ABHelper.Md5FileName); Dictionary <string, List <string> > lastVersionDependenciesList = ABHelper.ReadDependFile(PlatformABExportPath + "/" + (CurVersionNum - 1) + "/" + ABHelper.DependFileName); foreach (KeyValuePair <string, List <string> > pair in CurVersionDependenciesList) { string pathName = pair.Key; if (!lastVersionDependenciesList.ContainsKey(pathName.ToLower())) { if (IsScriptFileRes(pathName)) { updateScriptList.Add(pathName); } else { updateFileList.Add(pathName); } continue; } foreach (string depend in pair.Value) { if (!lastVersionDependenciesList[pathName.ToLower()].Contains(depend.ToLower())) { if (IsScriptFileRes(pathName)) { updateScriptList.Add(pathName); } else { updateFileList.Add(pathName); } break; } else if (CurVersionMd5List[depend].ToLower() != lastVersionMd5List[depend.ToLower()]) { if (IsScriptFileRes(pathName)) { updateScriptList.Add(pathName); } else { updateFileList.Add(pathName); } break; } } } // 检测cs和dll脚本 if (updateScriptList.Count > 0) { StringBuilder sb = new StringBuilder(); foreach (var pair in updateScriptList) { sb.AppendLine(pair); } if (EditorUtility.DisplayDialog("提示", "检测到有cs等脚本的更新!!\n “停止”则中断打包,“忽略”则继续。如下:\n" + sb, "停止", "忽略")) { throw new Exception("打包中断!!!!!!!!!!"); } } if (updateFileList.Count > 0) { ClearAssetBundlesName(); AssetDatabase.Refresh(); foreach (string path in updateFileList) { string fileName; switch (CurVersionFileType[path]) { case "0": foreach (string path2 in CurVersionDependenciesList[path]) { fileName = (ABHelper.GetFileFolderPath(path2).Replace(ResFolder, "")) + ".ab"; fileName = CreatFileUrlMd5(fileName); AssetImporter.GetAtPath(path2).assetBundleName = fileName; } break; case "1": CopyLuaFiles(path); break; case "2": fileName = (ABHelper.GetFileFullPathWithoutFtype(path).Replace(ResFolder, "")) + ".ab"; fileName = CreatFileUrlMd5(fileName); AssetImporter.GetAtPath(path).assetBundleName = fileName; break; case "3": fileName = (ABHelper.GetFileFullPathWithoutFtype(path).Replace(ResFolder, "")) + ".ab"; fileName = CreatFileUrlMd5(fileName); AssetImporter.GetAtPath(path).assetBundleName = fileName; break; default: break; } } try { // 生成ab文件 BuildPipeline.BuildAssetBundles(CurVersionABExportPath, BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle, platform); } catch (Exception e) { BuildFailure(); Debug.LogError("打包异常!!" + e.Message); } System.Threading.Thread.Sleep(1000); ClearAssetBundlesName(); AssetDatabase.Refresh(); } else { BuildFailure(); } bool result = updateFileList.Count > 0; if (result) { // 删除不用的manifest List <string> allFiles = ABHelper.GetAllFilesPathInDir(PlatformABExportPath); foreach (string path in allFiles) { if (path.EndsWith(".manifest")) { File.Delete(path); } if (path.EndsWith(CurVersionNum.ToString())) { File.Delete(path); } } } return(result); }
private static void CreatFileDependencies() { List <string> scriptPathList = new 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 ScriptFolderPath) { string fullPath = AssetFolder + path; scriptPathList.AddRange(ABHelper.GetAllFilesPathInDir(fullPath)); } 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)); } CurVersionFileType = new Dictionary <string, string>(); CurVersionDependenciesList = new Dictionary <string, List <string> >(); CurVersionManifestList = new Dictionary <string, List <string> >(); CurVersionMd5List = new Dictionary <string, string>(); foreach (string path in scriptPathList) { if (!IsScriptFileRes(path)) { continue; } string filePath = path.Replace("\\", "/"); if (!CurVersionMd5List.ContainsKey(filePath)) { CurVersionMd5List.Add(filePath, ABHelper.BuildMD5ByFile(filePath)); } if (!CurVersionDependenciesList.ContainsKey(filePath)) { CurVersionDependenciesList.Add(filePath, new List <string>()); CurVersionFileType.Add(filePath, "4"); } CurVersionDependenciesList[filePath].Add(filePath); } foreach (string path in folderBundlePathList) { if (!IsNeedFileRes(path)) { continue; } string filePath = path.Replace("\\", "/"); if (!CurVersionMd5List.ContainsKey(filePath)) { CurVersionMd5List.Add(filePath, ABHelper.BuildMD5ByFile(filePath)); } string filePath2 = ABHelper.GetFileFolderPath(filePath); if (!CurVersionDependenciesList.ContainsKey(filePath2)) { CurVersionDependenciesList.Add(filePath2, new List <string>()); CurVersionFileType.Add(filePath2, "0"); } CurVersionDependenciesList[filePath2].Add(filePath); } foreach (string path in luaPathList) { if (!path.EndsWith(".lua")) { continue; } string filePath = path.Replace("\\", "/"); if (!CurVersionMd5List.ContainsKey(filePath)) { CurVersionMd5List.Add(filePath, ABHelper.BuildMD5ByFile(filePath)); } if (!CurVersionDependenciesList.ContainsKey(filePath)) { CurVersionDependenciesList.Add(filePath, new List <string>()); CurVersionFileType.Add(filePath, "1"); } CurVersionDependenciesList[filePath].Add(filePath); } foreach (string path in fileBundlePathList) { if (!IsNeedFileRes(path)) { continue; } string filePath = path.Replace("\\", "/"); string[] dependPaths = AssetDatabase.GetDependencies(filePath); if (dependPaths.Length > 0) { CurVersionDependenciesList.Add(filePath, new List <string>(dependPaths)); CurVersionFileType.Add(filePath, "2"); foreach (string path1 in dependPaths) { if (!CurVersionMd5List.ContainsKey(path1)) { CurVersionMd5List.Add(path1, ABHelper.BuildMD5ByFile(path1)); } if (path1.Contains(ResFolder) && filePath != path1) { if (!CurVersionManifestList.ContainsKey(filePath)) { CurVersionManifestList.Add(filePath, new List <string>()); } CurVersionManifestList[filePath].Add(path1); } } } } foreach (string path in sceneBundlePathList) { if (!path.EndsWith(".unity")) { continue; } string filePath = path.Replace("\\", "/"); string[] dependPaths = AssetDatabase.GetDependencies(filePath); if (dependPaths.Length > 0) { CurVersionDependenciesList.Add(filePath, new List <string>(dependPaths)); CurVersionFileType.Add(filePath, "3"); foreach (string path1 in dependPaths) { if (!CurVersionMd5List.ContainsKey(path1)) { CurVersionMd5List.Add(path1, ABHelper.BuildMD5ByFile(path1)); } if (path1.Contains(ResFolder) && filePath != path1) { if (!CurVersionManifestList.ContainsKey(filePath)) { CurVersionManifestList.Add(filePath, new List <string>()); } CurVersionManifestList[filePath].Add(path1); } } } } // 版本所需资源的md5码保存 ABHelper.WriteMd5File(CurVersionABExportPath + ABHelper.Md5FileName, CurVersionMd5List); // 文件的依赖关系 ABHelper.WriteDependFile(CurVersionABExportPath + ABHelper.DependFileName, CurVersionDependenciesList); }
private void DoUnZip() { if (!File.Exists(zipFilePath)) { if (null != unzipComplete) { unzipComplete(); } return; } fileStream = File.OpenRead(zipFilePath); ZipInputStream _zipInputStream = new ZipInputStream(fileStream); ZipEntry theEntry = null; zipFileSize = null == zipFileSize ? fileStream.Length : zipFileSize; bool unzipsuc = true; long unzipSize = 0; float zipfilesize = null == unzipFileSize ? (float)zipFileSize : (float)unzipFileSize; // fileStream.Length; string releaseFolder = localFolder + "/"; string errorMsg = ""; List <string> decompressfiles = new List <string>(); Debug.Log("<color=#20F856>" + "解压文件: " + zipFilePath + " ----- fileName:" + zipFileName + " ---- zipfie size:" + zipfilesize + "</color>"); while (true) { try { theEntry = _zipInputStream.GetNextEntry(); } catch (Exception e) { errorMsg = e.Message; unzipsuc = false; break; } if (theEntry == null) { break; } if (theEntry.Name.EndsWith("/")) { continue; } // 文件存放位置 string fileName = releaseFolder + theEntry.Name; string directoryName = ABHelper.GetFileFolderPath(fileName); decompressfiles.Add(fileName); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } if (File.Exists(fileName)) { File.Delete(fileName); } // 写入文件 FileStream sw = File.Create(fileName); int readSize = 0; byte[] data = new byte[2048]; while (true) { if (theEntry.Size == 0) { break; } readSize = _zipInputStream.Read(data, 0, data.Length); unzipSize += readSize; if (readSize > 0) { UnzipProcess = unzipSize / zipfilesize; if (null != unzipProcess) { unzipProcess(UnzipProcess); } sw.Write(data, 0, readSize); } else { break; } } sw.Close(); sw.Dispose(); } fileStream.Close(); fileStream.Dispose(); fileStream = null; // 删除源文件 if (isDeleteZip && File.Exists(zipFilePath)) { File.Delete(zipFilePath); } // 解压成功 if (unzipsuc) { UnzipProcess = 1; if (null != unzipComplete) { unzipComplete(); } } else { if (null != handleError) { handleError("unzip error :" + errorMsg); } } }
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())); }
private void DoUnZip() { if (fileSuffix != ".zip") { Debug.Log("该文件不是zip文件,当前仅支持下载zip文件解压!!"); return; } if (!File.Exists(FilePath)) { if (null != OnUnzipComplete) { OnUnzipComplete(); } return; } FileStream fs = File.OpenRead(FilePath); ZipInputStream _zipInputStream = new ZipInputStream(fs); ZipEntry theEntry = null; bool unzipsuc = true; long unzipSize = 0; float zipfilesize = null == UnZipWholeSize ? (float)FileWholeSize : (float)UnZipWholeSize; // fs.Length; string tempFolder = localFolder + "/Temp/"; string releaseFolder = localFolder + "/"; string errorMsg = ""; List <string> decompressfiles = new List <string>(); Debug.Log("<color=#20F856>" + "解压文件: " + FilePath + " ----- fileName:" + FileName + " ---- zipfie size:" + zipfilesize + "</color>"); while (true) { try { theEntry = _zipInputStream.GetNextEntry(); } catch (Exception e) { errorMsg = e.Message; unzipsuc = false; break; } if (theEntry == null) { break; } if (theEntry.Name.EndsWith("/")) { continue; } // 文件存放位置 string fileName = tempFolder + theEntry.Name; string directoryName = ABHelper.GetFileFolderPath(fileName); decompressfiles.Add(fileName); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } if (File.Exists(fileName)) { File.Delete(fileName); } // 写入文件 FileStream sw = File.Create(fileName); int readSize = 0; byte[] data = new byte[2048]; while (true) { if (theEntry.Size == 0) { break; } readSize = _zipInputStream.Read(data, 0, data.Length); unzipSize += readSize; if (readSize > 0) { UnzipProcess = unzipSize / zipfilesize; if (null != OnUnzipProcess) { OnUnzipProcess(UnzipProcess); } sw.Write(data, 0, readSize); } else { break; } } sw.Close(); sw.Dispose(); } fs.Close(); fs.Dispose(); if (File.Exists(FilePath)) { File.Delete(FilePath); } // 解压成功 if (unzipsuc) { UnzipProcess = 1; foreach (var upfile in decompressfiles) { string newfile = upfile.Replace(tempFolder, releaseFolder); if (File.Exists(newfile)) { File.Delete(newfile); } string dir = Path.GetDirectoryName(newfile); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } File.Move(upfile, newfile); } if (Directory.Exists(tempFolder)) { Directory.Delete(tempFolder, true); } if (null != OnUnzipComplete) { OnUnzipComplete(); } } else { if (null != OnHandleError) { OnHandleError("unzip error :" + errorMsg); } } }