예제 #1
0
    private IEnumerator AppendAB(string fileName, FileList fileListLocal)
    {
        //下载资源文件
        UnityWebRequest unityWebRequest = UnityWebRequest.Get(serverName + fileName);  //不同协程的资源可能存在竞争,最后用局部变量,或者解决竞争关系

        yield return(unityWebRequest.SendWebRequest());

        using (FileStream fs = File.OpenWrite(Application.persistentDataPath + "/" + fileName))
        {
            fs.Write(unityWebRequest.downloadHandler.data, 0, unityWebRequest.downloadHandler.data.Length);
        }

        //下载文件清单
        unityWebRequest = UnityWebRequest.Get(serverName + fileName + ".manifest");
        yield return(unityWebRequest.SendWebRequest());

        using (FileStream fs = File.OpenWrite(Application.persistentDataPath + "/" + fileName + ".manifest"))
        {
            fs.Write(unityWebRequest.downloadHandler.data, 0, unityWebRequest.downloadHandler.data.Length);
        }

        //更新json中的MD5码
        fileListLocal.ToDictionaary().Add(fileName, Ablib.GetMd5(Application.persistentDataPath + "/" + fileName));
        File.WriteAllText(Application.persistentDataPath + "/filelist.json", JsonUtility.ToJson(fileListLocal));

        Debug.Log($"Append {fileName} Over, downLoadOverCout : {++downLoadOverCount}");
    }
예제 #2
0
    public static void Build()
    {
        Debug.Log("Building...");
        // BuildPipeline.BuildAssetBundles("AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);  //Applicateion.datePatch指向Asset文件夹

        //生成fileList.json(filename, md5)
        string[] files = Directory.GetFiles(Application.dataPath + "/../AssetBundles/StandaloneWindows/", "*_ab");

        Dictionary <string, string> fileDictionary = new Dictionary <string, string>();

        foreach (var file in files)
        {
            string fileName = new FileInfo(file).Name;
            fileDictionary[fileName] = Ablib.GetMd5(file);
        }

        //别忘了AssetBundles也要加入文件清单
        fileDictionary["StandaloneWindows"] = Ablib.GetMd5(Application.dataPath + "/../AssetBundles/StandaloneWindows/StandaloneWindows");
        File.WriteAllText(Application.dataPath + "/../AssetBundles/StandaloneWindows/filelist.json", JsonUtility.ToJson(new FileList(fileDictionary)));

        //生成resourceList.josn(abName, prefabName)
        string[] resourceLists = Directory.GetFiles(Application.dataPath + "/../AssetBundles/StandaloneWindows/", "*.manifest");

        Dictionary <string, string> resourceListDictionary = new Dictionary <string, string>();

        foreach (var file in resourceLists)
        {
            FileInfo fileInfo  = new FileInfo(file);
            string   fileName  = fileInfo.Name;
            string[] fileLines = File.ReadAllLines(fileInfo.DirectoryName + "/" + fileName);

            //读取Assets到Dependencies中的内容
            var state = 0;
            foreach (var str in fileLines)
            {
                if (state == 0)  //表示尚未读取到"Assets"
                {
                    if (str.Contains("Assets:"))
                    {
                        state = 1;
                    }
                }
                else if (state == 1) //读到Assets,开始处理
                {
                    if (str.Contains("Dependencies:"))
                    {
                        state = 2;
                    }
                    else
                    {
                        string[] subStrings = str.Split(' ');
                        resourceListDictionary.Add(subStrings[subStrings.Length - 1], fileName);
                    }
                }
                else if (state == 2)
                {
                    break;
                }
            }
        }
        File.WriteAllText(Application.dataPath + "/../AssetBundles/StandaloneWindows/resourceList.json", JsonUtility.ToJson(new FileList(resourceListDictionary)));



        // 拷贝打包文件到AssetString目录
        if (Directory.Exists(Application.streamingAssetsPath))
        {
            Directory.Delete(Application.streamingAssetsPath, true);
        }

        Directory.CreateDirectory(Application.streamingAssetsPath);
        Ablib.CopyDirectory(Application.dataPath + "/../AssetBundles/StandaloneWindows", Application.streamingAssetsPath);

        Debug.Log("Build Finish");
    }
예제 #3
0
    IEnumerator Start()
    {
        print(Application.persistentDataPath + $"Start DownLoad AB {++assetCount}");
        //第一次启动程序,把StreamingAssetsPath中的资源包移动到PersistantDataPath中
        if (PlayerPrefs.GetInt("IsFirstLoad", 1) == 1)
        {
            print("FirstLoad");
            Ablib.CopyDirectory(Application.streamingAssetsPath, Application.persistentDataPath);
            PlayerPrefs.SetInt("IsFirstLoad", 0);

            shouldUpdateLua = true;
        }

        //读取本地文件的MD5码
        FileList fileListLocal = JsonUtility.FromJson <FileList>(File.ReadAllText(Application.persistentDataPath + "/filelist.json"));

        //读取服务器上的MD5码
        UnityWebRequest unityWebRequest = UnityWebRequest.Get(serverName + "filelist.json");

        yield return(unityWebRequest.SendWebRequest());


        FileList fileListService = JsonUtility.FromJson <FileList>(unityWebRequest.downloadHandler.text);

        //本地和服务器相比较,用于新增和文件改变
        foreach (var fileMd5 in fileListService.ToDictionaary())
        {
            if (!fileListLocal.ToDictionaary().ContainsKey(fileMd5.Key)) //没有这个文件,表示新增
            {
                print($"Add New File : {fileMd5.Key}, assetCount : {++assetCount}");
                StartCoroutine(AppendAB(fileMd5.Key, fileListLocal));

                if (fileMd5.Key == luaABName)
                {
                    shouldUpdateLua = true;
                }
            }
            else         //更新
            {
                string md5Local  = fileListLocal.ToDictionaary()[fileMd5.Key];
                string md5Server = fileMd5.Value;

                if (md5Local != md5Server)
                {
                    print($"Changed File : ${fileMd5.Key}, assetCount : {++assetCount}");
                    StartCoroutine(UpdateAB(fileMd5.Key, fileListLocal));

                    if (fileMd5.Key == luaABName)
                    {
                        shouldUpdateLua = true;
                    }
                }
            }
        }

        // 服务器和本地比较,用于删除多余文件
        foreach (var fileMd5 in fileListLocal.ToDictionaary())
        {
            if (!fileListService.ToDictionaary().ContainsKey(fileMd5.Key)) //表明服务器已经删除了该文件
            {
                StartCoroutine(DeleteAB(fileMd5.Key, fileListLocal));
            }
        }

        //下载资源对应AB表:
        UnityWebRequest webRequestResourceList = UnityWebRequest.Get(serverName + "resourceList.json");

        yield return(webRequestResourceList.SendWebRequest());

        resToAB = JsonUtility.FromJson <FileList>(webRequestResourceList.downloadHandler.text).ToDictionaary();



        print($"Wait for DownLoad AB {++downLoadOverCount}");
    }