/// <summary> /// //从服务器下载到本地 /// </summary> /// <param name="AssetsHost">服务器路径</param> /// <param name="RootAssetsName">总依赖文件目录路径</param> /// <param name="AssetName">请求资源名称</param> /// <param name="saveLocalPath">保存到本地路径,一般存在Application.persistentDataPath</param> /// <returns></returns> IEnumerator DownLoadAssetsWithDependencies2Local(string AssetsHost, string RootAssetsName, string AssetName, string saveLocalPath, dlg_OnAssetBundleDownLoadOver OnDownloadOver = null) { WWW ServerManifestWWW = null; //用于存储依赖关系的 AssetBundle AssetBundle LocalManifestAssetBundle = null; //用于存储依赖关系的 AssetBundle AssetBundleManifest assetBundleManifestServer = null; //服务器 总的依赖关系 AssetBundleManifest assetBundleManifestLocal = null; //本地 总的依赖关系 if (RootAssetsName != "") //总依赖项为空的时候去加载总依赖项 { ServerManifestWWW = new WWW(AssetsHost + "/" + RootAssetsName); Debug.Log("___当前请求总依赖文件~\n"); yield return(ServerManifestWWW); if (ServerManifestWWW.isDone) { //加载总的配置文件 assetBundleManifestServer = ServerManifestWWW.assetBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); Debug.Log("___当前请求总依赖文件~\n"); } else { throw new Exception("总依赖文件下载失败~~~\n"); } } //获取需要加载物体的所有依赖项 string[] AllDependencies = new string[0]; if (assetBundleManifestServer != null) { //根据名称获取依赖项 AllDependencies = assetBundleManifestServer.GetAllDependencies(AssetName); } //下载队列 并获取每个资源的Hash值 Dictionary <string, Hash128> dicDownloadInfos = new Dictionary <string, Hash128>(); for (int i = AllDependencies.Length - 1; i >= 0; i--) { dicDownloadInfos.Add(AllDependencies[i], assetBundleManifestServer.GetAssetBundleHash(AllDependencies[i])); } dicDownloadInfos.Add(AssetName, assetBundleManifestServer.GetAssetBundleHash(AssetName)); if (assetBundleManifestServer != null) //依赖文件不为空的话下载依赖文件 { Debug.Log("Hash:" + assetBundleManifestServer.GetHashCode()); dicDownloadInfos.Add(RootAssetsName, new Hash128(0, 0, 0, 0)); } //卸载掉,无法同时加载多个配置文件 ServerManifestWWW.assetBundle.Unload(true); if (File.Exists(saveLocalPath + "/" + RootAssetsName)) { LocalManifestAssetBundle = AssetBundle.LoadFromFile(saveLocalPath + "/" + RootAssetsName); assetBundleManifestLocal = LocalManifestAssetBundle.LoadAsset <AssetBundleManifest>("AssetBundleManifest"); } foreach (var item in dicDownloadInfos) { if (!CheckLocalFileNeedUpdate(item.Key, item.Value, RootAssetsName, saveLocalPath, assetBundleManifestLocal)) { Debug.Log("无需下载:" + item.Key); continue; } else { DeleteFile(saveLocalPath + "/" + item.Key); } //直接加载所有的依赖项就好了 WWW wwwAsset = new WWW(AssetsHost + "/" + item.Key); //获取加载进度 while (!wwwAsset.isDone) { Debug.Log(string.Format("下载 {0} : {1:N1}%", item.Key, (wwwAsset.progress * 100))); yield return(new WaitForSeconds(0.2f)); } //保存到本地 SaveAsset2LocalFile(saveLocalPath, item.Key, wwwAsset.bytes, wwwAsset.bytes.Length); } if (LocalManifestAssetBundle != null) { LocalManifestAssetBundle.Unload(true); } if (OnDownloadOver != null) { OnDownloadOver(); } }
//5.0版本打包时候选中需要打包的东西然后设置右下角名称,同个/设置多集目录,后面的框标记后缀(后缀不重要) //打包时候的目标文件夹,假设目标文件夹名称为"WJJ",那么会生成"WJJ"和"WJJ.manifest"两个文件 //其中WJJ.manifest文件没有用,只是用来看的,WJJ是一个assetbundle包,里面包含了整个文件夹的依赖信息 //可以先加载这个东西,然后获取到依赖关系后逐步加载 //递一般归加载并保存到Application.persistentDataPath //注意用GetDirectDependencies递归,不要用GetAllDependencies,因为已经包含孙子儿子又会加载孙子,重复加载了 //简单用法直接获取不要用GetAllDependencies,然后倒序加载 /// <summary> /// 下载资源到本地包括它的依赖项 /// </summary> /// <param name="AssetsHost">根目录地址</param> /// <param name="RootAssetsName"></param> /// <param name="AssetName"></param> /// <param name="savePath"></param> public void DownLoadAssets2LocalWithDependencies(string AssetsHost, string RootAssetsName, string AssetName, string savePath, dlg_OnAssetBundleDownLoadOver OnDownloadOver = null) { StartCoroutine(DownLoadAssetsWithDependencies2Local(AssetsHost, RootAssetsName, AssetName, savePath, OnDownloadOver)); }