| name | assets |
| description | 资产管理系统,负责加载和管理游戏资源。 |
资产管理系统
资产管理系统负责加载和管理游戏资源,包括纹理、材质、网格等。通过 Boo::assetManager 全局实例访问。
核心功能
资源加载
- 统一加载接口:
loadAsset 按路径或 UUID 自动识别类型并加载
- 资源获取:
getAsset 获取已加载的资源,可选自动加载
- 资源卸载:
unloadAsset 卸载指定资源
资产类型
- TextureAsset:纹理资产,支持静态和动态两种模式
- MaterialAsset:材质资产,支持设置纹理和颜色
- MeshAsset:网格资产,支持静态和动态网格
主要接口
AssetManager
Asset* loadAsset(const std::string& uuid);
Asset* getAsset(const std::string& uuid, bool loadIfNotFound = false);
void unloadAsset(const std::string& uuid);
void setAssetsRoot(const std::string& assetsRoot);
const std::string& getAssetsRoot();
TextureAsset
void create(int width, int height, int channels,
std::vector<uint8_t> pixelsVector, GfxTextureFormat format);
void createDynamic(int width, int height, int channels,
std::vector<uint8_t> pixelsVector, GfxTextureFormat format);
void create(const unsigned char* data, size_t size);
int getWidth();
int getHeight();
int channels();
GfxTexture* getGfxTexture();
MaterialAsset
void create(const json& materialData);
void create(MaterialAsset* mtl);
void setTexture(TextureAsset* texture);
void setTexture(const std::string& key, TextureAsset* texture);
void setUIColor(float r, float g, float b, float w);
GfxMaterial* getGfxMaterial();
MeshAsset
void create(std::vector<FMeshPrimitive> primitives);
void createDynamic(std::vector<FMeshPrimitive> primitives);
void updateDynamicSubMesh(int index, FMeshPrimitive& data);
size_t getSubMeshCount();
GfxMesh* getGfxMesh(int index);
内置资产常量
| 常量 | 描述 |
|---|
Boo::AssetBuiltinTexture::Default | 默认白色纹理 |
Boo::AssetBuiltinMaterial::UI | 内置 UI 材质 |
枚举类型
EAssetType
| 值 | 描述 |
|---|
None | 无 |
Texture | 纹理 |
Audio | 音频 |
Font | 字体 |
Shader | 着色器 |
Material | 材质 |
Scene | 场景 |
GLTF | GLTF 模型 |
Mesh | 网格 |
ETextureType
| 值 | 描述 |
|---|
Static | 静态纹理,GPU 上传后释放 CPU 数据 |
Dynamic | 动态纹理,保留 CPU 数据支持运行时修改 |
使用示例
加载纹理并设置到精灵
#include "engine/boo.h"
void loadResources() {
Boo::TextureAsset* tex = static_cast<Boo::TextureAsset*>(
Boo::assetManager->loadAsset("assets/textures/player.png")
);
Boo::UISprite* sprite = dynamic_cast<Boo::UISprite*>(node->addComponent("UISprite"));
if (sprite && tex) {
sprite->setTexture(tex);
}
}
加载材质
Boo::MaterialAsset* mat = static_cast<Boo::MaterialAsset*>(
Boo::assetManager->loadAsset("assets/materials/ui.mat")
);
Boo::TextureAsset* tex = static_cast<Boo::TextureAsset*>(
Boo::assetManager->getAsset("assets/textures/player.png", true)
);
mat->setTexture(tex);
使用内置资产
sprite->setTexture(Boo::AssetBuiltinTexture::Default);
sprite->setMaterial(Boo::AssetBuiltinMaterial::UI);
最佳实践
- 资源路径:使用相对路径,
setAssetsRoot 设置根目录后路径会自动拼接
- 避免重复加载:优先用
getAsset 获取已缓存资源
- 及时卸载:不再使用的资源调用
unloadAsset 释放内存
总结
资产管理系统通过 loadAsset/getAsset/unloadAsset 统一管理所有资产。内置常量 AssetBuiltinTexture::Default 和 AssetBuiltinMaterial::UI 可直接使用,无需加载。