Up to date

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

自定义资源格式加载器

前言

ResourceFormatLoader 是一个用来加载文件资源的工厂接口。资源是基本容器。当再次对同一文件路径调用 load 时,将引用先前加载的 Resource。自然,加载的资源必须是无状态的。

本指南假设读者已经知道如何创建 C++ 模块和 Godot 数据类型。如果你还不知道,请参考这份指南:自定义 C++ 模块

参考

可以做什么?

  • 添加对多种文件格式的新支持

  • 音频格式

  • 视频格式

  • 机器学习模型

不可以做什么?

  • 光栅图像

应使用ImageFormatLoader加载图像.

参考

创建 ResourceFormatLoader

每种文件格式都包含一个数据容器和一个 ResourceFormatLoader.

ResourceFormatLoaders are classes which return all the necessary metadata for supporting new extensions in Godot. The class must return the format name and the extension string.

此外,ResourceFormatLoaders 必须使用 load 函数将文件路径转换为资源(Resource). 要加载资源, load 必须能够读取和处理序列化的资源数据.

/* resource_loader_json.h */

#ifndef RESOURCE_LOADER_JSON_H
#define RESOURCE_LOADER_JSON_H

#include "core/io/resource_loader.h"

class ResourceFormatLoaderJson : public ResourceFormatLoader {
    GDCLASS(ResourceFormatLoaderJson, ResourceFormatLoader);
public:
    virtual RES load(const String &p_path, const String