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);
    }