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.

ResourceFormatLoader

繼承: RefCounted < Object

從檔案中載入特定資源型別。

說明

Godot 在編輯器或匯出的遊戲中使用 ResourceFormatLoaders 來載入資源。它們通過 ResourceLoader 單例自動查詢,或在載入具有內部依賴項的資源時自動查詢。每種文件型別可以作為不同的資源型別載入,因此在引擎中註冊多個 ResourceFormatLoaders。

擴充此類可讓你定義自己的載入器。請確保遵守文件中記錄的返回型別和值。你應該使用 class_name 給它一個全域類別名稱以進行註冊。像內建的 ResourceFormatLoaders 一樣,當載入其處理的型別的資源時,它將自動被呼叫。你還可以實作 ResourceFormatSaver

注意:如果你需要的資源型別存在,但 Godot 無法載入其格式,則也可以擴充 EditorImportPlugin。選擇哪種方法取決於該格式是否適用於最終匯出的遊戲。例如,最好首先將 .png 紋理作為 .ctexCompressedTexture2D)匯入,以便在圖形卡上能更高效的載入它們。

方法

bool

_exists(path: String) virtual const

PackedStringArray

_get_classes_used(path: String) virtual const

PackedStringArray

_get_dependencies(path: String, add_types: bool) virtual const

PackedStringArray

_get_recognized_extensions() virtual const

String

_get_resource_script_class(path: String) virtual const

String

_get_resource_type(path: String) virtual const

int

_get_resource_uid(path: String) virtual const

bool

_handles_type(type: StringName) virtual const

Variant

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

bool

_recognize_path(path: String, type: StringName) virtual const

Error

_rename_dependencies(path: String, renames: Dictionary) virtual const


列舉

enum CacheMode: 🔗

CacheMode CACHE_MODE_IGNORE = 0

Neither the main resource (the one requested to be loaded) nor any of its subresources are retrieved from cache nor stored into it. Dependencies (external resources) are loaded with CACHE_MODE_REUSE.

CacheMode CACHE_MODE_REUSE = 1

The main resource (the one requested to be loaded), its subresources, and its dependencies (external resources) are retrieved from cache if present, instead of loaded. Those not cached are loaded and then stored into the cache. The same rules are propagated recursively down the tree of dependencies (external resources).

CacheMode CACHE_MODE_REPLACE = 2

Like CACHE_MODE_REUSE, but the cache is checked for the main resource (the one requested to be loaded) as well as for each of its subresources. Those already in the cache, as long as the loaded and cached types match, have their data refreshed from storage into the already existing instances. Otherwise, they are recreated as completely new objects.

CacheMode CACHE_MODE_IGNORE_DEEP = 3

Like CACHE_MODE_IGNORE, but propagated recursively down the tree of dependencies (external resources).

CacheMode CACHE_MODE_REPLACE_DEEP = 4

Like CACHE_MODE_REPLACE, but propagated recursively down the tree of dependencies (external resources).


方法說明

bool _exists(path: String) virtual const 🔗

There is currently no description for this method. Please help us by contributing one!


PackedStringArray _get_classes_used(path: String) virtual const 🔗

There is currently no description for this method. Please help us by contributing one!


PackedStringArray _get_dependencies(path: String, add_types: bool) virtual const 🔗

Should return the dependencies for the resource at the given path. Each dependency is a string composed of one to three sections separated by ::, with trailing empty sections omitted:

  • The first section should contain the UID if the resource has one. Otherwise, it should contain the file path.

  • The second section should contain the class name of the dependency if add_types is true. Otherwise, it should be empty.

  • The third section should contain the fallback path if the resource has a UID. Otherwise, it should be empty.

func _get_dependencies(path, add_types):
    return [
        "uid://fqgvuwrkuixh::Script::res://script.gd",
        "uid://fqgvuwrkuixh::::res://script.gd",
        "res://script.gd::Script",
        "res://script.gd",
    ]

Note: Custom resource types defined by scripts aren't known by the ClassDB, so "Resource" can be used for the class name.


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 🔗

Should return the unique ID for the resource associated with the given path. If this method is not overridden, a .uid file is generated along with the resource file, containing the unique 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 required 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 常數。