Exemplo n.º 1
0
    private void ShowBuildUI(string platform)
    {
        versionPart = EditorGUILayout.Foldout(versionPart, "已打版本");
        if (versionPart)
        {
            var versionList = GetVersions(m_ExportAssetBundleDir + platform + "/Version");
            if (versionList != null && versionList.Count > 0)
            {
                GUILayoutOption[] options =
                {
                    GUILayout.Width(150),
                    GUILayout.Height(20),
                };

                EditorGUILayout.BeginVertical();
                EditorGUILayout.Space();

                foreach (var v in versionList)
                {
                    if (!m_VersionMap.ContainsKey(v.ToString()))
                    {
                        m_VersionMap.Add(v.ToString(), -1);
                    }

                    var selectIndex = m_VersionMap[v.ToString()];
                    selectIndex = GUILayout.SelectionGrid(selectIndex, new string[] { v.ToString() }, 1);
                    m_VersionMap[v.ToString()] = selectIndex;

                    if (selectIndex == 0)
                    {
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.Space();

                        if (GUILayout.Button("浏览", options))
                        {
                            WindowsOSUtility.ExploreFile(m_ExportAssetBundleDir + platform + "/Version/" + v.ToString());
                        }

                        if (GUILayout.Button("检查ab包大小", options))
                        {
                            AssetBundleBuilder.CheckAssetBundleSize(m_ExportAssetBundleDir + platform + "/Version/" + v, ".pkg");
                        }

                        if (GUILayout.Button("取消查看", options))
                        {
                            m_VersionMap[v.ToString()] = -1;
                            Repaint();
                        }

                        if (GUILayout.Button("拷贝到StreamingAssets", options))
                        {
                            var targetPath = Application.dataPath + "/StreamingAssets/" + UGCoreConfig.MiddleFilePathName;
                            EditorFileUtility.ClearDirectory(targetPath);
                            EditorFileUtility.CopyDirectory(m_ExportAssetBundleDir + platform + "/Version/" + v.ToString(), targetPath);
                            AssetDatabase.Refresh();
                            WindowsOSUtility.ExploreFile(targetPath);
                        }

                        if (GUILayout.Button("拷贝到persistentDataPath", options))
                        {
                            var targetPath = Application.persistentDataPath + "/" + UGCoreConfig.MiddleFilePathName + "/" + UGCoreConfig.ResourcesFolderName;
                            EditorFileUtility.ClearDirectory(targetPath);
                            EditorFileUtility.CopyDirectory(m_ExportAssetBundleDir + platform + "/Version/" + v.ToString(), targetPath);
                            AssetDatabase.Refresh();
                            WindowsOSUtility.ExploreFile(targetPath);
                        }

                        if (GUILayout.Button("删除版本", options))
                        {
                            Directory.Delete(m_ExportAssetBundleDir + platform + "/Version/" + v.ToString(), true);
                            Repaint();
                        }
                        EditorGUILayout.Space();
                        EditorGUILayout.EndHorizontal();
                        EditorGUILayout.Space();
                    }
                }

                EditorGUILayout.Space();
                EditorGUILayout.EndVertical();
            }
        }

        patchPart = EditorGUILayout.Foldout(patchPart, "已打补丁");
        if (patchPart)
        {
            var patchList = GetPatches(m_ExportAssetBundleDir + platform + "/Patches");
            if (patchList != null && patchList.Count > 0)
            {
                GUILayoutOption[] options =
                {
                    GUILayout.Width(150),
                    GUILayout.Height(20),
                };

                EditorGUILayout.BeginVertical();
                EditorGUILayout.Space();

                foreach (var p in patchList)
                {
                    if (!m_VersionMap.ContainsKey(p.ToString()))
                    {
                        m_VersionMap.Add(p.ToString(), -1);
                    }

                    var selectIndex = m_VersionMap[p.ToString()];
                    selectIndex = GUILayout.SelectionGrid(selectIndex, new string[] { p.ToString() }, 1);
                    m_VersionMap[p.ToString()] = selectIndex;

                    if (selectIndex == 0)
                    {
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.Space();

                        if (GUILayout.Button("浏览", options))
                        {
                            WindowsOSUtility.ExploreFile(m_ExportAssetBundleDir + platform + "/Patches/" + p);
                        }

                        if (GUILayout.Button("取消查看", options))
                        {
                            m_VersionMap[p.ToString()] = -1;
                            Repaint();
                        }

                        if (GUILayout.Button("删除补丁", options))
                        {
                            Directory.Delete(m_ExportAssetBundleDir + platform + "/Patches/" + p, true);
                            Repaint();
                        }

                        EditorGUILayout.Space();
                        EditorGUILayout.EndHorizontal();
                        EditorGUILayout.Space();
                    }
                }

                EditorGUILayout.Space();
                EditorGUILayout.EndVertical();
            }
        }

        GUIContent[] guiBuildObjs =
        {
            new GUIContent("打版本"),
            new GUIContent("打补丁"),
        };

        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();
        m_BuildOperation = (BuildOperation)GUILayout.Toolbar((int)m_BuildOperation, guiBuildObjs);
        GUILayout.FlexibleSpace();
        GUILayout.EndHorizontal();

        if (m_BuildOperation == BuildOperation.Version)
        {
            m_CreateVersion = EditorGUILayout.TextField("打包版本号:", m_CreateVersion);

            GUILayoutOption[] options =
            {
                GUILayout.Width(150),
                GUILayout.Height(26),
            };
            GUILayout.FlexibleSpace();
            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("重新打包版本(慎用)", options))
            {
                var versionNumber = ValidVersion(m_CreateVersion);
                if (versionNumber == null)
                {
                    GetWindow <AssetBundleBuildEditor>().ShowNotification(new GUIContent("版本名称不符合规范,请重新输入"));
                    return;
                }

                if (!Directory.Exists(m_ExportAssetBundleDir + platform + "/Version/" + versionNumber))
                {
                    GetWindow <AssetBundleBuildEditor>().ShowNotification(new GUIContent("版本号未经过打包,请使用打包版本选项来打包"));
                    return;
                }

                CreateVersion(m_ExportAssetBundleDir + platform + "/Version/" + versionNumber, true);
                WindowsOSUtility.ExploreFile(m_ExportAssetBundleDir + platform + "/Version/" + versionNumber);
            }
            if (GUILayout.Button("打包版本", options))
            {
                var versionNumber = ValidVersion(m_CreateVersion);
                if (versionNumber == null)
                {
                    GetWindow <AssetBundleBuildEditor>().ShowNotification(new GUIContent("版本名称不符合规范,请重新输入"));
                    return;
                }

                if (Directory.Exists(m_ExportAssetBundleDir + platform + "/Version/" + versionNumber))
                {
                    GetWindow <AssetBundleBuildEditor>().ShowNotification(new GUIContent("版本目录已经存在,请选择新的版本号或者清除现有的版本号目录"));
                    return;
                }

                var versionList = GetVersions(m_ExportAssetBundleDir + platform + "/Version");
                if (versionList != null && versionList.Count > 0)
                {
                    if (VersionNumber.CompareTo(versionList[versionList.Count - 1], versionNumber) > 0)
                    {
                        GetWindow <AssetBundleBuildEditor>().ShowNotification(new GUIContent("当前版本号低于最新的版本号,无法打包,请重新选择版本号"));
                        return;
                    }
                }

                CreateVersion(m_ExportAssetBundleDir + platform + "/Version/" + versionNumber);
                WindowsOSUtility.ExploreFile(m_ExportAssetBundleDir + platform + "/Version/" + versionNumber);
            }
            GUILayout.EndHorizontal();
        }
        else if (m_BuildOperation == BuildOperation.Patch)
        {
            var versionList = GetVersions(m_ExportAssetBundleDir + platform + "/Version");
            if (versionList != null && versionList.Count > 0)
            {
                List <string> versionStringList = new List <string>();
                foreach (var v in versionList)
                {
                    versionStringList.Add(v.ToString());
                }
                GUILayout.BeginHorizontal();
                EditorGUILayout.Space();

                GUILayout.BeginVertical();
                EditorGUILayout.LabelField("低版本");
                m_lowVersion = EditorGUILayout.Popup(m_lowVersion, versionStringList.ToArray());
                GUILayout.EndVertical();

                GUILayout.FlexibleSpace();

                GUILayout.BeginVertical();
                EditorGUILayout.LabelField("高版本");
                m_highVersion = EditorGUILayout.Popup(m_highVersion, versionStringList.ToArray());
                GUILayout.EndVertical();

                EditorGUILayout.Space();
                GUILayout.EndHorizontal();

                GUILayoutOption[] options =
                {
                    GUILayout.Width(150),
                    GUILayout.Height(26),
                };
                GUILayout.FlexibleSpace();
                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();

                if (GUILayout.Button("打补丁", options))
                {
                    if (VersionNumber.CompareTo(versionList[m_lowVersion], versionList[m_highVersion]) >= 0)
                    {
                        GetWindow <AssetBundleBuildEditor>().ShowNotification(new GUIContent("低版本号不能大于等于高版本号"));
                        return;
                    }

                    var patchName = versionList[m_lowVersion] + "-" + versionList[m_highVersion];

                    var fileMd5LowVersion =
                        AssetBundleBuilder.GetFilesMD5(
                            m_ExportAssetBundleDir + platform + "/Version/" + versionList[m_lowVersion], ".pkg");

                    var fileMd5HighVersion =
                        AssetBundleBuilder.GetFilesMD5(
                            m_ExportAssetBundleDir + platform + "/Version/" + versionList[m_highVersion], ".pkg");

                    Dictionary <string, string> lowVersionAssets = new Dictionary <string, string>();
                    foreach (var v in fileMd5LowVersion)
                    {
                        var str = v.Key.Replace(Path.GetFullPath(m_ExportAssetBundleDir + platform + "/Version/" + versionList[m_lowVersion] + "/"), "");
                        lowVersionAssets.Add(str, v.Value);
                    }

                    Dictionary <string, string> highVersionAssets = new Dictionary <string, string>();
                    foreach (var v in fileMd5HighVersion)
                    {
                        var str = v.Key.Replace(Path.GetFullPath(m_ExportAssetBundleDir + platform + "/Version/" + versionList[m_highVersion] + "/"), "");
                        highVersionAssets.Add(str, v.Value);
                    }

                    Dictionary <string, string> resultAssets = new Dictionary <string, string>();
                    foreach (var v in highVersionAssets)
                    {
                        if (!lowVersionAssets.ContainsKey(v.Key))
                        {
                            resultAssets.Add(v.Key, v.Value);
                        }
                        else
                        {
                            if (lowVersionAssets[v.Key] != v.Value)
                            {
                                resultAssets.Add(v.Key, v.Value);
                            }
                        }
                    }

                    if (resultAssets.Count == 0)
                    {
                        GetWindow <AssetBundleBuildEditor>().ShowNotification(new GUIContent("两个版本之间没有差异,无需生成补丁"));
                        return;
                    }

                    EditorFileUtility.ClearDirectory(m_ExportAssetBundleDir + platform + "/Patches/" + patchName);

                    foreach (var v in resultAssets)
                    {
                        var sourceFileName = m_ExportAssetBundleDir + platform + "/Version/" +
                                             versionList[m_highVersion] + "/" + v.Key;
                        var targetFileName = m_ExportAssetBundleDir + platform + "/Patches/" + patchName + "/" + v.Key;
                        if (!Directory.Exists(Path.GetDirectoryName(targetFileName)))
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(targetFileName));
                        }

                        File.Copy(sourceFileName, targetFileName);
                    }

                    var targetPath = m_ExportAssetBundleDir + platform + "/Patches/" + patchName + "/";
                    AssetBundleBuilder.PackPatches(targetPath, patchName, targetPath, ".pkg");
                    WindowsOSUtility.ExploreFile(targetPath);
                }
                GUILayout.EndHorizontal();
            }
        }

        if (m_OldBuildOperation != m_BuildOperation)
        {
            m_lowVersion  = 0;
            m_highVersion = 0;
        }

        m_OldBuildOperation = m_BuildOperation;
    }