Пример #1
0
        private IEnumerator AsyncLoadAsset(ABLoadTask task)
        {
            ResourceRequest request;

            if (null == task.AbType)
            {
                request = Resources.LoadAsync(ABHelper.GetFileFullPathWithoutFtype(task.AbPath));
            }
            else
            {
                request = Resources.LoadAsync(ABHelper.GetFileFullPathWithoutFtype(task.AbPath), task.AbType);
            }
            while (!request.isDone)
            {
                if (null != task.Progress)
                {
                    task.Progress(request.progress);
                }
                yield return(null);
            }
            yield return(request);

            // 加载完成
            if (request.isDone)
            {
                LoadComplete(request.asset, task);
            }
        }
Пример #2
0
        public static void WriteManifestFile(string filePath, string replaceTxt, Dictionary <string, List <string> > manifestInfo)
        {
            StringBuilder manifestTxt = new StringBuilder();

            foreach (KeyValuePair <string, List <string> > pair in manifestInfo)
            {
                string fileName = ABHelper.GetFileFullPathWithoutFtype(pair.Key).Replace(replaceTxt, "") + ".ab";
                manifestTxt.Append(fileName + ":");

                for (int i = 0; i < pair.Value.Count; i++)
                {
                    fileName = ABHelper.GetFileFullPathWithoutFtype(pair.Value[i]).Replace(replaceTxt, "") + ".ab";
                    if (i == pair.Value.Count - 1)
                    {
                        manifestTxt.Append(fileName + "\r");
                    }
                    else
                    {
                        manifestTxt.Append(fileName + ",");
                    }
                }
            }

            byte[] manifestbytes = Encoding.UTF8.GetBytes(manifestTxt.ToString().TrimEnd().ToLower());
            Encrypt(ref manifestbytes); //RC4 加密文件
            ABHelper.WriteFileByBytes(filePath, manifestbytes);
        }
Пример #3
0
 private UnityEngine.Object SyncLoadAsset(ABLoadTask task)
 {
     UnityEngine.Object spawner;
     if (null == task.AbType)
     {
         spawner = Resources.Load(ABHelper.GetFileFullPathWithoutFtype(task.AbPath));
     }
     else
     {
         spawner = Resources.Load(ABHelper.GetFileFullPathWithoutFtype(task.AbPath), task.AbType);
     }
     LoadComplete(spawner, task);
     return(spawner);
 }
