Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
ResourceFormatLoader
继承: RefCounted < Object
从文件中加载特定资源类型。
描述
Godot 在编辑器或导出的游戏中使用 ResourceFormatLoaders 来加载资源。它们通过 ResourceLoader 单例自动查询,或在加载具有内部依赖项的资源时自动查询。每种文件类型可以作为不同的资源类型加载,因此在引擎中注册多个 ResourceFormatLoaders。
扩展此类可让你定义自己的加载器。请确保遵守文档中记录的返回类型和值。你应该使用 class_name
给它一个全局类名以进行注册。像内置的 ResourceFormatLoaders 一样,当加载其处理的类型的资源时,它将自动被调用。你还可以实现 ResourceFormatSaver。
注意:如果你需要的资源类型存在,但 Godot 无法加载其格式,则也可以扩展 EditorImportPlugin。选择哪种方法取决于该格式是否适用于最终导出的游戏。例如,最好首先将 .png
纹理作为 .ctex
(CompressedTexture2D)导入,以便在图形卡上能更高效的加载它们。
方法
_get_classes_used(path: String) virtual const |
|
_get_dependencies(path: String, add_types: bool) virtual const |
|
_get_recognized_extensions() virtual const |
|
_get_resource_script_class(path: String) virtual const |
|
_get_resource_type(path: String) virtual const |
|
_get_resource_uid(path: String) virtual const |
|
_handles_type(type: StringName) virtual const |
|
_load(path: String, original_path: String, use_sub_threads: bool, cache_mode: int) virtual const |
|
_recognize_path(path: String, type: StringName) virtual const |
|
_rename_dependencies(path: String, renames: Dictionary) virtual const |
枚举
enum CacheMode: 🔗
CacheMode CACHE_MODE_IGNORE = 0
主资源(请求加载的资源)及所有子资源都既不会从缓存中检索也不会存储到缓存中。依赖项(外部资源)使用 CACHE_MODE_REUSE 加载。
CacheMode CACHE_MODE_REUSE = 1
主资源(请求加载的资源)、子资源、依赖项(外部资源)如果在缓存中存在,则将从缓存中检索,不进行加载。如果未缓存则将进行加载,然后存储到缓存中。相同的规则将沿着依赖关系树(外部资源)递归传播。
CacheMode CACHE_MODE_REPLACE = 2
与 CACHE_MODE_REUSE 类似,但会检查主资源(请求加载的资源)及每个子资源的缓存。如果在缓存中存在,只要加载的类型和缓存的类型匹配,就会使用存储的数据刷新已经存在的实例。否则会重新作为全新的对象创建。
CacheMode CACHE_MODE_IGNORE_DEEP = 3
与 CACHE_MODE_IGNORE 类似,但沿依赖关系树(外部资源)递归传播。
CacheMode CACHE_MODE_REPLACE_DEEP = 4
与 CACHE_MODE_REPLACE 类似,但沿依赖关系树(外部资源)递归传播。
方法说明
bool _exists(path: String) virtual const 🔗
该方法目前没有描述,请帮我们贡献一个吧!
PackedStringArray _get_classes_used(path: String) virtual const 🔗
该方法目前没有描述,请帮我们贡献一个吧!
PackedStringArray _get_dependencies(path: String, add_types: bool) virtual const 🔗
如果实现,则获取给定资源的依赖项。如果 add_types
为 true
,路径应追加 ::TypeName
,其中 TypeName
是依赖项的类名。
注意:脚本定义的自定义资源类型并不为 ClassDB 所知,因此可能只能为它们返回 "Resource"
。
PackedStringArray _get_recognized_extensions() virtual const 🔗
获取该加载器能够读取的文件的扩展名列表。
String _get_resource_script_class(path: String) virtual const 🔗
返回与给定的 path
下的 Resource 关联的脚本类名称。如果资源没有脚本或脚本不是一个命名的类,则应返回 ""
。
String _get_resource_type(path: String) virtual const 🔗
获取与给定路径相关的资源的类名。如果加载器不能处理它,它应该返回 ""
。
注意:ClassDB 不知道脚本定义的自定义资源类型,因此你可能只为它们返回 "Resource"
。
int _get_resource_uid(path: String) virtual const 🔗
应返回与给定路径关联的资源的唯一 ID。如果未覆盖该方法,则会在资源文件旁生成一个 .uid
文件,该文件中包含唯一 ID。
bool _handles_type(type: StringName) virtual const 🔗
说明这个加载器可以加载哪个资源类。
注意:ClassDB 不知道脚本定义的自定义资源类型,因此你可以只为它们处理 "Resource"
。
Variant _load(path: String, original_path: String, use_sub_threads: bool, cache_mode: int) virtual const 🔗
当引擎发现该加载程序兼容时加载资源。如果加载的资源是导入的结果,original_path
将以源文件为目标。成功时返回一个 Resource 对象,失败时返回一个 Error 常量。
cache_mode
属性定义加载资源时是否以及如何使用或更新缓存。有关详细信息,请参阅 CacheMode。
bool _recognize_path(path: String, type: StringName) virtual const 🔗
判断该加载器是否应对于给定类型从其资源路径加载资源。
如果未实现,则默认行为是检查路径的扩展名是否在_get_recognized_extensions()提供的范围内,以及类型是否在_get_resource_type()提供的范围内。
Error _rename_dependencies(path: String, renames: Dictionary) virtual const 🔗
如果被实现,将重命名给定资源中的依赖项并保存它。renames
是一个字典 { String => String }
,将旧依赖路径映射到新路径。
成功时返回 @GlobalScope.OK,失败时返回 Error 常量。