feat(engine): 添加编辑器模式标志控制编辑器UI显示 (#274)
* feat(engine): 添加编辑器模式标志控制编辑器UI显示 - 在 Rust 引擎中添加 isEditor 标志,控制网格、gizmos、坐标轴指示器的显示 - 运行时模式下自动隐藏所有编辑器专用 UI - 编辑器预览和浏览器运行时通过 setEditorMode(false) 禁用编辑器 UI - 添加 Scene.isEditorMode 延迟组件生命周期回调,直到 begin() 调用 - 修复用户组件注册到 Core ComponentRegistry 以支持序列化 - 修复 Run in Browser 时用户组件加载问题 * fix: 复制引擎模块的类型定义文件到 dist/engine * fix: 修复用户项目 tsconfig paths 类型定义路径 - 从 module.json 读取实际包名而不是使用目录名 - 修复 .d.ts 文件复制逻辑,支持 .mjs 扩展名
This commit is contained in:
@@ -375,7 +375,15 @@ export class Entity {
|
||||
if (this.scene.referenceTracker) {
|
||||
this.scene.referenceTracker.registerEntityScene(this.id, this.scene);
|
||||
}
|
||||
component.onAddedToEntity();
|
||||
|
||||
// 编辑器模式下延迟执行 onAddedToEntity | Defer onAddedToEntity in editor mode
|
||||
if (this.scene.isEditorMode) {
|
||||
this.scene.queueDeferredComponentCallback(() => {
|
||||
component.onAddedToEntity();
|
||||
});
|
||||
} else {
|
||||
component.onAddedToEntity();
|
||||
}
|
||||
|
||||
if (this.scene && this.scene.eventSystem) {
|
||||
this.scene.eventSystem.emitSync('component:added', {
|
||||
|
||||
@@ -78,6 +78,18 @@ export interface IScene {
|
||||
*/
|
||||
readonly services: ServiceContainer;
|
||||
|
||||
/**
|
||||
* 编辑器模式标志
|
||||
*
|
||||
* 当为 true 时,组件的生命周期回调(如 onAddedToEntity)会被延迟,
|
||||
* 直到调用 begin() 开始运行场景时才会触发。
|
||||
*
|
||||
* Editor mode flag.
|
||||
* When true, component lifecycle callbacks (like onAddedToEntity) are deferred
|
||||
* until begin() is called to start running the scene.
|
||||
*/
|
||||
isEditorMode: boolean;
|
||||
|
||||
/**
|
||||
* 获取系统列表
|
||||
*/
|
||||
@@ -98,6 +110,15 @@ export interface IScene {
|
||||
*/
|
||||
unload(): void;
|
||||
|
||||
/**
|
||||
* 添加延迟的组件生命周期回调
|
||||
*
|
||||
* Queue a deferred component lifecycle callback.
|
||||
*
|
||||
* @param callback 要延迟执行的回调 | The callback to defer
|
||||
*/
|
||||
queueDeferredComponentCallback(callback: () => void): void;
|
||||
|
||||
/**
|
||||
* 开始场景
|
||||
*/
|
||||
|
||||
@@ -117,6 +117,30 @@ export class Scene implements IScene {
|
||||
*/
|
||||
private _didSceneBegin: boolean = false;
|
||||
|
||||
/**
|
||||
* 编辑器模式标志
|
||||
*
|
||||
* 当为 true 时,组件的生命周期回调(如 onAddedToEntity)会被延迟,
|
||||
* 直到调用 begin() 开始运行场景时才会触发。
|
||||
*
|
||||
* Editor mode flag.
|
||||
* When true, component lifecycle callbacks (like onAddedToEntity) are deferred
|
||||
* until begin() is called to start running the scene.
|
||||
*/
|
||||
public isEditorMode: boolean = false;
|
||||
|
||||
/**
|
||||
* 延迟的组件生命周期回调队列
|
||||
*
|
||||
* 在编辑器模式下,组件的 onAddedToEntity 回调会被加入此队列,
|
||||
* 等到 begin() 调用时统一执行。
|
||||
*
|
||||
* Deferred component lifecycle callback queue.
|
||||
* In editor mode, component's onAddedToEntity callbacks are queued here,
|
||||
* and will be executed when begin() is called.
|
||||
*/
|
||||
private _deferredComponentCallbacks: Array<() => void> = [];
|
||||
|
||||
/**
|
||||
* 系统列表缓存
|
||||
*/
|
||||
@@ -319,14 +343,47 @@ export class Scene implements IScene {
|
||||
*/
|
||||
public unload(): void {}
|
||||
|
||||
/**
|
||||
* 添加延迟的组件生命周期回调
|
||||
*
|
||||
* 在编辑器模式下,组件的 onAddedToEntity 回调会通过此方法加入队列。
|
||||
*
|
||||
* Queue a deferred component lifecycle callback.
|
||||
* In editor mode, component's onAddedToEntity callbacks are queued via this method.
|
||||
*
|
||||
* @param callback 要延迟执行的回调 | The callback to defer
|
||||
*/
|
||||
public queueDeferredComponentCallback(callback: () => void): void {
|
||||
this._deferredComponentCallbacks.push(callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始场景,启动实体处理器等
|
||||
*
|
||||
* 这个方法会启动场景。它将启动实体处理器等,并调用onStart方法。
|
||||
* 在编辑器模式下,此方法还会执行所有延迟的组件生命周期回调。
|
||||
*
|
||||
* This method starts the scene. It will start entity processors and call onStart.
|
||||
* In editor mode, this method also executes all deferred component lifecycle callbacks.
|
||||
*/
|
||||
public begin() {
|
||||
// 标记场景已开始运行并调用onStart方法
|
||||
// 标记场景已开始运行
|
||||
this._didSceneBegin = true;
|
||||
|
||||
// 执行所有延迟的组件生命周期回调 | Execute all deferred component lifecycle callbacks
|
||||
if (this._deferredComponentCallbacks.length > 0) {
|
||||
for (const callback of this._deferredComponentCallbacks) {
|
||||
try {
|
||||
callback();
|
||||
} catch (error) {
|
||||
this.logger.error('Error executing deferred component callback:', error);
|
||||
}
|
||||
}
|
||||
// 清空队列 | Clear the queue
|
||||
this._deferredComponentCallbacks = [];
|
||||
}
|
||||
|
||||
// 调用onStart方法
|
||||
this.onStart();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user