public static void Build() { m_BundleTargetPath = EditorAssetPath.ProjectPath + "AssetBundle/" + buildTarget.ToString() + "/"; AppInfoConfig appInfoConfig = AssetDatabasex.LoadAssetOfType <AppInfoConfig>("AppInfoConfig"); AF_ABConfig abConfig = AssetDatabasex.LoadAssetOfType <AF_ABConfig>("AF_ABConfig"); EditorAssetPath.InitABBuildPath(abConfig); //清空 m_ConfigFile.Clear(); m_AllFileAB.Clear(); m_AllFileDir.Clear(); m_AllPrefabDir.Clear(); string mTargetPath = EditorAssetPath.ABTargetPath; string mInfoPath = EditorAssetPath.ABInfoPath; if (abConfig.packageABType != PackageABType.PhoneAB) { mTargetPath = EditorAssetPath.ABTargetPath + buildTarget.ToString() + "/"; mInfoPath = EditorAssetPath.ABInfoPath + buildTarget.ToString() + "/"; } if (Directory.Exists(mTargetPath)) { FileUtil.DeleteFileOrDirectory(mTargetPath); } if (Directory.Exists(mInfoPath)) { FileUtil.DeleteFileOrDirectory(mInfoPath); } Directory.CreateDirectory(mInfoPath); Directory.CreateDirectory(mTargetPath); Directory.CreateDirectory(m_BundleTargetPath); //创建APP版本文件 FileHelper.CreatFile(EditorAssetPath.AppInfoPath, SerializeHelper.ToByteArray(appInfoConfig), true); Dictionary <string, bool> ClassToNeedPackageAB = new Dictionary <string, bool>(); for (int i = 0; i < abConfig.m_AllClass.Count; i++) { if (abConfig.m_AllClass[i].isSameAppType(abConfig.CurrentAppType)) { ClassToNeedPackageAB.Add(abConfig.m_AllClass[i].ABClassType, abConfig.m_AllClass[i].isNeedPackageAB); } } //剔除重复路径,将单个文件及文件夹的路径保存下来 //先处理文件夹再处理单个文件,因为有可能prefab依赖某个文件中的东西,所以要进行过滤 foreach (AF_OneAB oneAB in abConfig.m_AllFileAB) { bool isNeedPackageAB = false; for (int k = 0; k < oneAB.CategoryOfOwnership.Count; k++) { if (ClassToNeedPackageAB.ContainsKey(oneAB.CategoryOfOwnership[k]) && ClassToNeedPackageAB[oneAB.CategoryOfOwnership[k]]) { isNeedPackageAB = true; break; } } if (!isNeedPackageAB) { continue; } if (m_AllFileDir.ContainsKey(oneAB.mABIdentifier)) { AFLogger.EditorErrorLog("AB包配置名字重复,请检查!"); EditorUtility.ClearProgressBar(); } else { m_AllFileDir.Add(oneAB.mABIdentifier, oneAB.Path); //保存已打包的AB包路径 m_AllFileAB.Add(oneAB.Path); m_ConfigFile.Add(oneAB.Path); } } string[] AllPrefabsPath = abConfig.GetAllPrefabsPath(); if (AllPrefabsPath.Length > 0) { //获取路径下的所有prefab string[] allStr = AssetDatabase.FindAssets("t:Prefab", AllPrefabsPath); AFLogger.EditorInfoLog("获取的路径长度:" + allStr.Length); for (int i = 0; i < allStr.Length; i++) { bool isNeedPackageAB = false; for (int k = 0; k < abConfig.m_AllPrefabAB[i].CategoryOfOwnership.Count; k++) { if (ClassToNeedPackageAB.ContainsKey(abConfig.m_AllPrefabAB[i].CategoryOfOwnership[k]) && ClassToNeedPackageAB[abConfig.m_AllPrefabAB[i].CategoryOfOwnership[k]]) { isNeedPackageAB = true; break; } } if (!isNeedPackageAB) { continue; } //GUID转asset路径 string path = AssetDatabase.GUIDToAssetPath(allStr[i]); EditorUtility.DisplayProgressBar("查找Prefab", "Prefab:" + path, i * 1.0f / allStr.Length); m_ConfigFile.Add(path); if (!ContainAllFileAB(path)) { //获取资源的所有依赖 string[] allDepend = AssetDatabase.GetDependencies(path); List <string> allDependPath = new List <string>(); //遍历依赖文件 for (int j = 0; j < allDepend.Length; j++) { if (!ContainAllFileAB(allDepend[j]) && !allDepend[j].EndsWith(".cs", System.StringComparison.Ordinal)) { m_AllFileAB.Add(allDepend[j]); allDependPath.Add(allDepend[j]); } } if (m_AllPrefabDir.ContainsKey(abConfig.m_AllPrefabAB[i].mABIdentifier)) { AFLogger.EditorErrorLog("存在相同名字的Prefab!名字:" + abConfig.m_AllPrefabAB[i].mABIdentifier); EditorUtility.ClearProgressBar(); } else { m_AllPrefabDir.Add(abConfig.m_AllPrefabAB[i].mABIdentifier, allDependPath); } } } } //点击打包之后可以看见更改的文件的.meta文件改变 //文件夹设置ABName foreach (string name in m_AllFileDir.Keys) { SetABName(name, m_AllFileDir[name]); } //单个文件设置包名 foreach (string name in m_AllPrefabDir.Keys) { SetABName(name, m_AllPrefabDir[name]); } //打包 BunildAssetBundle(abConfig); //清除AB包名,因为上面更改了.meta文件,我们很多时候会使用svn或者git,有可能会导致一不小心上传特别乱 string[] oldABNames = AssetDatabase.GetAllAssetBundleNames(); for (int i = 0; i < oldABNames.Length; i++) { AssetDatabase.RemoveAssetBundleName(oldABNames[i], true); EditorUtility.DisplayProgressBar("清除AB包名", "名字:" + oldABNames[i], i * 1.0f / oldABNames.Length); } EditorUtility.ClearProgressBar(); //根据设置生成文件 ABBuildFinishEvent.BundleFinish(mTargetPath, m_BundleTargetPath, abConfig, mInfoPath, appInfoConfig); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); EditorUtility.ClearProgressBar(); }