Example #1
0
        /// <summary>
        /// init
        /// </summary>
        public void Init()
        {
            MappingRes();

            // 发送消息,模拟更新完成
            AppEvent.BroadCastEvent(AssetBundleDefine.EVENT_UI_UPDATE_COMPOLETE);
        }
    /// <summary>
    /// 游戏中所有资源下载完成
    /// </summary>
    /// <param name="_s"></param>
    private void GameingDownloadComplete(string _s)
    {
        if (false == string.IsNullOrEmpty(_s))
        {
            this.Error("游戏中下载数据有失败信息 = " + _s);
            return;
        }

        // 游戏中资源下载完成
        AppEvent.BroadCastEvent(AssetBundleDefine.EVENT_UPDATESUCCESSONGAMING);
    }
    /// <summary>
    /// AssetBundle下映射资源
    /// 1.处理资源名字和资源路径的映射
    /// 2.处理ab包和其他ab包的映射
    /// </summary>
    public void MappingRes()
    {
        // 组织AssetBundle依赖关系(manifest.bytes)
        AssetBundleDependent();

        // 映射资源和AssetBundle的关系(version.bytes)
        MappingAssetToAssetBundle(() =>
        {
            // 通知ui层更新完成
            AppEvent.BroadCastEvent(AssetBundleDefine.EVENT_UI_UPDATE_COMPOLETE, null);
        });
    }
    /// <summary>
    /// 单个资源下载完成
    /// 将文件写入本地
    /// 如果有压缩或者加密,可以在这里进行解析
    /// </summary>
    /// <param name="s"></param>
    /// <param name="o"></param>
    /// <param name="arg3"></param>
    private void SingleAssetComplete(string s, object o, object arg3)
    {
        byte[] bytes = (byte[])o;
        AssetBundleVersionItem downloadItem = (AssetBundleVersionItem)arg3;

        string localItemPath = GetFileLocalPath(downloadItem);

        // 写入到本地
        App.Make <IFileDiskSystem>().CreateFile(localItemPath, bytes);

        if (downloadItem.AssetPathType == AssetPathType.gameLoad)
        {
            // 广播,通知临时资源使用中心来替换临时资源(一般为模型,音效,UI等)
            AppEvent.BroadCastEvent(string.Format("{0}_{1}", AssetBundleDefine.ABDownloadSuccess, localItemPath));
        }
    }
    /// <summary>
    /// 所有资源下载完成
    /// </summary>
    /// <param name="s"></param>
    private void GameStartDownloadComplete(string s)
    {
        if (false == string.IsNullOrEmpty(s))
        {
            this.Error("游戏开始下载数据有失败信息 = " + s);
            // 这里如果return可能会导致整个游戏流程中断
        }

        // 游戏启动下载资源完成
        AppEvent.BroadCastEvent(AssetBundleDefine.EVENT_UPDATE_SUCCESS_ON_GAMESTART);

        // 统计游戏中需要下载的的资源
        var gameLoaDownloadList = CheckDownloadList(serverVersion.AssetBundleVersionItems, AssetPathType.gameLoad);

        if (null != gameLoaDownloadList && gameLoaDownloadList.Count > 0)
        {
            if (null != downloadInfo)
            {
                downloadInfo.SetDownloadList(AssetPathType.gameLoad, gameLoaDownloadList);
            }
            this.Info("游戏中需要下载资源统计完成 数量 = " + gameLoaDownloadList.Count);
        }
    }
    /// <summary>
    /// 资源更新入口
    /// </summary>
    public void UpdateGameResources()
    {
        string versionFile = string.Format("{0}/{1}/{2}", AssetBundleDefine.SERVER_RES_PATH, AssetBundleDefine.RuntimePlatformName(), AssetBundleDefine.VERSION_FILE);

        this.Info("version file path = " + versionFile);
        App.Make <IHttpRequest>().GetBytes(versionFile, bytes =>
        {
            if (null == bytes || bytes.Length <= 0)
            {
                this.Error("Version文件下载失败");
                return;
            }

            var decompressBytes   = GameFramework.Utility.Zip.Decompress(bytes);
            string versionContent = System.Text.Encoding.UTF8.GetString(decompressBytes);
            if (true == string.IsNullOrEmpty(versionContent))
            {
                this.Error("Version文件解析失败");
                return;
            }

            // 存入本地,并不是做对比使用,而是name和path映射做准备
            App.Make <IFileDiskSystem>()
            .CreateFile(string.Format("{0}/{1}/{2}", AssetBundleDefine.AssetBundlesDir, AssetBundleDefine.RuntimePlatformName(), AssetBundleDefine.VERSION_FILE),
                        decompressBytes);

            serverVersion = JsonUtility.FromJson <AssetBundleVersion>(versionContent);
            if (null == serverVersion)
            {
                this.Error("version文件转换失败");
                return;
            }

            var downloadList = CheckDownloadList(serverVersion.AssetBundleVersionItems, AssetPathType.persisent);
            downloadInfo.SetDownloadList(AssetPathType.persisent, downloadList);

            if (downloadList.Count > 0)
            {
                this.Debug("总计下载文件个数 = " + downloadList.Count + "  大小 = " + downloadInfo.DownloadSizeCountDes);
                // 这里要根据网络情况,判断是否直接下载

                if (UnityEngine.Application.internetReachability == NetworkReachability.ReachableViaLocalAreaNetwork)                 // 移动网络
                {
                    // 开始下载
                    AppEvent.BroadCastEvent(AssetBundleDefine.EVENT_GAME_START_RESOURCES_UPDATE);
                }
                else if (UnityEngine.Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork)                  // WIFI网络
                {
                    // 询问玩家
                    AppEvent.BroadCastEvent(AssetBundleDefine.EVENT_ASK_UPDATE_RESOURCES, downloadInfo);
                }
            }
            else
            {
                GameStartDownloadComplete(null);                 // 没有最新资源需要下载
            }
        }, _errorCode =>
        {
            this.Error("Version文件下载失败 error code = " + _errorCode);
        });
    }
Example #7
0
 public void UnLoad()
 {
     AppEvent.BroadCastEvent("unload_asset", SourceDataName);
 }