Example #1
0
    /// <summary>
    /// 根据build assetbundle后返回的AssetBundleManifest 生成一个本次AssetBundle的信息文件,包含所有的AssetBundle文件名与其hash值。
    /// </summary>
    /// <param name="mf">build assetbundle后返回的manifest对象</param>
    /// <param name="versionName">一般是平台名字,也可以加其他文件夹在</param>
    private static void GenABVersionInfo(AssetBundleManifest mf, string versionName)
    {
        var         abpi   = ABSH.GetABInfoXmlPath(versionName);
        XmlDocument xmlDoc = null;

        //创建XML文档实例
        xmlDoc = new XmlDocument();
        XmlElement AllRoot = xmlDoc.CreateElement(AssetBundleSettingHelper.xmlNode_AssetBundles);

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

        //输出结果按名字排序,以后要对比两个文件也方面一些
        var abNames = mf.GetAllAssetBundles().OrderBy(n => n).Select(key => AssetBundleSettingHelper.PathToPlatformFormat(key).ToLower());
        List <KeyValuePair <long, XmlElement> > allE = new List <KeyValuePair <long, XmlElement> >();

        foreach (var abName in abNames)
        {
            //bundle大小也输出一下
            FileInfo fi = new FileInfo(AssetBundleSettingHelper.Combine(abpi.Dir_Relative, abName));
            {
                //把bundle大小也写到AB依赖中
                string name = abName.Substring(0, abName.IndexOf(ABSH.AssetBundelExtName));
                var    drac = ABDependenciesPositive.GetDRAC(name, true);
                if (drac != null)
                {
                    drac.FinalSize = fi.Length;
                }
            }
            var        hash = mf.GetAssetBundleHash(abName);
            XmlElement node = xmlDoc.CreateElement(AssetBundleSettingHelper.xmlNode_AB);
            node.SetAttribute(AssetBundleSettingHelper.xmlNode_Name, abName);
            node.SetAttribute(AssetBundleSettingHelper.xmlNode_Hash, hash.ToString());
            node.SetAttribute(AssetBundleSettingHelper.xmlNode_ABSize, fi.Length.ToString());
            if (fi.Length < 1024)
            {
                node.SetAttribute(AssetBundleSettingHelper.xmlNode_ABSizeXB, ((float)fi.Length).ToString() + " B");
            }
            else if (fi.Length >= 1024 && fi.Length < 1024 * 1024)
            {
                node.SetAttribute(AssetBundleSettingHelper.xmlNode_ABSizeXB, ((float)fi.Length / 1024).ToString("f3") + " KB");
            }
            else
            {
                node.SetAttribute(AssetBundleSettingHelper.xmlNode_ABSizeXB, ((float)fi.Length / (1024 * 1024)).ToString("f3") + " MB");
            }
            allE.Add(new KeyValuePair <long, XmlElement>(fi.Length, node));
        }
        foreach (var node in allE.OrderByDescending(k => k.Key).ThenBy(k => k.Value.GetAttribute(AssetBundleSettingHelper.xmlNode_Name)))
        {
            AllRoot.AppendChild(node.Value);
        }
        //同名文件直接覆盖
        xmlDoc.Save(abpi.FullName);

        xmlDoc = VersionInfo.OutputXmlDoc();
        abpi   = ABSH.GetABXmlPath(versionName, VersionXmlInfo.FileName);
        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();
        //*****************************************************************************
    }