Пример #4
0
        private static bool CreatABPacker(BuildTarget platform)
        {
            List <string> updateFileList   = new List <string>();
            List <string> updateScriptList = new List <string>();

            Dictionary <string, string>         lastVersionMd5List          = ABHelper.ReadMd5File(PlatformABExportPath + "/" + (CurVersionNum - 1) + "/" + ABHelper.Md5FileName);
            Dictionary <string, List <string> > lastVersionDependenciesList = ABHelper.ReadDependFile(PlatformABExportPath + "/" + (CurVersionNum - 1) + "/" + ABHelper.DependFileName);

            foreach (KeyValuePair <string, List <string> > pair in CurVersionDependenciesList)
            {
                string pathName = pair.Key;
                if (!lastVersionDependenciesList.ContainsKey(pathName.ToLower()))
                {
                    if (IsScriptFileRes(pathName))
                    {
                        updateScriptList.Add(pathName);
                    }
                    else
                    {
                        updateFileList.Add(pathName);
                    }
                    continue;
                }
                foreach (string depend in pair.Value)
                {
                    if (!lastVersionDependenciesList[pathName.ToLower()].Contains(depend.ToLower()))
                    {
                        if (IsScriptFileRes(pathName))
                        {
                            updateScriptList.Add(pathName);
                        }
                        else
                        {
                            updateFileList.Add(pathName);
                        }
                        break;
                    }
                    else if (CurVersionMd5List[depend].ToLower() != lastVersionMd5List[depend.ToLower()])
                    {
                        if (IsScriptFileRes(pathName))
                        {
                            updateScriptList.Add(pathName);
                        }
                        else
                        {
                            updateFileList.Add(pathName);
                        }
                        break;
                    }
                }
            }

            // 检测cs和dll脚本
            if (updateScriptList.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var pair in updateScriptList)
                {
                    sb.AppendLine(pair);
                }
                if (EditorUtility.DisplayDialog("提示", "检测到有cs等脚本的更新!!\n “停止”则中断打包,“忽略”则继续。如下:\n" + sb, "停止", "忽略"))
                {
                    throw new Exception("打包中断!!!!!!!!!!");
                }
            }

            if (updateFileList.Count > 0)
            {
                ClearAssetBundlesName();
                AssetDatabase.Refresh();

                foreach (string path in updateFileList)
                {
                    string fileName;
                    switch (CurVersionFileType[path])
                    {
                    case "0":
                        foreach (string path2 in CurVersionDependenciesList[path])
                        {
                            fileName = (ABHelper.GetFileFolderPath(path2).Replace(ResFolder, "")) + ".ab";
                            fileName = CreatFileUrlMd5(fileName);
                            AssetImporter.GetAtPath(path2).assetBundleName = fileName;
                        }
                        break;

                    case "1":
                        CopyLuaFiles(path);
                        break;

                    case "2":
                        fileName = (ABHelper.GetFileFullPathWithoutFtype(path).Replace(ResFolder, "")) + ".ab";
                        fileName = CreatFileUrlMd5(fileName);
                        AssetImporter.GetAtPath(path).assetBundleName = fileName;
                        break;

                    case "3":
                        fileName = (ABHelper.GetFileFullPathWithoutFtype(path).Replace(ResFolder, "")) + ".ab";
                        fileName = CreatFileUrlMd5(fileName);
                        AssetImporter.GetAtPath(path).assetBundleName = fileName;
                        break;

                    default: break;
                    }
                }
                try
                {
                    // 生成ab文件
                    BuildPipeline.BuildAssetBundles(CurVersionABExportPath, BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle, platform);
                }
                catch (Exception e)
                {
                    BuildFailure();
                    Debug.LogError("打包异常!!" + e.Message);
                }
                System.Threading.Thread.Sleep(1000);

                ClearAssetBundlesName();
                AssetDatabase.Refresh();
            }
            else
            {
                BuildFailure();
            }

            bool result = updateFileList.Count > 0;

            if (result)
            {
                // 删除不用的manifest
                List <string> allFiles = ABHelper.GetAllFilesPathInDir(PlatformABExportPath);
                foreach (string path in allFiles)
                {
                    if (path.EndsWith(".manifest"))
                    {
                        File.Delete(path);
                    }
                    if (path.EndsWith(CurVersionNum.ToString()))
                    {
                        File.Delete(path);
                    }
                }
            }
            return(result);
        }
