Пример #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);
    }
Пример #2
0
 DependenciesRefAndCount(string sKey)
 {
     sKey      = AssetBundleSettingHelper.PathToPlatformFormat(sKey).ToLower();
     key       = sKey;
     count     = 0;
     finalSize = 0;
     refs      = new List <string>();
 }
Пример #3
0
    public string Dir_Relative;                    //相对路径,例如"assets\\streamingassets\\apple\\assets\\resources\\prefabs"
    #endregion
    public ABPInfo(string name, string extName, string path = "", bool bFindPath = false, string relativePath = "", bool bFindRelativePath = false)
    {
        name         = AssetBundleSettingHelper.PathToPlatformFormat(name.ToLower());
        path         = AssetBundleSettingHelper.PathToPlatformFormat(path.ToLower());
        relativePath = AssetBundleSettingHelper.PathToPlatformFormat(relativePath.ToLower());

        ExtName = extName.ToLower();;
        string tmpRP = "";

        if (!name.Equals(""))
        {
            //name中可能自己就带有路径,所以要剥离出来
            char slash = AssetBundleSettingHelper.GetSlash();
            int  idxLS = name.LastIndexOf(slash);
            if (idxLS > 0)
            {
                //名字里包含路径
                tmpRP = name.Substring(0, idxLS);
                Name  = name.Substring(idxLS + 1);
            }
            else
            {
                Name = name;
            }
            //名字最末端如果正是扩展名,那么把它干掉,所以一定不能出现连续相同扩展名在结尾,如XXXXX.assetbundle.assetbundle
            if (!ExtName.Equals(""))
            {
                if (Name.EndsWith(ExtName))
                {
                    Name = Name.Substring(0, Name.Length - ExtName.Length);
                }
            }
            NameWithExt = Name + ExtName;
        }

        AssetName = name;
        if (!AssetName.Equals(""))
        {
            if (AssetName.EndsWith(ExtName))
            {
                AssetName = AssetName.Substring(0, AssetName.Length - ExtName.Length);
            }
            DependencyName = AssetName + ExtName;
        }

        if (!path.Equals("") || bFindPath)
        {
            SetPath(Path.Combine(path, tmpRP));
        }
        else if (!relativePath.Equals("") || bFindRelativePath)
        {
            SetRelativePath(Path.Combine(relativePath, tmpRP));
        }



        CaseToLower();
    }
Пример #4
0
 public DependenciesRefAndCount this[string key]
 {
     get
     {
         key = AssetBundleSettingHelper.PathToPlatformFormat(key).ToLower();
         return(dic[key]);
     }
     set
     {
         key      = AssetBundleSettingHelper.PathToPlatformFormat(key).ToLower();
         dic[key] = value;
     }
 }
Пример #5
0
 public DependenciesRefAndCount GetDRAC(string key, bool dontAdd = false)
 {
     key = AssetBundleSettingHelper.PathToPlatformFormat(key).ToLower();
     if (!allDic.ContainsKey(key))
     {
         if (dontAdd)
         {
             return(null);
         }
         var d = DependenciesRefAndCount.GetNew(key);
         allDic.Add(key, d);
     }
     return(allDic[key]);
 }
Пример #6
0
    public void SetRelativePath(string rPath)
    {
        Dir_Relative = AssetBundleSettingHelper.PathToPlatformFormat(rPath);

        var rootPath = AssetBundleSettingHelper.PathToPlatformFormat(Application.dataPath);

        rootPath = rootPath.Substring(0, rootPath.Length - AssetBundleSettingHelper.Asset_RelativePath.Length);
        Dir_Full = Path.Combine(rootPath, Dir_Relative);
        if (NameWithExt != null)
        {
            FullName_RelativePath = (Path.Combine(Dir_Relative, NameWithExt));
            FullName = Path.Combine(rootPath, FullName_RelativePath);
            URI      = AssetBundleSettingHelper.PathToFileUri(FullName);
            FullNameWithoutExtName_Relative = FullName_RelativePath.Substring(0, FullName_RelativePath.Length - ExtName.Length);
        }
    }
