private static bool RemoveAlllBundle(BuildTarget buildTarget)
 {
     try
     {
         string path = PathConfig.BuildAssetBundleRootDir(buildTarget);
         PathTools.RemoveDir(path);
         return(true);
     }
     catch (Exception e)
     {
         Debug.LogError("删除AssetBundle文件失败!path=" + PathConfig.BuildAssetBundleRootDir(buildTarget) + "\n" + e.Message + "\n" + e.StackTrace);
         return(false);
     }
 }
        private static bool BuildAssetBundles(BuildTarget buildTarget)
        {
            string buildPath = PathConfig.BuildAssetBundleRootDir(buildTarget);

            if (!Directory.Exists(buildPath))
            {
                Directory.CreateDirectory(buildPath);
            }
            AssetBundleManifest manifest = BuildPipeline.BuildAssetBundles(buildPath, BuildAssetBundleOptions.UncompressedAssetBundle | BuildAssetBundleOptions.ForceRebuildAssetBundle, buildTarget);

            if (manifest == null)
            {
                Debug.LogError("BuildAssetBundle Fail!");
                return(false);
            }

            return(true);
        }
 public static bool CompressABToStreamingAssetDir(BuildTarget buildTarget)
 {
     try
     {
         EditorUtility.DisplayCancelableProgressBar("Package", "正在压缩AssetBundle", 0);
         string streamingAssetDir = PathConfig.BuildStreamingAssetsRootDir(buildTarget);
         if (!Directory.Exists(streamingAssetDir))
         {
             Directory.CreateDirectory(streamingAssetDir);
         }
         string compressPath = streamingAssetDir + "/" + PathConfig.CompressAssetBundleName;
         Debug.LogError("压缩失败");
         //CompressTools.CompressDir(PathConfig.BuildOuterAssetBundleRootDir(buildTarget), compressPath);
         AssetDatabase.Refresh();
         return(true);
     }
     finally
     {
         EditorUtility.ClearProgressBar();
     }
 }
 private static bool DeleteAllManifest(BuildTarget buildTarget)
 {
     try
     {
         string        buildPath = PathConfig.BuildAssetBundleRootDir(buildTarget);
         List <string> files     = new List <string>();
         PathTools.GetAllFiles(buildPath, files, null, "*.manifest", SearchOption.AllDirectories);
         for (int i = 0; i < files.Count; i++)
         {
             if (File.Exists(files[i]))
             {
                 File.Delete(files[i]);
             }
         }
         return(true);
     }
     catch (Exception e)
     {
         Debug.LogError("删除manifest文件出错\n" + e.Message + "\n" + e.StackTrace);
         return(false);
     }
 }
 private void OnGUI()
 {
     EditorGUILayout.BeginHorizontal();
     m_nBuildTargetIdx = EditorGUILayout.Popup("构建平台", m_nBuildTargetIdx, m_arrBuildTargetDesc);;
     EditorGUILayout.EndHorizontal();
     EditorGUILayout.BeginHorizontal();
     m_bOpenFolder = EditorGUILayout.Toggle("构建完成后打开目录", m_bOpenFolder);
     EditorGUILayout.EndHorizontal();
     EditorGUILayout.BeginHorizontal();
     m_bUseMD5Name = EditorGUILayout.Toggle("AssetBundle使用MD5名字", m_bUseMD5Name);
     EditorGUILayout.EndHorizontal();
     EditorGUILayout.BeginHorizontal();
     m_nPkgVersion = EditorGUILayout.IntField("包版本号", m_nPkgVersion);
     EditorGUILayout.EndHorizontal();
     if (GUILayout.Button("开始构建AssetBundle"))
     {
         BuildTarget buildTarget = m_arrBuildTarget[m_nBuildTargetIdx];
         if (AssetBundleBuildUtil.BuildAssetBundle(buildTarget, m_bUseMD5Name))
         {
             AssetVersionUtil.GenerateVersionInfoFile(m_nPkgVersion, PathConfig.BuildOuterAssetBundleRootDir(buildTarget));
             if (m_bOpenFolder)
             {
                 //打开包所在的文件夹
                 System.Diagnostics.Process.Start(PathConfig.BuildOuterAssetBundleRootDir(buildTarget));
             }
         }
         else
         {
             Debug.LogError("AssetBundle构建失败!");
         }
     }
     if (GUILayout.Button("打开bundle依赖查找窗口(工具)"))
     {
         AssetBundleBuildUtil.ClearAssetBundleNames();
         AssetBundleBuildUtil.SetAssetBundleName(m_bUseMD5Name);
         AssetBundleBrowser.AssetBundleBrowserMain.ShowWindow();
     }
 }
        private void OnGUI()
        {
            EditorGUILayout.BeginHorizontal();
            m_nBuildTargetIdx = EditorGUILayout.Popup("构建平台", m_nBuildTargetIdx, m_arrBuildTargetDesc);;
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            m_bOpenFolder = EditorGUILayout.Toggle("构建完成后打开目录", m_bOpenFolder);
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            m_bRebuildAssetBundle = EditorGUILayout.Toggle("是否重新打AssetBundle", m_bRebuildAssetBundle);
            EditorGUILayout.EndHorizontal();
            if (m_bRebuildAssetBundle)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.Space();
                m_bUseMD5Name = EditorGUILayout.Toggle("AssetBundle使用MD5名字", m_bUseMD5Name);
                EditorGUILayout.EndHorizontal();
            }
            EditorGUILayout.BeginHorizontal();
            m_nPkgVersion = EditorGUILayout.IntField("包版本号", m_nPkgVersion);
            EditorGUILayout.EndHorizontal();

            if (m_arrOption.Length > 0)
            {
                m_arrOptionState[0] = EditorGUILayout.BeginToggleGroup(m_arrOption[0].ToString(), m_arrOptionState[0]);
                for (int i = 1; i < m_arrOptionState.Length; i++)
                {
                    m_arrOptionState[i] = EditorGUILayout.Toggle(m_arrOption[i].ToString(), m_arrOptionState[i]);
                }
                EditorGUILayout.EndToggleGroup();
            }

            EditorGUILayout.BeginHorizontal();
            m_bDeleteAllOldPackage = EditorGUILayout.Toggle("删除所有旧包文件", m_bDeleteAllOldPackage);
            EditorGUILayout.EndHorizontal();

            if (GUILayout.Button("开始打包"))
            {
                BuildTarget buildTarget = m_arrBuildTarget[m_nBuildTargetIdx];
                if (m_bDeleteAllOldPackage)
                {
                    PathTools.RemoveDir(PathConfig.BuildPackageRootDir(buildTarget));
                }
                BuildOptions buildOptions = GetBuildOptions();
                Action <BuildTarget, BuildOptions> action = null;
                if (!m_dic.TryGetValue(buildTarget, out action))
                {
                    EditorUtility.DisplayDialog("提示", "打包失败,未找到" + buildTarget + "的打包方法", "确定");
                    return;
                }
                if (m_bRebuildAssetBundle)
                {
                    if (AssetBundleBuildUtil.BuildAssetBundle(buildTarget, m_bUseMD5Name))
                    {
                        AssetVersionUtil.GenerateVersionInfoFile(m_nPkgVersion, PathConfig.BuildOuterAssetBundleRootDir(buildTarget));
                        action.Invoke(buildTarget, buildOptions);
                    }
                    else
                    {
                        EditorUtility.DisplayDialog("提示", "AssetBundle构建失败!", "确定");
                        return;
                    }
                }
                else
                {
                    if (!Directory.Exists(PathConfig.BuildOuterAssetBundleRootDir(buildTarget)))
                    {
                        EditorUtility.DisplayDialog("提示", "打包失败,未找到AssetBundle目录" + PathConfig.BuildOuterAssetBundleRootDir(buildTarget), "确定");
                        return;
                    }
                    AssetVersionUtil.GenerateVersionInfoFile(m_nPkgVersion, PathConfig.BuildOuterAssetBundleRootDir(buildTarget));
                    action.Invoke(buildTarget, buildOptions);
                }

                if ((buildOptions & BuildOptions.AutoRunPlayer) == 0)
                {
                    //如果不是自动运行,打开包目录
                    //打开包所在的文件夹
                    System.Diagnostics.Process.Start(PathConfig.BuildPackageRootDir(buildTarget));
                }
                Debug.Log("打包成功");
            }
        }
        private static bool GenerateAssetMapping(BuildTarget buildTarget)
        {
            try
            {
                string buildPath = PathConfig.BuildAssetBundleRootDir(buildTarget);
                //更改Manifest文件的名字
                string manifestPath       = buildPath + "/" + buildPath.Substring(buildPath.LastIndexOf("/") + 1);
                string targetManifestPath = buildPath + "/" + PathConfig.AssetBundleManifestName;
                if (File.Exists(manifestPath))
                {
                    File.Move(manifestPath, targetManifestPath);
                }

                string assetPathMappingPath = buildPath + "/" + PathConfig.assetPathMappingName;
                if (File.Exists(assetPathMappingPath))
                {
                    File.Delete(assetPathMappingPath);
                }
                //获取映射
                var assetBundleNames = AssetDatabase.GetAllAssetBundleNames();
                Dictionary <string, string> mapping = new Dictionary <string, string>();
                string resDir = PathTools.PathToUnityAssetPath(PathConfig.ResourceRootDir);
                for (int i = 0; i < assetBundleNames.Length; i++)
                {
                    string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundle(assetBundleNames[i]);
                    for (int j = 0; j < assetPaths.Length; j++)
                    {
                        if (assetPaths[j].StartsWith(resDir))
                        {
                            string subAssetPath = assetPaths[j].Replace(resDir + "/", "");
                            if (!mapping.ContainsKey(subAssetPath))
                            {
                                mapping.Add(subAssetPath, assetBundleNames[i]);
                            }
                            else
                            {
                                Debug.LogError(subAssetPath + "被打入了多个bundle中");
                            }
                        }
                    }
                }
                //写入xml文件
                XmlDocument    document = new XmlDocument();
                XmlDeclaration dec      = document.CreateXmlDeclaration("1.0", "UTF-8", null);
                XmlElement     root     = document.CreateElement("root");
                document.AppendChild(root);
                XmlElement manifestNode = document.CreateElement("manifest");
                manifestNode.SetAttribute("path", targetManifestPath);
                root.AppendChild(manifestNode);
                foreach (var item in mapping)
                {
                    XmlElement assetMappingNode = document.CreateElement("assetmapping");
                    assetMappingNode.SetAttribute("assetpath", item.Key);
                    assetMappingNode.SetAttribute("assetbundlepath", item.Value);
                    root.AppendChild(assetMappingNode);
                }
                XmlWriterSettings setting = new XmlWriterSettings();
                setting.Indent   = true;
                setting.Encoding = new UTF8Encoding(false);
                XmlWriter write = XmlWriter.Create(assetPathMappingPath, setting);
                document.Save(write);

                write.Flush();
                write.Close();
                return(true);
            }catch (Exception e)
            {
                Debug.LogError("产生资源映射文件失败\n" + e.Message + "\n" + e.StackTrace);
                return(false);
            }
        }