Пример #5
0
        private static void CreatPacker(BuildTarget platform)
        {
            ClearAssetBundlesName();
            AssetDatabase.Refresh();

            CurVersionManifestList = new Dictionary <string, List <string> >();
            List <string> folderBundlePathList = new List <string>();
            List <string> fileBundlePathList   = new List <string>();
            List <string> sceneBundlePathList  = new List <string>();
            List <string> luaPathList          = new List <string>();

            foreach (string path in BundleFolderPath)
            {
                string fullPath = ResFolder + path;
                folderBundlePathList.AddRange(ABHelper.GetAllFilesPathInDir(fullPath));
            }
            foreach (string path in BundleFilePath)
            {
                string fullPath = ResFolder + path;
                fileBundlePathList.AddRange(ABHelper.GetAllFilesPathInDir(fullPath));
            }
            foreach (string path in BundleScenePath)
            {
                string fullPath = AssetFolder + path;
                sceneBundlePathList.AddRange(ABHelper.GetAllFilesPathInDir(fullPath));
            }
            foreach (string path in BundleLuaPath)
            {
                string fullPath = AssetFolder + path;
                luaPathList.AddRange(ABHelper.GetAllFilesPathInDir(fullPath));
            }

            foreach (string path in folderBundlePathList)
            {
                if (!IsNeedFileRes(path))
                {
                    continue;
                }
                // "Assets/Resources/UI/Tips/Tips.bytes"
                string assetPath = path.Replace("\\", "/");
                string abName    = ABHelper.GetFileFolderPath(assetPath.Replace(ResFolder, "")) + ".ab";
                AssetImporter.GetAtPath(assetPath).assetBundleName = CreatFileUrlMd5(abName);
            }
            foreach (string path in fileBundlePathList)
            {
                if (!IsNeedFileRes(path))
                {
                    continue;
                }
                string assetPath = path.Replace("\\", "/");
                string abName    = ABHelper.GetFileFullPathWithoutFtype(assetPath.Replace(ResFolder, "")) + ".ab";
                AssetImporter.GetAtPath(assetPath).assetBundleName = CreatFileUrlMd5(abName);

                // 依赖文件
                string[] dependPaths = AssetDatabase.GetDependencies(assetPath);
                if (dependPaths.Length > 0)
                {
                    foreach (string path1 in dependPaths)
                    {
                        if (path1.Contains(ResFolder) && assetPath != path1)
                        {
                            assetPath = assetPath.Replace(ResFolder, "");
                            if (!CurVersionManifestList.ContainsKey(assetPath))
                            {
                                CurVersionManifestList.Add(assetPath, new List <string>());
                            }
                            CurVersionManifestList[assetPath].Add(path1.Replace(ResFolder, ""));
                        }
                    }
                }
            }
            foreach (string path in sceneBundlePathList)
            {
                if (!path.EndsWith(".unity"))
                {
                    continue;
                }
                string assetPath = path.Replace("\\", "/");
                string abName    = ABHelper.GetFileFullPathWithoutFtype(assetPath.Replace(ResFolder, "") + ".ab");
                AssetImporter.GetAtPath(assetPath).assetBundleName = CreatFileUrlMd5(abName);

                // 依赖文件
                string[] dependPaths = AssetDatabase.GetDependencies(assetPath);
                if (dependPaths.Length > 0)
                {
                    foreach (string path1 in dependPaths)
                    {
                        if (path1.Contains(ResFolder) && assetPath != path1)
                        {
                            assetPath = assetPath.Replace(AssetFolder, "");
                            if (!CurVersionManifestList.ContainsKey(assetPath))
                            {
                                CurVersionManifestList.Add(assetPath, new List <string>());
                            }
                            CurVersionManifestList[assetPath].Add(path1.Replace(ResFolder, ""));
                        }
                    }
                }
            }
            foreach (string path in luaPathList)
            {
                if (!path.EndsWith(".lua"))
                {
                    continue;
                }
                string filePath = path.Replace("\\", "/");
                CopyLuaFiles(filePath);
            }

            System.GC.Collect();
            BuildPipeline.BuildAssetBundles(PlatformABExportPath, BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle, platform);

            ClearAssetBundlesName();
            AssetDatabase.Refresh();

            // ab的依赖文件
            string fileUrl = CreatFileUrlMd5(ABHelper.ManifestFileName);

            ABHelper.WriteManifestFile(PlatformABExportPath + "/" + fileUrl, CurVersionManifestList);

            // 删除不用的manifest
            List <string> allFiles = ABHelper.GetAllFilesPathInDir(PlatformABExportPath);

            foreach (string path in allFiles)
            {
                if (path.EndsWith(".manifest"))
                {
                    File.Delete(path);
                }
                if (ABHelper.GetFileNameWithoutSuffix(path.Replace("\\", "/")) == PlatformName)
                {
                    File.Delete(path);
                }
            }

            // 写入文件
            StringBuilder txt = new StringBuilder();

            foreach (var pair in CurVersionFileUrlMd5)
            {
                txt.Append(pair.Value + ":" + pair.Key + "\r");
            }
            ABHelper.WriteFileByBytes(PlatformABExportPath + "/" + ABHelper.VersionFileName, Encoding.UTF8.GetBytes(txt.ToString().TrimEnd().ToLower()));
        }