Пример #7
0
    public void SetPath(string path)
    {
        path = AssetBundleSettingHelper.PathToPlatformFormat(path);;
        var rootPath = AssetBundleSettingHelper.PathToPlatformFormat(Application.dataPath);

        rootPath = rootPath.Substring(0, rootPath.Length - AssetBundleSettingHelper.Asset_RelativePath.Length);
        if (NameWithExt != null)
        {
            FullName = AssetBundleSettingHelper.PathToPlatformFormat(Path.Combine(path, NameWithExt));
            var index = FullName.IndexOf(rootPath) + rootPath.Length;
            if (index < FullName.Length)
            {
                FullName_RelativePath           = FullName.Substring(index);
                FullNameWithoutExtName_Relative = FullName_RelativePath.Substring(0, FullName_RelativePath.Length - ExtName.Length);
            }
            else
            {
                Debug.LogError("ABPInfo::SetPath Error:" + FullName + " :pathPrefix:" + rootPath);
            }
            URI = AssetBundleSettingHelper.PathToFileUri(FullName);
        }
        Dir_Relative = path.Substring(path.IndexOf(rootPath) + rootPath.Length + 1);
        Dir_Full     = path;
    }
Пример #8
0
    /// <summary>
    /// 删除指定名称的文件
    /// 会处理相关的相对路径
    /// </summary>
    /// <param name="path">搜索的目录</param>
    /// <param name="versionName">一般是平台名字,也可以加其他文件夹在</param>
    /// <param name="fileNames">需要删除的文件名字,相对路径,斜杠为/</param>
    private static void DeleteAssetBundleFiles(string path, string versionName, Dictionary <string, string> fileNames)
    {
        DirectoryInfo fileDir = new DirectoryInfo(path);

        FileInfo[] files = fileDir.GetFiles();
        foreach (var file in files)
        {
            //绝对路径转换为相对路径,并且转换为UNIX的斜杠‘/’
            var abpi = ABSH.GetAssetBundleOutputPath(versionName);
            //路径里可千万别有相同的目录结构啊……比如 Assets/AssetBundles/Windows64/XXXXXX/Assets/AssetBundles/Windows64/
            int index = file.FullName.LastIndexOf(abpi.Dir_Relative) + abpi.Dir_Relative.Length + 1;
            if (index >= 0)
            {
                if (index < file.FullName.Length)
                {
                    var relativePath = AssetBundleSettingHelper.PathToPlatformFormat(file.FullName.Substring(index));
                    //Debug.Log(relativePath);
                    if (fileNames.ContainsKey(relativePath.ToLower()))
                    {
                        DeleteFileAndManifest(file.FullName);
                    }
                }
                else
                {
                    Debug.LogError("DeleteAssetBundleFiles Error:" + file.FullName + " :abpi.Dir_Relative:" + abpi.Dir_Relative);
                }
            }
        }
        var dirs = fileDir.GetDirectories();

        foreach (var dir in dirs)
        {
            //递归调用
            DeleteAssetBundleFiles(dir.FullName, versionName, fileNames);
        }
    }
Пример #9
0
 public bool ContainsKey(string key)
 {
     key = AssetBundleSettingHelper.PathToPlatformFormat(key).ToLower();
     return(dic.ContainsKey(key));
 }
Пример #10
0
 public void Add(string key, DependenciesRefAndCount value)
 {
     key = AssetBundleSettingHelper.PathToPlatformFormat(key).ToLower();
     dic.Add(key.ToLower(), value);
 }
