예제 #1
0
        public void UpdateRes(Action <Boolean> _onCompleted)
        {
            Log.Debug("开始资源更新准备工作");
            mOnUpdateCompleted = _onCompleted;
            var appConfigAb = AssetBundle.LoadFromFile(
                ResInfo.GetSystemApiLoadPath("res_config.grt", AssetLocation.StreamingPath));

            if (appConfigAb == null)
            {
                Log.Critical(eLogType.Resources, "加载 In App res config 失败!!!");
                appConfigAb.Unload(true);
                return;
            }

            mInAppConfig = ResConfigFile.Create(appConfigAb.LoadAsset <TextAsset>("res_config").bytes);
            if (appConfigAb == null)
            {
                Log.Critical(eLogType.Resources, "解析 In App res config 失败!!!");
                appConfigAb.Unload(true);
                return;
            }
            appConfigAb.Unload(true);
            mLocalConfig = ResConfigFile.Create(ResInfo.GetSystemApiLoadPath("res_config", AssetLocation.PersistentPath), true);

            if (mLocalConfig == null)
            {
                Log.Critical(eLogType.Resources, "创建 Local res config 失败!!!");
                return;
            }

            StartCoroutine(checkUpdate());
        }
예제 #2
0
        private IEnumerator checkUpdate()
        {
            Log.Debug("开始检查资源更新");
            using (UnityWebRequest uwr = UnityWebRequest.GetAssetBundle(ResUtility.ResConfigDownloadUrl))
            {
                yield return(uwr.Send());

                if (uwr.isError)
                {
                    Debug.Log(uwr.error);
                    showCheckUpdateFailTip();
                    yield break;
                }
                else
                {
                    AssetBundle srvConfigAb = ((DownloadHandlerAssetBundle)uwr.downloadHandler).assetBundle;
                    if (srvConfigAb == null)
                    {
                        showCheckUpdateFailTip();
                        yield break;
                    }
                    TextAsset asset = srvConfigAb.LoadAsset <TextAsset>("res_config");
                    if (asset == null)
                    {
                        showCheckUpdateFailTip();
                        srvConfigAb.Unload(true);
                        yield break;
                    }
                    mServerConfig = ResConfigFile.Create(asset.bytes);
                    srvConfigAb.Unload(true);
                    if (mServerConfig == null)
                    {
                        showCheckUpdateFailTip();
                        yield break;
                    }
                }
            }

            // 处理更新列表
            Boolean localResConfigUpdated;

            mUpdateTotalSize = mLocalConfig.GetUpdateList(mInAppConfig, mServerConfig, out mUpdateList, out localResConfigUpdated);
            if (mUpdateTotalSize == 0)
            {
                mOnUpdateCompleted(localResConfigUpdated);
            }
            else
            {
                showUpdateTip();
            }
        }
예제 #3
0
        /// <summary>
        /// 加载一个最新的package
        /// </summary>
        /// <returns></returns>
        public static ResUpdatePkg LoadLastPkg()
        {
            var          resConfigPath = String.Concat(OUTPUT_ROOT, "/res_config.grt");
            ResUpdatePkg pkg           = new ResUpdatePkg();

            if (!File.Exists(resConfigPath))
            {
                Log.Info("没有更新包!");
                return(null);
            }
            else
            {
                // 直接读取txt
                pkg.m_res_config = ResConfigFile.Create(String.Concat(OUTPUT_ROOT, "/res_config.txt"), false);
                pkg.Version      = pkg.m_res_config.ResVersion;
                if (pkg.m_res_config == null)
                {
                    Log.Error("读取最新更新包失败!");
                    return(null);
                }
            }
            return(pkg);
        }
예제 #4
0
        /// <summary>
        /// 初始化ResourceManager, 加载包内和包外的res_config
        /// </summary>
        /// <param name="_action_initialized"></param>
        /// <returns></returns>
        private IEnumerator _asyncInitialize(Action <Boolean> _action_initialized)
        {
            String app_res_config_path = ResInfo.GetWWWLoadPath("res_config", AssetLocation.StreamingPath);

            Log.Debug(eLogType.Resources, app_res_config_path);
            using (WWW www = new WWW(app_res_config_path))
            {
                yield return(www);

                if (!String.IsNullOrEmpty(www.error))                    // 加载错误
                {
                    Log.Error(eLogType.Resources, "[ResourceManager]: In App res_config assetbundle 加载失败!: {0}", www.error);
                    _action_initialized(false);
                    yield break;
                }


                ResConfigFile app_res_config = ResConfigFile.Create(www.bytes);
                if (app_res_config == null)                  // 解析错误
                {
                    Log.Error(eLogType.Resources, "[ResourceManager]: In App res_config 解析错误!");
                    _action_initialized(false);
                    yield break;
                }

                mLoadResConfig = ResConfigFile.Create(ResUtility.LocalResConfigPath, true);
                if (mLoadResConfig == null)                  // 保存用assetsconfig 创建或读取错误
                {
                    app_res_config = null;
                    Log.Error(eLogType.Resources, "[ResouceManager]: Saved res_config file 创建或解析错误!");
                    _action_initialized(false);
                    yield break;
                }
                _action_initialized(true);
            }
        }
