1. Code implementation
Using Unityengine;
Using System.Collections;
Using System;
Using System.Text;
Using System.Net;
Using System.IO;
Internal class Webreqstate
{
Public byte[] Buffer;
Public FileStream FS;
public const int buffersize = 1024;
Public Stream Orginalstream;
Public HttpWebResponse WebResponse;
Public webreqstate (String path)
{
Buffer = new byte[1024];
FS = new FileStream (path, filemode.create);
}
}
public class Httphelper
{
string path = null;
String Assetname;
Public Httphelper (String path)
{
This.path = path;
}
public void asydownload (string url)
{
Debug.Log ("url is:+" +url);
Assetname = URL. Split ('/') [4];
Debug.Log ("Assetname is: +" +assetname);
HttpWebRequest HttpRequest = webrequest.create (URL) as HttpWebRequest;
Httprequest.begingetresponse (New AsyncCallback (Responsecallback), HttpRequest);
}
void Responsecallback (IAsyncResult ar)
{
HttpWebRequest req = ar. AsyncState as HttpWebRequest;
if (req = = null) return;
HttpWebResponse response = req. EndGetResponse (AR) as HttpWebResponse;
if (response. StatusCode! = Httpstatuscode.ok)
{
Response. Close ();
Return
}
Debug.Log (path + "/" + assetname);
Webreqstate st = new Webreqstate (path + "/" + assetname);
St. WebResponse = response;
Stream Responsestream = Response. GetResponseStream ();
St.orginalstream = Responsestream;
Responsestream.beginread (St. Buffer, 0, Webreqstate.buffersize, new AsyncCallback (Readdatacallback), ST);
}
void Readdatacallback (IAsyncResult ar)
{
Webreqstate rs = ar. AsyncState as Webreqstate;
int read = Rs.OrginalStream.EndRead (AR);
if (Read > 0)
{
Rs.fs.Write (Rs. Buffer, 0, read);
Rs.fs.Flush ();
Rs.OrginalStream.BeginRead (Rs. Buffer, 0, Webreqstate.buffersize, new AsyncCallback (Readdatacallback), RS);
}
Else
{
Rs.fs.Close ();
Rs.OrginalStream.Close ();
Rs. WebResponse.Close ();
Debug.Log (Assetname + "::: Success");
}
}
}
2. Download resources from server to local (PC side tested successfully)
void Ongui ()
{
string[] str = new String[2] {"Sphere", "Cube"};
Download
if (GUI. button (new Rect (10, 10, 200, 45), "Download"))
{
String rootpath = Application.persistentdatapath;//android saved to the/storage/sdcard0/android/data/package name ( Example: Com.example.test)/files
for (int i = 0; i < str. Length; i++)//str is a string array that holds some assetbundle names
{
Thread thread = new Thread (new Parameterizedthreadstart (Downasset)); Parameterizedthreadstart multi-line Path parameter
Thread. Start (RootPath + "|" + Str[i]); With only one object argument, so use string concatenation
}
}
void Downasset (System.Object file)
{
string[] FileName = file. ToString (). Split (' | ');
Debug.Log ("filename 000000 is: +" + filename[0]);
Debug.Log ("filename is 1111:+" + filename[1]);
Httphelper help = new Httphelper (filename[0]);
Help. Asydownload ("http://192.168.0.98/MyItems/Test/" + filename[1] + ". Assetbundle");//Note test the IP address on the phone
Debug.Log ("Download address is: +" + "http://192.168.0.98/MyItems/Test/" + filename[1] + ". Assetbundle");
}
multithreaded download resources on a server