Пример #11
0
    /// <summary>
    /// 根据versionName获取需要打包的所有资源并返回AssetBundleBuild数组
    /// </summary>
    /// <param name="versionName">一般是平台名字,也可以加其他文件夹在</param>
    /// <returns>返回一个数组,可以直接用于BuildPipeline.BuildAssetBundles</returns>
    private static AssetBundleBuild[] GetBuildMapByVersion(string versionName)
    {
        List <AssetBundleBuild> list = new List <AssetBundleBuild>();

        if (!Directory.Exists(ABSH.NeedBuildABPath))
        {
            return(null);
        }
        var NeedBuildABFileList = GetAllFilesWithoutMeta(ABSH.NeedBuildABPath);
        var dicDependencies     = new Dictionary <string, string>();

        //遍历这些文件去找到所有的依赖项
        foreach (var file in NeedBuildABFileList)
        {
            //添加正向依赖,这个不可能会重复,所以不用判断ContainsKey
            ABDependenciesPositive.Add(file.Key, ABDependenciesPositive.GetDRAC(file.Key));
            //var relativePath = Path.Combine();
            var dps = AssetDatabase.GetDependencies(file.Key);
            //                                                                         这里文件名进行了转换,windows下都变成\\,OSX下都是/
            //也转换了一下小写
            foreach (var dp in dps.Where(d => !d.EndsWith(".cs")).Select(d => AssetBundleSettingHelper.PathToPlatformFormat(d.ToLower())))           //脚本文件排除
            {
                {
                    //增加反向依赖,这个会统计数量
                    if (!file.Key.Equals(dp))
                    {
                        if (!ABDependenciesReverse.ContainsKey(dp))
                        {
                            ABDependenciesReverse.Add(dp, ABDependenciesReverse.GetDRAC(dp));
                        }
                        ABDependenciesReverse[dp].AddRef(file.Key);

                        //正向依赖
                        ABDependenciesPositive[file.Key].AddRef(dp);
                    }
                }
                if (dicDependencies.ContainsKey(dp))
                {
                    continue;
                }
                else
                {
                    dicDependencies.Add(dp, dp);
                }
            }
        }
        //这里根据依赖关系,把只被一个单独AB依赖的资源不设置单独打包
        foreach (var abd in ABDependenciesReverse.GetDic().Where(dr => dr.Value.Count == 1))
        {
            if (dicDependencies.ContainsKey(abd.Key))
            {
                dicDependencies.Remove(abd.Key);
            }
        }
        //这里已经获得了所有的资源名称,可以直接生成AssetBundleBuild了
        foreach (var file in dicDependencies)
        {
            //目前先把所有资源单独打包,不做任何合并处理
            var tmp = new AssetBundleBuild();
            {
                tmp.assetBundleName = ABSH.ResourceNameToBundleName(file.Key);
                string[] assets = new string[1] {
                    file.Key
                };
                tmp.assetNames = assets;
            }
            list.Add(tmp);
        }

        return(list.ToArray());
    }
Пример #12
0
    /// <summary>
    /// 获得路径下所有非meta文件,递归搜索文件夹
    /// </summary>
    /// <param name="path">搜索的目标文件夹</param>
    /// <returns>返回一个序列,包含每一个文件的信息和相对于assets文件夹的路径</returns>
    private static List <KeyValuePair <string, FileInfo> > GetAllFilesWithoutMeta(string path)
    {
        var list = new List <KeyValuePair <string, FileInfo> >();

        if (Directory.Exists(path))
        {
            DirectoryInfo fileDir = new DirectoryInfo(path);
            var           files   = fileDir.GetFiles();

            list.AddRange(files.Where(f => !f.Name.Contains(".meta")).Select(
                              //这里文件名进行了转换,windows下都变成\\,OSX下都是/
                              f => new KeyValuePair <string, FileInfo>(f.FullName.Substring(f.FullName.IndexOf(AssetBundleSettingHelper.PathToPlatformFormat(ABSH.NeedBuildABPath))).ToLower(), f)
                              ));
            var dirs = fileDir.GetDirectories();
            foreach (var dir in dirs)
            {
                //递归
                var tmpList = GetAllFilesWithoutMeta(dir.FullName);
                list.AddRange(tmpList);
            }
        }
        return(list);
    }
Пример #13
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);
    }
Пример #14
0
 public void AddRef(string sRef)
 {
     sRef = AssetBundleSettingHelper.PathToPlatformFormat(sRef).ToLower();
     count++;
     refs.Add(sRef);
 }
Пример #15
0
 public static DependenciesRefAndCount  GetNew(string sKey)
 {
     sKey = AssetBundleSettingHelper.PathToPlatformFormat(sKey).ToLower();
     return(new DependenciesRefAndCount(sKey));
 }