资源下载

DownloadMgr 普通类单例
AssetBundleDownload 继承mono单例
AssetBundleDownloadRoutine 继承mono的脚本
DownloadDataEntity 自定义数据实体

资源下载流程:
1.检查持久化路径下是否有版本文件,如果有检查资源更新,如果没有检查streaming路径是否有版本文件,如果没有检查资源更新,如果有将streaming路径下文件解压到持久化路径

public class DownloadDataEntity
{
	public string FullName;
	public string MD5;
	public int Size;
	public bool IsFirstData;
}

private IEnumerator DownloadData()
    {
        if (NeedDownloadCount == 0) yield break;

        m_CurrDownloadData = m_List[0];//当前正在下载的实体

        //服务器上的资源下载路径
        string dataUrl = DownloadMgr.DownloadUrl + m_CurrDownloadData.FullName;
        int index = m_CurrDownloadData.FullName.LastIndexOf('\\');
        string shortPath = m_CurrDownloadData.FullName.Substring(0, index);//短路径 用于创建文件夹
        string localFolderPath = Application.persistentDataPath + "/" + shortPath;//得到本地路径 即在客户端本地当前下载文件存放的文件夹路径
        if (!Directory.Exists(localFolderPath))
        {
            Directory.CreateDirectory(localFolderPath);
        }

        WWW www = new WWW(dataUrl);

        float timeout = Time.time;
        float progress = www.progress;
        while (www != null && www.isDone == false)
        {
            if (progress < www.progress)
            {
                timeout = Time.time;
                progress = www.progress;
                m_CurrDownloadSize = (int)(m_CurrDownloadData.Size * progress);//当前正在下载的文件 已经下载好的大小
            }
            if ((Time.time - timeout) > DownloadMgr.DownloadTimeOut)
            {
                AppDebug.LogError("download fail!");
                yield break;
            }
            yield return null;//一定要等一帧 否则会卡死
        }

        yield return www;

        if (www != null && www.error == null)
        {
            using (FileStream fs = new FileStream(DownloadMgr.Instance.LocalFilePath + m_CurrDownloadData.FullName, FileMode.Create, FileAccess.ReadWrite))
            {
                fs.Write(www.bytes, 0, www.bytes.Length);
            }
        }

        //下载成功
        m_CurrDownloadSize = 0;//当前正在下载的文件已经下载完毕 此刻没有正在下载的文件
        m_DownloadSize += m_CurrDownloadData.Size;//总下载文件大小 加上刚刚下载完毕的文件大小

        //写入本地版本文件
        DownloadMgr.Instance.ModifyLocalData(m_CurrDownloadData);

        m_List.RemoveAt(0);//将已经下载完成的对象从需要下载列表移除
        CompletedCount++;//已经下载完成的数量加一

        if (m_List.Count == 0)//需要下载列表为空
        {
            m_List.Clear();
        }
        else//需要下载列表不为空 继续下载
        {
            IsStartDownload = true;
        }
    }
上一篇:硬件平台上深度学习自动内核优化


下一篇:JS 处理小数小技巧