- Home /
Question by
numetdevelop · Jan 31, 2020 at 03:31 AM ·
c#iosassetbundledownloadftp
Guideline 4.2.3-Design-Minimum Functionality
Our app needs to download external resource files to expand the game, and also provide the size of the download file and check download box. Assetbundle using ftp to download and no compression.
public static double GetGameSize(string _FileName)
{
//取得大小
string mURI = mHTTP + _FileName;
//Debug.LogError(mURI);
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri(mURI));
ftpRequest.Credentials = new NetworkCredential(mUserName, mPassword);
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
ftpRequest.UseBinary = true;
ftpRequest.KeepAlive = false;
return ftpRequest.GetResponse().ContentLength * 1.0 / (1024 * 1024);
}
public static IEnumerator DownloadGame(Text _text, string _fileName,string _FileExName)
{
string mURI = mHTTP + _fileName + _FileExName;
FtpWebRequest ftpRequest;
double DownLoadSize = 0;
double TotalSize = 0;
// Debug.Log(mURI);
TotalSize= GetGameSize(_fileName+ _FileExName);
//取得資源
ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri(mURI));
ftpRequest.Credentials = new NetworkCredential(mUserName, mPassword);
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.UseBinary = true;
ftpRequest.KeepAlive = false;
ftpRequest.Timeout = 5000;
mStream = ftpRequest.GetResponse().GetResponseStream();
mFileStream = new FileStream(Application.persistentDataPath + "/" + _fileName + _FileExName, FileMode.Create);
int Length = 1024 * 8;
Byte[] buffer = new Byte[Length];
int bytesRead = mStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
mFileStream.Write(buffer, 0, bytesRead);
bytesRead = mStream.Read(buffer, 0, Length);
DownLoadSize = mFileStream.Length * 1.0 / (1024 * 1024);
int Number = (int)((DownLoadSize / TotalSize) * 100);
_text.text = Number.ToString() + "%";
yield return null;
}
CloseDownloadStream();
Debug.Log("[Download] "+ _fileName+ _FileExName + "下載完成");
}
Comment
Your answer
Follow this Question
Related Questions
What would be the best approach to making a puzzle game that downloads levels dynamically 1 Answer
How use asset bundle for a image folder ? 1 Answer
Texture on instantiated asset bundle sometimes black only on iPhone 0 Answers
Error when downloading AssetBundle from external server during runtime 0 Answers
Download an save assetbundle from server , on device Android and iOS 2 Answers