Feature/tilemap editor (#237)
* feat: 添加 Tilemap 编辑器插件和组件生命周期支持 * feat(editor-core): 添加声明式插件注册 API * feat(editor-core): 改进tiledmap结构合并tileset进tiledmapeditor * feat: 添加 editor-runtime SDK 和插件系统改进 * fix(ci): 修复SceneResourceManager里变量未使用问题
This commit is contained in:
@@ -84,4 +84,31 @@ export abstract class Component implements IComponent {
|
||||
* 虽然保留此方法,但建议将复杂的清理逻辑放在 System 中处理。
|
||||
*/
|
||||
public onRemovedFromEntity(): void {}
|
||||
|
||||
/**
|
||||
* 组件反序列化后的回调
|
||||
*
|
||||
* 当组件从场景文件加载或快照恢复后调用,可以在此方法中恢复运行时数据。
|
||||
*
|
||||
* @remarks
|
||||
* 这是一个生命周期钩子,用于恢复无法序列化的运行时数据。
|
||||
* 例如:从图片路径重新加载图片尺寸信息,重建缓存等。
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* class TilemapComponent extends Component {
|
||||
* public tilesetImage: string = '';
|
||||
* private _tilesetData: TilesetData | undefined;
|
||||
*
|
||||
* public async onDeserialized(): Promise<void> {
|
||||
* if (this.tilesetImage) {
|
||||
* // 重新加载 tileset 图片并恢复运行时数据
|
||||
* const img = await loadImage(this.tilesetImage);
|
||||
* this.setTilesetInfo(img.width, img.height, ...);
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
public onDeserialized(): void | Promise<void> {}
|
||||
}
|
||||
|
||||
@@ -281,6 +281,40 @@ export class SceneSerializer {
|
||||
if (serializedScene.sceneData) {
|
||||
this.deserializeSceneData(serializedScene.sceneData, scene.sceneData);
|
||||
}
|
||||
|
||||
// 调用所有组件的 onDeserialized 生命周期方法
|
||||
// Call onDeserialized lifecycle method on all components
|
||||
const deserializedPromises: Promise<void>[] = [];
|
||||
for (const entity of entities) {
|
||||
this.callOnDeserializedRecursively(entity, deserializedPromises);
|
||||
}
|
||||
|
||||
// 如果有异步的 onDeserialized,在后台执行
|
||||
if (deserializedPromises.length > 0) {
|
||||
Promise.all(deserializedPromises).catch(error => {
|
||||
console.error('Error in onDeserialized:', error);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归调用实体及其子实体所有组件的 onDeserialized 方法
|
||||
*/
|
||||
private static callOnDeserializedRecursively(entity: Entity, promises: Promise<void>[]): void {
|
||||
for (const component of entity.components) {
|
||||
try {
|
||||
const result = component.onDeserialized();
|
||||
if (result instanceof Promise) {
|
||||
promises.push(result);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error calling onDeserialized on component ${component.constructor.name}:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
for (const child of entity.children) {
|
||||
this.callOnDeserializedRecursively(child, promises);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user