diff --git a/src/base/assets_manager.c b/src/base/assets_manager.c index 31a80a618..7eae5c90f 100644 --- a/src/base/assets_manager.c +++ b/src/base/assets_manager.c @@ -544,6 +544,11 @@ asset_info_t* assets_manager_load_ex(assets_manager_t* am, asset_type_t type, ui return am->custom_load_asset(am->custom_load_asset_ctx, type, subtype, name); } info = assets_manager_load_impl(am, type, subtype, name); + + if (info == NULL && am->fallback_load_asset != NULL) { + info = am->fallback_load_asset(am->fallback_load_asset_ctx, type, subtype, name); + } + if (info != NULL) { assets_manager_dispatch_event(am, EVT_ASSET_MANAGER_LOAD_ASSET, info); } @@ -832,6 +837,16 @@ ret_t assets_manager_set_custom_load_asset(assets_manager_t* am, return RET_OK; } +ret_t assets_manager_set_fallback_load_asset(assets_manager_t* am, + assets_manager_load_asset_t fallback_load_asset, + void* ctx) { + return_value_if_fail(am != NULL, RET_BAD_PARAMS); + am->fallback_load_asset_ctx = ctx; + am->fallback_load_asset = fallback_load_asset; + + return RET_OK; +} + ret_t assets_manager_destroy(assets_manager_t* am) { return_value_if_fail(am != NULL, RET_BAD_PARAMS); emitter_deinit(EMITTER(am)); diff --git a/src/base/assets_manager.h b/src/base/assets_manager.h index 31a01acd6..c5e4bca6b 100644 --- a/src/base/assets_manager.h +++ b/src/base/assets_manager.h @@ -84,6 +84,9 @@ struct _assets_manager_t { void* custom_load_asset_ctx; assets_manager_load_asset_t custom_load_asset; + + void* fallback_load_asset_ctx; + assets_manager_load_asset_t fallback_load_asset; void* custom_build_asset_dir_ctx; assets_manager_build_asset_dir_t custom_build_asset_dir; @@ -331,6 +334,19 @@ ret_t assets_manager_set_custom_load_asset(assets_manager_t* am, assets_manager_load_asset_t custom_load_asset, void* ctx); +/** + * @method assets_manager_set_fallback_load_asset + * 设置一个函数,该函数在找不到资源时加载后补资源。 + * + * @param {assets_manager_t*} am asset manager对象。 + * @param {assets_manager_load_asset_t} fallback_load_asset 回调函数。 + * @param {void*} ctx 回调函数的上下文。 + * + * @return {ret_t} 返回RET_OK表示成功,否则表示失败。 + */ +ret_t assets_manager_set_fallback_load_asset(assets_manager_t* am, + assets_manager_load_asset_t fallback_load_asset, + void* ctx); /** * @method assets_manager_clear_cache * 清除指定类型的缓存。