Recently has been the company's game to support ARM64, there are a lot of bugs or unity currently does not support the operation of the problem, these two days encountered asynchronous HttpWebRequest not support the problem, because there has been no contact, so also took the opportunity to learn about the use of the next.
Here's an example of using HttpWebRequest to download a zip file asynchronously.
Using unityengine;using system.collections;using system.net;using system.io;using system;using System.Threading; Using System.text;public class requeststate{const int m_buffetsize = 1024; Public StringBuilder M_requestdata; Public byte[] M_bufferread; Public HttpWebRequest M_request; Public HttpWebResponse M_response; Public Stream M_streamresponse; Public RequestState () {m_bufferread = new byte[m_buffetsize]; M_requestdata = new StringBuilder (""); M_request = null; M_streamresponse = null; }}public class Newbehaviourscript:monobehaviour {FileStream filestream=null;//Use this for initializationvoid Start () {fileStream = new FileStream ("Disunity_v0.3.4.zip", FileMode.Create); Downloadmusicasyn ();} void Downloadmusicasyn () {Debug.Log ("Downloadmusicasyn Thread Start"); try {HttpWebRequest myhttpwebrequest = (HttpWebRequest) webrequest.create ("http://dev.thisisgame.com.Cn/disunity_v0.3.4.zip "); RequestState myrequeststate = new RequestState (); Myrequeststate.m_request = myHttpWebRequest; Debug.Log ("BeginGetResponse Start"); asynchronous acquisition; IAsyncResult result = (IAsyncResult) myhttpwebrequest.begingetresponse (new AsyncCallback (Respcallback), Myrequeststate); Debug.Log ("BeginGetResponse End"); } catch (System.Exception ex) {Debug.logerror (ex. ToString ()); }} void Respcallback (IAsyncResult result) {Debug.Log ("Respcallback 0"); try {requeststate myrequeststate = (requeststate) result. asyncstate; HttpWebRequest myhttpwebrequest = myrequeststate.m_request; Debug.Log ("Respcallback endgetresponse"); Myrequeststate.m_response = (HttpWebResponse) myhttpwebrequest.endgetresponse (result); Stream Responsestream = Myrequeststate.m_response. GetResponseStream (); MyrequeststatE.m_streamresponse = Responsestream; Start reading data; IAsyncResult Asyncreadresult = responsestream.beginread (myrequeststate.m_bufferread, 0, 1024x768, new AsyncCallback ( Readcallback), myrequeststate); Return } catch (System.Exception ex) {Debug.logerror (ex. ToString ()); }} void Readcallback (IAsyncResult result) {Debug.Log ("readcallback"); try {requeststate myrequeststate = (requeststate) result. asyncstate; Stream responsestream = myrequeststate.m_streamresponse; int read = Responsestream.endread (result); Debug.Log ("Read size =" + read); if (Read > 0) {//writes the received data; FileStream.Write (myrequeststate.m_bufferread, 0, 1024); Filestream.flush (); Filestream.close (); continue to read data; Myrequeststate.m_bufferread = new byte[1024]; IasYncresult Asyncreadresult = responsestream.beginread (myrequeststate.m_bufferread, 0, 1024x768, new AsyncCallback ( Readcallback), myrequeststate); }} catch (System.Exception ex) {Debug.logerror (ex). ToString ()); }} void Timeoutcallback (object state, bool timeout) {if (timeout) {HttpWebRequest req Uest = state as HttpWebRequest; if (request!=null) {request. Abort (); }}}//update is called once per framevoid update () {}}
Unity3d C # httpwebrequest asynchronous download file