Exemplo n.º 1
0
    /// <summary>
    /// 初始化函数
    /// </summary>
    public void init()
    {
        //读取本地资源包信息
        string fileName = Application.persistentDataPath + "/" + ConstString.sAssetBundleInfoXML;

        AssetBundleXMLUtils.loadLocalInfo(ref m_LocalAssetBundleInfos, fileName);
        //资源包名称归类
        InitPathInfo();
    }
    public AssetBundleDownloader(string assetBundleInfo, string serverPath)
    {
        if (string.IsNullOrEmpty(assetBundleInfo))
        {
            m_IsDone   = true;
            m_Error    = null;
            m_Progress = 1;
            return;
        }
        //解析字符串
        parseInfo(assetBundleInfo, serverPath);
        //读取本地资源包信息
        string fileName = Application.persistentDataPath + "/" + ConstString.sAssetBundleInfoXML;

        AssetBundleXMLUtils.loadLocalInfo(ref m_LocalAssetBundleInfos, fileName);
        //过滤出需要更新的资源包
        selectAssetBudlesForUpdate();
    }
    /// <summary>
    /// 下载资源包函数
    /// </summary>
    /// <param name="finishDownload"></param>
    /// <returns></returns>
    public IEnumerator StartDownload()
    {
        Debug.Log("In Start DownLoad~~~~~~~~~~~~~~||");
        m_AmountOfAB = m_DownloadAssetBundleInfos.Count;
        if (m_DownloadAssetBundleInfos == null || m_DownloadAssetBundleInfos.Count == 0)
        {
            m_IsDone   = true;
            m_Error    = null;
            m_Progress = 1;
            yield return(null);
        }
        Debug.Log("Show DownloadAsset Count:" + m_DownloadAssetBundleInfos.Count);
        foreach (KeyValuePair <string, AssetBundleInfo> assetBundle in m_DownloadAssetBundleInfos)
        {
            AssetBundleInfo strcAssetBundleInfo = assetBundle.Value;
            WWW             w = new WWW(strcAssetBundleInfo.sPath);
            m_Size             = w.bytesDownloaded;
            m_DownloadedBytes += w.bytesDownloaded;
            Debug.Log("Show strcAssetBundleInfo.sPath: " + strcAssetBundleInfo.sPath);
            yield return(w);

            if (w.error != null)
            {
                if (string.IsNullOrEmpty(m_Error))
                {
                    m_Error = assetBundle.Key + ":" + w.error + ";";
                }
                else
                {
                    m_Error += assetBundle.Key + ":" + w.error + ";";
                }

                Debug.Log("m_Error: " + m_Error + "@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
            }
            else if (w.isDone)
            {
                byte[] model  = w.bytes;
                int    length = model.Length;
                //写入文件到本地
                string sAssetBundlePath = Application.persistentDataPath;
                CreateModelFile(sAssetBundlePath, assetBundle.Key, model, length);
                m_NumOfDowloadedAB++;
                m_Progress = (float)m_NumOfDowloadedAB / m_AmountOfAB;
                Debug.Log("Create File Over~~~~~~~~~~~~~~");
                //写入本地记录
                if (m_LocalAssetBundleInfos.ContainsKey(assetBundle.Key))
                {
                    m_LocalAssetBundleInfos[assetBundle.Key].sPath    = strcAssetBundleInfo.sPath;
                    m_LocalAssetBundleInfos[assetBundle.Key].sVersion = strcAssetBundleInfo.sVersion;
                }
                else
                {
                    m_LocalAssetBundleInfos.Add(assetBundle.Key, assetBundle.Value);
                }
                //保存信息文件到本地
                if (string.IsNullOrEmpty(m_Error) || !m_Error.Contains(assetBundle.Key))
                {
                    string fileName = Application.persistentDataPath + "/" + ConstString.sAssetBundleInfoXML;
                    AssetBundleXMLUtils.saveLocalInfo(m_LocalAssetBundleInfos, fileName);
                }
            }
        }

        Debug.Log("DownLoad Done~~~~~~~~~~~");
        m_IsDone = true;
        if (FinishDownloadDelegate != null)
        {
            FinishDownloadDelegate();
            FinishDownloadDelegate = null;
        }
    }