void StartDownloadBunle() { for (int i = 0; i < serverVersionLoader.files.Count; i++) { DownloadBundle downloadBundle = gameObject.AddComponent <DownloadBundle> (); string fileName = ""; serverVersionLoader.files.TryGetValue(i, out fileName); downloadBundle.bundleName = fileName + ".unity3d"; downloadBundle.OnDownLoadFinish += this.OnDownLoadFinish; } }
void OnDownLoadFinish(DownloadBundle loader) { loader.OnDownLoadFinish -= this.OnDownLoadFinish; finished++; if (finished== serverVersionLoader.files.Count) { serverVersionLoader.xmlDoc.Save(Application.persistentDataPath+"/AssetXML.xml"); if(OnDownloadAllFinish!=null){ OnDownloadAllFinish(null); } Destroy(this); } }
void OnDownLoadFinish(DownloadBundle loader) { loader.OnDownLoadFinish -= this.OnDownLoadFinish; finished++; if (finished == serverVersionLoader.files.Count) { serverVersionLoader.xmlDoc.Save(Application.persistentDataPath + "/AssetXML.xml"); if (OnDownloadAllFinish != null) { OnDownloadAllFinish(null); } Destroy(this); } }
protected void DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) { DownloadBundle bundle = e.UserState as DownloadBundle; try { if (!e.Cancelled && e.Error == null) { bundle.bytes = (byte[])e.Result; bundle.success = true; } } finally { bundle.waiter.Set(); } }
public byte[] Download(string uri, string localPath = "", string QueryHeader = "BaseHeader") { using (WebClient fileDownloader = new WebClient()) { fileDownloader.Encoding = System.Text.Encoding.UTF8; fileDownloader.Headers.Add("User-Agent", QueryHeader); fileDownloader.DownloadDataCompleted += new DownloadDataCompletedEventHandler(DownloadDataCompleted); DownloadBundle bundle = new DownloadBundle(); bundle.waiter = new System.Threading.AutoResetEvent(false); bundle.success = false; fileDownloader.DownloadDataAsync(new Uri(uri), bundle); // block until download complete bundle.waiter.WaitOne(); if (!bundle.success) { throw new GEventException(new ErrorBundle(EventType.WEB, ErrorCode.DownloadFailed, "Download File Failed")); } if (localPath != "") { Directory.CreateDirectory(Path.GetDirectoryName(Path.GetFullPath(localPath))); using (FileStream fs = new FileStream(localPath, FileMode.Create)) using (BinaryWriter bw = new BinaryWriter(fs)) { bw.Write(bundle.bytes); bw.Close(); fs.Close(); } } return(bundle.bytes); } }