Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

ResourceFormatLoader

继承: RefCounted < Object

从文件中加载特定资源类型。

描述

Godot 在编辑器或导出的游戏中使用 ResourceFormatLoaders 来加载资源。它们通过 ResourceLoader 单例自动查询,或在加载具有内部依赖项的资源时自动查询。每种文件类型可以作为不同的资源类型加载,因此在引擎中注册多个 ResourceFormatLoaders。

扩展此类可让你定义自己的加载器。请确保遵守文档中记录的返回类型和值。你应该使用 class_name 给它一个全局类名以进行注册。像内置的 ResourceFormatLoaders 一样,当加载其处理的类型的资源时,它将自动被调用。你还可以实现 ResourceFormatSaver

注意:如果你需要的资源类型存在,但 Godot 无法加载其格式,则也可以扩展 EditorImportPlugin。选择哪种方法取决于该格式是否适用于最终导出的游戏。例如,最好首先将 .png 纹理作为 .ctexCompressedTexture2D)导入,以便在图形卡上能更高效的加载它们。

方法

bool

_exists ( String path ) virtual const

PackedStringArray

_get_classes_used ( String path ) virtual const

PackedStringArray

_get_dependencies ( String path, bool add_types ) virtual const

PackedStringArray

_get_recognized_extensions ( ) virtual const

String

_get_resource_script_class ( String path ) virtual const

String

_get_resource_type ( String path ) virtual const

int

_get_resource_uid ( String path ) virtual const

bool

_handles_type ( StringName type ) virtual const

Variant

_load ( String path, String original_path, bool use_sub_threads, int cache_mode ) virtual const

bool

_recognize_path ( String path, StringName type ) virtual const

Error

_rename_dependencies ( String path, Dictionary renames ) virtual const


枚举

enum CacheMode:

CacheMode CACHE_MODE_IGNORE = 0

CacheMode CACHE_MODE_REUSE = 1

CacheMode CACHE_MODE_REPLACE = 2


方法说明

bool _exists ( String path ) virtual const

目前没有这个方法的描述。请帮我们贡献一个


PackedStringArray _get_classes_used ( String path ) virtual const

目前没有这个方法的描述。请帮我们贡献一个


PackedStringArray _get_dependencies ( String path, bool add_types ) virtual const

如果实现,则获取给定资源的依赖项。如果 add_typestrue,路径应追加 ::TypeName,其中 TypeName 是依赖项的类名。

注意:脚本定义的自定义资源类型并不为 ClassDB 所知,因此可能只能为它们返回 "Resource"


PackedStringArray _get_recognized_extensions ( ) virtual const

获取该加载器能够读取的文件的扩展名列表。


String _get_resource_script_class ( String path ) virtual const

返回与给定的 path 下的 Resource 关联的脚本类名称。如果资源没有脚本或脚本不是一个命名的类,则应返回 ""


String _get_resource_type ( String path ) virtual const

获取与给定路径相关的资源的类名。如果加载器不能处理它,它应该返回 ""

注意:ClassDB 不知道脚本定义的自定义资源类型,因此你可能只为它们返回 "Resource"


int _get_resource_uid ( String path ) virtual const

目前没有这个方法的描述。请帮我们贡献一个


bool _handles_type ( StringName type ) virtual const

说明这个加载器可以加载哪个资源类。

注意:ClassDB 不知道脚本定义的自定义资源类型,因此你可以只为它们处理 "Resource"


Variant _load ( String path, String original_path, bool use_sub_threads, int cache_mode ) virtual const

当引擎发现该加载程序兼容时加载资源。如果加载的资源是导入的结果,original_path 将以源文件为目标。成功时返回一个 Resource 对象,失败时返回一个 Error 常量。

cache_mode 属性定义加载资源时是否以及如何使用或更新缓存。有关详细信息,请参阅 CacheMode


bool _recognize_path ( String path, StringName type ) virtual const

判断该加载器是否应对于给定类型从其资源路径加载资源。

如果未实现,则默认行为是检查路径的扩展名是否在_get_recognized_extensions提供的范围内,以及类型是否在_get_resource_type提供的范围内。


Error _rename_dependencies ( String path, Dictionary renames ) virtual const

如果被实现,将重命名给定资源中的依赖项并保存它。renames 是一个字典 { String => String },将旧依赖路径映射到新路径。

成功时返回 @GlobalScope.OK,失败时返回 Error 常量。