Пример #1
0
    /// <summary>
    /// 对比上次打包结果,输出新增,删除和改变的部分。
    /// </summary>
    /// <param name="oldABVersionInfo">GetABVersionInfo返回的值,也就是上一次打包的结果信息</param>
    /// <param name="newMf">build assetbundle后返回的manifest对象</param>
    /// <param name="versionName">一般是平台名字,也可以加其他文件夹在</param>
    private static void CompareOldInfoAndNewManifest(Dictionary <string, string> oldABVersionInfo, AssetBundleManifest newMf, string versionName,
                                                     out Dictionary <string, string> newAB, out Dictionary <string, string> delAB, out Dictionary <string, KeyValuePair <string, string> > changedAB)
    {
        //临时记录新增的AssetBundle
        newAB = new Dictionary <string, string>();
        //临时记录删除的AssetBundle,这里返回后可以查找已经不需要存在的assetbundle,可以手动删除
        delAB = new Dictionary <string, string>();
        //临时记录改变的AssetBundle
        changedAB = new Dictionary <string, KeyValuePair <string, string> >();
        var abNames = newMf.GetAllAssetBundles().Select(key => AssetBundleSettingHelper.PathToPlatformFormat(key).ToLower());

        foreach (var name in abNames)
        {
            var newHash = newMf.GetAssetBundleHash(name).ToString();
            if (oldABVersionInfo.ContainsKey(name))
            {
                if (!oldABVersionInfo[name].Equals(newHash))
                {
                    //changedAB
                    changedAB.Add(name, new KeyValuePair <string, string>(oldABVersionInfo[name], newHash));
                }
                else
                {
                    //not changed
                }
                oldABVersionInfo.Remove(name);
            }
            else
            {
                //newAB
                newAB.Add(name, newHash);
            }
        }
        foreach (var name in oldABVersionInfo)
        {
            delAB.Add(name.Key, name.Value);
        }
        //对结果进行排序
        newAB.OrderBy(n => n);
        delAB.OrderBy(n => n);
        changedAB.OrderBy(n => n);

        //创建XML文档实例
        var abpi = ABSH.GetDifferXmlPath(versionName);
        //string filepath = ABSH.GetDifferXmlPath(versionName);
        XmlDocument xmlDoc  = new XmlDocument();
        XmlElement  AllRoot = xmlDoc.CreateElement(AssetBundleSettingHelper.xmlNode_AssetBundles);

        xmlDoc.AppendChild(AllRoot);

        //创建个时间属性,可以更直观的对比不同版本的
        AllRoot.SetAttribute(AssetBundleSettingHelper.xmlAttribute_CreateTime, VersionInfo.GetVersionString());

        XmlElement NewRoot = xmlDoc.CreateElement(AssetBundleSettingHelper.xmlNode_NewAssetBundles);

        AllRoot.AppendChild(NewRoot);
        foreach (var nAB in newAB)
        {
            XmlElement node = xmlDoc.CreateElement(AssetBundleSettingHelper.xmlNode_AB);
            node.SetAttribute(AssetBundleSettingHelper.xmlNode_Name, nAB.Key);
            node.SetAttribute(AssetBundleSettingHelper.xmlNode_Hash, nAB.Value);
            NewRoot.AppendChild(node);
        }
        XmlElement ChangedRoot = xmlDoc.CreateElement(AssetBundleSettingHelper.xmlNode_ChangedAssetBundles);

        AllRoot.AppendChild(ChangedRoot);
        foreach (var cAB in changedAB)
        {
            XmlElement node = xmlDoc.CreateElement(AssetBundleSettingHelper.xmlNode_AB);
            node.SetAttribute(AssetBundleSettingHelper.xmlNode_Name, cAB.Key);
            node.SetAttribute(AssetBundleSettingHelper.xmlNode_OldHash, cAB.Value.Key);
            node.SetAttribute(AssetBundleSettingHelper.xmlNode_NewHash, cAB.Value.Value);
            ChangedRoot.AppendChild(node);
        }
        XmlElement DelRoot = xmlDoc.CreateElement(AssetBundleSettingHelper.xmlNode_DelAssetBundles);

        AllRoot.AppendChild(DelRoot);
        foreach (var dAB in delAB)
        {
            XmlElement node = xmlDoc.CreateElement(AssetBundleSettingHelper.xmlNode_AB);
            node.SetAttribute(AssetBundleSettingHelper.xmlNode_Name, dAB.Key);
            node.SetAttribute(AssetBundleSettingHelper.xmlNode_Hash, dAB.Value);
            DelRoot.AppendChild(node);
        }
        //直接覆盖上次结果
        xmlDoc.Save(abpi.FullName);
    }
    //绘制窗口时调用
    void OnGUI()
    {
        //***************************************************************************
        GUILayout.BeginArea(new Rect(0, 0, position.width, LayoutOneHeight));
        GUILayout.Label("编辑器模式运行的过程中:", titleSytle);
        //是否使用bundle
        {
            GUILayout.BeginHorizontal();
            {
                SetTab(2);
                GUILayout.Label("编辑器模式下是否使用AssetBundle:", NormalSytle, GUILayout.Width(260));
                if (ABSH.UseAssetBundle)
                {
                    //GUI.enabled = false;
                    GUILayout.Label("使用AB", RedTextFieldSytle, GUILayout.Width(100));
                    //GUI.enabled = true;
                }
                else
                {
                    GUI.enabled = false;
                    GUILayout.Label("不使用AB", RedTextFieldSytle, GUILayout.Width(100));
                    GUI.enabled = true;
                }
                if (GUILayout.Button("更改", ResetButtonSytle, GUILayout.Width(100)))
                {
                    ABSH.UseAssetBundle = !ABSH.UseAssetBundle;
                    bDirty = true;
                }
                //AssetBundleHelper.UseAssetBundle = GUILayout.Toggle(AssetBundleHelper.UseAssetBundle, "", ToggleSytle, GUILayout.ExpandWidth(false));
            }
            GUILayout.EndHorizontal();
        }
        GUILayout.EndArea();
        //**************************************************************************
        GUILayout.BeginArea(new Rect(0, LayoutOneHeight, position.width, position.height));
        GUILayout.Label("Build AssetBundle的时候:", titleSytle);
        GUILayout.Label("本打包脚本会将制定目录下所有的资源单独打包,被引用的资源如果被共享则单独打包,未共享的被一同打包:", titleSytle);

        //输出目录
        {
            GUILayout.BeginHorizontal();
            {
                SetTab(1);
                GUILayout.Label("AssetBundle输出的目录(相对与Assets目录上一层的相对目录,修改前请思考一下):", NormalSytle);
            }
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            {
                SetTab(2);
                var EditString = (GUILayout.TextField(ABSH.ABOutputPath, TextFieldSytle, GUILayout.Width(700)));
                if (!EditString.Equals(ABSH.ABOutputPath))               //BuildBundleManager.AssetBundlePath
                {
                    ABSH.ABOutputPath = EditString;
                    bDirty            = true;
                }
                //防止不小心修改,可以重置为默认值
                if (GUILayout.Button("重置为默认", ResetButtonSytle, GUILayout.Width(100)))
                {
                    //BuildBundleManager.AssetBundlePath = AssetBundleHelper.Ins.ABSettingHelper.ABOutputPath;
                }
            }
            GUILayout.EndHorizontal();
        }
        //打包目录
        {
            GUILayout.BeginHorizontal();
            {
                SetTab(1);
                GUILayout.Label("需要build assetbundle的目录(相对与Assets目录上一层的相对目录,修改前请思考一下):", NormalSytle);
            }
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            {
                SetTab(1);
                GUILayout.Label("如果真的要改这个,请记得把AssetBundleHelper.AssetBundlePrefixPath一同修改,这样游戏内才能正确找到bundle位置:", RedNormalSytle);
            }
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();
            {
                SetTab(2);
                var EditString = (GUILayout.TextField(ABSH.NeedBuildABPath, TextFieldSytle, GUILayout.Width(700)));                //BuildBundleManager.NeedBuildAssetBundlePath
                if (!EditString.Equals(ABSH.NeedBuildABPath))
                {
                    ABSH.NeedBuildABPath = EditString;
                    bDirty = true;
                }
                //防止不小心修改,可以重置为默认值
                if (GUILayout.Button("重置为默认", ResetButtonSytle, GUILayout.Width(100)))
                {
                    //BuildBundleManager.NeedBuildAssetBundlePath = BuildBundleManager.needBuildAssetBundlePath;
                }
            }
            GUILayout.EndHorizontal();
        }
        //xml信息
        {
            //AB信息
            {
                GUILayout.BeginHorizontal();
                {
                    SetTab(1);
                    GUILayout.Label("ABInfo 的xml文件路径(相对与Assets目录上一层的相对目录,这个并不准备让你修改,但是让你看一下在哪):", NormalSytle);
                }
                GUILayout.EndHorizontal();
                //ios系列
                GUILayout.BeginHorizontal();
                {
                    SetTab(2);
                    GUILayout.Label("iOS系列:", NormalSytle);
                }
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal();
                {
                    SetTab(2);
                    GUI.enabled = false;
                    var abpi = ABSH.GetABInfoXmlPath(ABSH.RuntimePlatformToSimplifyName(RuntimePlatform.OSXEditor));
                    GUILayout.Label(abpi.FullName_RelativePath, TextFieldSytle, GUILayout.Width(700));
                    GUI.enabled = true;
                }
                GUILayout.EndHorizontal();
                //android系列
                GUILayout.BeginHorizontal();
                {
                    SetTab(2);
                    GUILayout.Label("android系列:", NormalSytle);
                }
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal();
                {
                    SetTab(2);
                    GUI.enabled = false;
                    var abpi = ABSH.GetABInfoXmlPath(ABSH.RuntimePlatformToSimplifyName(RuntimePlatform.Android));
                    GUILayout.Label(abpi.FullName_RelativePath, TextFieldSytle, GUILayout.Width(700));
                    GUI.enabled = true;
                }
                GUILayout.EndHorizontal();
                //Windows系列
                GUILayout.BeginHorizontal();
                {
                    SetTab(2);
                    GUILayout.Label("Windows系列:", NormalSytle);
                }
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal();
                {
                    SetTab(2);
                    GUI.enabled = false;
                    var abpi = ABSH.GetABInfoXmlPath(ABSH.RuntimePlatformToSimplifyName(RuntimePlatform.WindowsEditor));
                    GUILayout.Label(abpi.FullName_RelativePath, TextFieldSytle, GUILayout.Width(700));
                    GUI.enabled = true;
                }
                GUILayout.EndHorizontal();
            }
            //ABDiffer信息
            {
                GUILayout.BeginHorizontal();
                {
                    SetTab(1);
                    GUILayout.Label("ABInfo 的xml文件路径(相对与Assets目录上一层的相对目录,这个并不准备让你修改,但是让你看一下在哪):", NormalSytle);
                }
                GUILayout.EndHorizontal();
                //ios系列
                GUILayout.BeginHorizontal();
                {
                    SetTab(2);
                    GUILayout.Label("iOS系列:", NormalSytle);
                }
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal();
                {
                    SetTab(2);
                    GUI.enabled = false;
                    var abpi = ABSH.GetDifferXmlPath(ABSH.RuntimePlatformToSimplifyName(RuntimePlatform.OSXEditor));
                    GUILayout.Label(abpi.FullName_RelativePath, TextFieldSytle, GUILayout.Width(700));
                    GUI.enabled = true;
                }
                GUILayout.EndHorizontal();
                //android系列
                GUILayout.BeginHorizontal();
                {
                    SetTab(2);
                    GUILayout.Label("android系列:", NormalSytle);
                }
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal();
                {
                    SetTab(2);
                    GUI.enabled = false;
                    var abpi = ABSH.GetDifferXmlPath(ABSH.RuntimePlatformToSimplifyName(RuntimePlatform.Android));
                    GUILayout.Label(abpi.FullName_RelativePath, TextFieldSytle, GUILayout.Width(700));
                    GUI.enabled = true;
                }
                GUILayout.EndHorizontal();
                //Windows系列
                GUILayout.BeginHorizontal();
                {
                    SetTab(2);
                    GUILayout.Label("Windows系列:", NormalSytle);
                }
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal();
                {
                    SetTab(2);
                    GUI.enabled = false;
                    var abpi = ABSH.GetDifferXmlPath(ABSH.RuntimePlatformToSimplifyName(RuntimePlatform.WindowsEditor));
                    GUILayout.Label(abpi.FullName_RelativePath, TextFieldSytle, GUILayout.Width(700));
                    GUI.enabled = true;
                }
                GUILayout.EndHorizontal();
            }
        }
        GUILayout.EndArea();
        //*****************************************************************************
    }