Esempio n. 1
0
    IEnumerator _DownloadAssetImpl(Mission mission)
    {
        mCurMission = mission;

        string bundleName = mission.CurrentBundle.ToLower();
        DownloadedCB cb_PerBundleLoaded = mission.PerloadedCB;
        AllDownloadedCB cb_all = mission.AllLoadedCB;
        AssetBundle bundle = GetCached(bundleName);

        string url = "";
        if (bundle == null)
        {
            // 尝试从本地读取文件
            bool bLocalNew = IsLocalNewVersion(bundleName);
            if (bLocalNew)
                url = GetLocalPath(ly.AssetDependencyManager.GetPackagePath(bundleName));
            else
                url = GetHttpPath(ly.AssetDependencyManager.RealPath2PackagePath(bundleName));

            // 如果本地已经更新,并且没有设置回调,则直接跳过
            if (!bLocalNew || cb_PerBundleLoaded != null)
            {
                WWW downloading = new WWW(url);
                mission.CurrentDownloading = downloading;
                yield return downloading;

                if (downloading.error != null)
                {
                    UIHelper.ShowMessage("Download error:"+downloading.error+", file:"+url+", make sure your asset is in the main bundle.");
                }
                else
                {
                    Debug.Log("Bundle has been downloaded! file:"+url);

                    // 有回调函数设置,才认为需要立即加载到内存
                    if (cb_PerBundleLoaded != null)
                        bundle = downloading.assetBundle;

                    // 如果远程下载了文件,则保存到本地
                    if (!bLocalNew)
                    {
                        string path = GetLocalPathNoPrefix(ly.AssetDependencyManager.GetPackagePath(bundleName));
                        string dir = System.IO.Path.GetDirectoryName(path);
                        if (!System.IO.Directory.Exists(dir))
                            System.IO.Directory.CreateDirectory(dir);
                        System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Create);
                        stream.Write(downloading.bytes, 0, downloading.bytes.Length);
                        stream.Flush();
                        stream.Close();

                        string MD5 = GetMD5(downloading.bytes);
                        UpdateLocalMD5(bundleName, MD5);

                        // 每下载一个资源文件,都更新一下本地的VersionFile,防止中途退出又要重新下载
                        UpdateLocalVersionFile();
                    }

                    if (bundle != null)
                        mCacheBundle.Add(bundleName, bundle);
                }
            }
        }
        else
        {
            Debug.Log("Bundle has been loaded! file:"+bundleName);
        }

        if (cb_PerBundleLoaded != null && bundle == null)
        {
            Debug.LogError("No bundle!"+url);
        }

        mission.Done();
        if (cb_PerBundleLoaded != null)
            cb_PerBundleLoaded(bundleName, bundle, null, mission.mUserData);

        if (!mission.IsCompleted())
        {
            this.StartCoroutine(_DownloadAssetImpl(mission));
        }
        else
        {
            if (cb_all != null)
                cb_all(bundleName, mission.mUserData);

            mMissionList.RemoveFirst();
            if (mMissionList.Count > 0)
            {
                mission = mMissionList.First.Value;
                this.StartCoroutine(_DownloadAssetImpl(mission));
            }
        }
    }