ShitEngine
基于 C++20 与 SDL3 的轻量级 2D 游戏引擎
载入中...
搜索中...
未找到
Prefab类 参考

预制体,可重复生成相同配置的 GameObject 更多...

#include <Prefab.h>

struct  ComponentData
 单个组件的数据(类型名 + 反射字段值,JSON 可序列化) 更多...

Public 成员函数

nlohmann::json toJson () const
 序列化为 JSON
void apply (GameObject *go) const
 把此预制体应用到已有 GameObject(反射克隆)
GameObjectinstantiate (Scene *scene, const std::string &name="") const
 创建新 GameObject 并应用此预制体
bool hasData () const
 是否包含反射数据
const std::vector< ComponentData > & getComponents () const
 已捕获的组件数据(只读,供编辑器序列化/预览)

静态 Public 成员函数

static Prefab Capture (GameObject *source)
 从现有 GameObject 捕获组件与字段(跳过 readOnly 字段与不可序列化类型)
static Prefab FromJson (const nlohmann::json &j)
 从 JSON 反序列化

详细描述

预制体,可重复生成相同配置的 GameObject

数据驱动:从现有 GameObject 捕获全部反射组件及字段值,实例化时通过 反射工厂 + 字段拷贝重建。支持 JSON 序列化(编辑器保存/加载预制体)。

auto prefab = Shit::Prefab::Capture(enemyGO); // 捕获 auto prefab = Shit::Prefab::FromJson(jsonData); // 或从 JSON 反序列化 auto* enemy = prefab.instantiate(scene, "enemy"); // 实例化 auto json = prefab.toJson(); // 序列化

成员函数说明

◆ apply()

void apply ( GameObject * go) const

把此预制体应用到已有 GameObject(反射克隆)

◆ Capture()

Prefab Capture ( GameObject * source)
static

从现有 GameObject 捕获组件与字段(跳过 readOnly 字段与不可序列化类型)

◆ FromJson()

Prefab FromJson ( const nlohmann::json & j)
static

从 JSON 反序列化

◆ getComponents()

const std::vector< ComponentData > & getComponents ( ) const
inline

已捕获的组件数据(只读,供编辑器序列化/预览)

◆ hasData()

bool hasData ( ) const
inline

是否包含反射数据

◆ instantiate()

GameObject * instantiate ( Scene * scene,
const std::string & name = "" ) const

创建新 GameObject 并应用此预制体

◆ toJson()

nlohmann::json toJson ( ) const

序列化为 JSON


该类的文档由以下文件生成:
  • include/ShitEngine/GameObject/Prefab.h