/// <summary> /// 留给外部的接口,根据bundle名异步加载文件,回调需要提前赋值 /// </summary> /// <param name="bundleName"></param> /// <returns></returns> internal IEnumerator LoadFileAsync(string bundleName) { //是否使用StreamingAssets? if (!string.IsNullOrEmpty(pather.OriginMainManifest)) { var streamingPath = @Path.Combine(Application.streamingAssetsPath, bundleName); if (File.Exists(streamingPath)) { // Debug.Log("LoadStreaming"); yield return(LoadBundleAsync(bundleName, null, BundleLoadMethod.File_StreamingBundle)); yield break; } } //是否使用服务器资源 if (!string.IsNullOrEmpty(pather.WebServeMainManifest)) { Assert.IsNotNull(version); while (!version.IsUpdateToServe) { yield return(null); } var bundleVersion = version.currentServeVersion; var localBundlePath = pather.GetLocalBundlePath(bundleName, bundleVersion); if (File.Exists(localBundlePath)) { // Debug.Log("LoadLocalFile"); yield return(LoadBundleAsync(bundleName, bundleVersion, BundleLoadMethod.File_LocalBundle)); yield break; } // Debug.Log("LoadWWW"); yield return(LoadBundleAsync(bundleName, bundleVersion, BundleLoadMethod.WWW_LoadBundleAndBytes, true)); yield break; } Debug.LogError("没有这个AssetBundle: " + bundleName); }
/// <summary> /// 从服务器下载并保存新资源包 /// </summary> /// <param name="bundleName"></param> /// <param name="bundleVersion"></param> /// <returns></returns> private IEnumerator DownloadAndSaveBundle(string bundleName, string bundleVersion) { //获取服务器请求url var requestUrl = pather.GetServeBundlePath(bundleName, bundleVersion); var webBytesLoadRequest = UnityWebRequest.Get(requestUrl); yield return(webBytesLoadRequest.SendWebRequest()); byte[] bs = webBytesLoadRequest.downloadHandler.data; //获取本地包存储路径 var localPath = pather.GetLocalBundlePath(bundleName, bundleVersion); //将服务器获得的新包数据保存到本地 FileUtil.CreateFile(localPath, bs); }