예제 #5
0
        /// <summary>
        /// 初始化资源管理器
        /// </summary>
        /// <param name="_updateByAppConfig">是否使用app内config更新列表,在更新完成之后重新初始化时不需要更新</param>
        /// <returns></returns>
        public Boolean Initialize(Boolean _updateByAppConfig)
        {
            String appResConfigPath        = String.Concat(Application.streamingAssetsPath, "/res/res_config.grt");
            var    assetbundleAppResConfig = AssetBundle.LoadFromFile(appResConfigPath);

            if (assetbundleAppResConfig == null)
            {
                Log.Critical("[ResourceManager.Initialize]:读取inapp res config 失败! assetbundle 加载失败!");
                return(false);
            }

            var appResConfigBytes = assetbundleAppResConfig.LoadAsset <TextAsset>("res_config");

            if (appResConfigBytes == null)
            {
                Log.Critical("[ResourceManager.Initialize]:读取inapp res config 失败! TextAsset加载失败");
                assetbundleAppResConfig.Unload(true);
                return(false);
            }
            var appResConfig = ResConfigFile.Create(appResConfigBytes.bytes);

            if (appResConfig == null)
            {
                Log.Critical(eLogType.Resources, "[ResourceManager.Initialize]: 读取inapp res config 失败! 解析失败!");
                assetbundleAppResConfig.Unload(true);
                return(false);
            }
            assetbundleAppResConfig.Unload(true);


            mLoadResConfig = ResConfigFile.Create(ResUtility.LocalResConfigPath, true);
            if (mLoadResConfig == null)
            {
                Log.Error(eLogType.Resources, "[ResouceManager]: Local res config file 创建失败!");
                return(false);
            }

            // 首次初始化时执行
            if (_updateByAppConfig)
            {
                mLoadResConfig.UpdateByAppResConfig(appResConfig);
            }


            mLoadResConfig.TraverseAssets(_info => {
                m_res_dictionary.Add(_info.Name, _createResourceData(_info));
            });

            // 读取依赖
            var assetbundleManifest      = AssetBundle.LoadFromFile(GetResource("res.grt").ResouceInfo.SystemApiLoadingPath);
            AssetBundleManifest manifest = assetbundleManifest.LoadAsset <AssetBundleManifest>("AssetBundleManifest");

            foreach (Resource_Base res in m_res_dictionary.Values)
            {
                if (res.ResouceInfo.ResType != ResourceType.AssetBundle)
                {
                    continue;
                }
                Resource_Assetbundle resAssetbundle = res as Resource_Assetbundle;
                var dependenciesName = manifest.GetDirectDependencies(res.Name);
                for (int i = 0; i < dependenciesName.Length; i++)
                {
                    Resource_Assetbundle dependency = GetResource <Resource_Assetbundle>(dependenciesName[i]);
                    if (dependency == null)
                    {
                        Log.Critical(eLogType.Resources, "[ResourceManager.Initialize]: {0}的依赖资源: {1}没有在ResourceConfig中找到! 请尝试重新打包!", res.Name, dependenciesName[i]);
                        return(false);
                    }
                    resAssetbundle.AddDependencies(dependency);
                }
            }
            assetbundleManifest.Unload(true);

            Log.Debug(eLogType.Resources, "ResourceManager Initialized Done!");
            return(true);
        }
