由于unity www不能设定下载超时时间,所以利用下面方法实现
private IEnumerator IEDownFile(url)
{
using (WWW www = new WWW(url))
{
float timeout = Time.time;
float process = www.progress;
while (www != null && www.isDone == false)
{
if (process < www.progress)//当进度不动了 下载卡住了 会引起下载超时
{
timeout = Time.time;
process = www.progress;
}
//超时判断
if (Time.time - timeout > downTimeOut)
{
HandleDownFaile("超时");
yield break;
}
yield return null;//等待1帧 防止卡死线程
}
yield return www;
//本地写入文件
if (www != null && string.IsNullOrEmpty(www.error))
{
Debuger.Log("下载文件url:"+url);
}
else
{
HandleDownFaile(www.error);
yield break;//结束协程
}
}
}