예제 #6
0
        public static void BuildNew(ResUpdatePkg _old_pkg)
        {
            //AssetBundle bundle = AssetBundle.LoadFromFile( String.Concat( OUTPUT_TMP, "/data/global_configtest" ) );
            //TextAsset g = bundle.LoadAsset<TextAsset>( "global_configtest" );
            ////TextAsset r = bundle.LoadAsset<TextAsset>( "rookie_config" );
            //bundle.Unload( true );
            ////AssetBundle bundle = AssetBundle.LoadFromFile( String.Concat( OUTPUT_TMP, "/ui/ui_main.ui" ) );
            //return;
            FileHelper.DeleteDirectory(OUTPUT_TMP, true);
            FileHelper.CreateDirectory(OUTPUT_TMP);

            // 打包Assetbundle
            BuildPipeline.BuildAssetBundles(OUTPUT_TMP, BUILD_OPTIONS, BUILD_TARGET);
            AssetBundle assetbundleManifest = null;

            assetbundleManifest = AssetBundle.LoadFromFile(String.Concat(OUTPUT_TMP, "/res"));
            AssetBundleManifest manifest = assetbundleManifest.LoadAsset <AssetBundleManifest>("AssetBundleManifest");

            String[] allAssetBundles = manifest.GetAllAssetBundles();

            // 添加Assetbundle到config
            var newPkg = new ResUpdatePkg {
                Version = _old_pkg == null ? 1 : _old_pkg.Version + 1, m_pre_version = _old_pkg
            };

            newPkg.m_res_config = ResConfigFile.Create(String.Concat(OUTPUT_TMP, "/res_config.txt"), true);

            foreach (String assetBundle in allAssetBundles)
            {
                newPkg.m_res_config.BuidlerAndResInfo(OUTPUT_TMP, assetBundle, newPkg.Version);
            }

            assetbundleManifest.Unload(true);

            File.Move(String.Concat(OUTPUT_TMP, "/res"), String.Concat(OUTPUT_TMP, "/res.grt"));
            newPkg.m_res_config.BuidlerAndResInfo(OUTPUT_TMP, "res.grt", newPkg.Version);

            // 打包lua文件
            GrtFileEditor.Pack("Assets/res/lua", OUTPUT_TMP, "scripts/ui.grt");
            newPkg.m_res_config.BuidlerAndResInfo(OUTPUT_TMP, "scripts/ui.grt", newPkg.Version);

            Boolean hasNewRes = true;

            // 处理res_config
            if (newPkg.m_pre_version != null)
            {
                hasNewRes = newPkg.m_res_config.BuilderMergeOld(_old_pkg.m_res_config);
            }

            if (!hasNewRes)
            {
                EditorUtility.DisplayDialog("", "资源无更新!", "确定");
                return;
            }

            // 保存到版本目录
            String version_root = String.Concat(OUTPUT_ROOT, "/", newPkg.Version.ToString());

            FileHelper.CreateDirectory(version_root);

            // 临时保存到Assets下以便打包
            newPkg.m_res_config.SaveAs("Assets/res_config.txt", true);
            File.Copy("Assets/res_config.txt", String.Concat(version_root, "/res_config.txt"), true);
            File.Copy("Assets/res_config.txt", String.Concat(OUTPUT_ROOT, "/res_config.txt"), true);

            AssetDatabase.Refresh();
            BuildPipeline.BuildAssetBundles(
                OUTPUT_ROOT,
                new AssetBundleBuild[] { new AssetBundleBuild()
                                         {
                                             assetBundleName = "res_config.grt", assetNames = new[] { "Assets/res_config.txt" }
                                         } },
                BUILD_OPTIONS,
                BUILD_TARGET
                );

            AssetDatabase.Refresh();
            File.Copy(String.Concat(OUTPUT_ROOT, "/res_config.grt"), String.Concat(version_root, "/res_config.grt"), true);


            // 保存的根目录
            //new_pkg.m_res_config.SaveAs( String.Concat( OUTPUT_ROOT, "/res_config" ), true );

            FileHelper.CreateDirectory(version_root);
            // copy main assetbundle manifest
            //File.Copy( String.Concat( OUTPUT_TMP, "/res" ), String.Concat( version_root, "/res" ), true );

            newPkg.m_res_config.TraverseAssets(
                _info => {
                if (_info.Version != newPkg.Version)
                {
                    return;
                }
                String dest = String.Concat(version_root, "/", _info.Name);
                FileHelper.CreateDirectoryByFilePath(dest);
                File.Copy(String.Concat(OUTPUT_TMP, "/", _info.Name), dest, true);
            });

            // 删除指定Assetbundle打包后生成的信息文件(没用)
            File.Delete(String.Concat(OUTPUT_ROOT, "/update_pkg"));
            File.Delete(String.Concat(OUTPUT_ROOT, "/update_pkg.manifest"));

            File.Delete("Assets/res_config.txt");
            File.Delete("Assets/res_config.grt");

            AssetDatabase.Refresh();


            EditorUtility.DisplayDialog("成功", "打包完成!", "确定");
        }