* 设置当前场景(立即切换) * * 会自动处理旧场景的结束和新场景的初始化。 * 持久化实体会自动迁移到新场景。 * * Set current scene (immediate transition). * Automatically handles old scene cleanup and new scene initialization. * Persistent entities are automatically migrated to the new scene. * * @param s
(scene: T)
| 129 | * ``` |
| 130 | */ |
| 131 | public setScene<T extends IScene>(scene: T): T { |
| 132 | // 从当前场景提取持久化实体 |
| 133 | const currentScene = this.currentScene; |
| 134 | if (currentScene && currentScene instanceof Scene) { |
| 135 | this._pendingPersistentEntities = currentScene.extractPersistentEntities(); |
| 136 | if (this._pendingPersistentEntities.length > 0) { |
| 137 | this._logger.debug( |
| 138 | `Extracted ${this._pendingPersistentEntities.length} persistent entities for migration` |
| 139 | ); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // 移除旧场景 |
| 144 | this._defaultWorld.removeAllScenes(); |
| 145 | |
| 146 | // 注册全局 PerformanceMonitor 到 Scene 的 ServiceContainer |
| 147 | if (this._performanceMonitor) { |
| 148 | scene.services.registerInstance(PerformanceMonitor, this._performanceMonitor); |
| 149 | } |
| 150 | |
| 151 | // 通过 World 创建新场景 |
| 152 | this._defaultWorld.createScene(SceneManager.DEFAULT_SCENE_ID, scene); |
| 153 | this._defaultWorld.setSceneActive(SceneManager.DEFAULT_SCENE_ID, true); |
| 154 | |
| 155 | // 迁移持久化实体到新场景 |
| 156 | if (this._pendingPersistentEntities.length > 0 && scene instanceof Scene) { |
| 157 | scene.receiveMigratedEntities(this._pendingPersistentEntities); |
| 158 | this._logger.debug( |
| 159 | `Migrated ${this._pendingPersistentEntities.length} persistent entities to new scene` |
| 160 | ); |
| 161 | this._pendingPersistentEntities = []; |
| 162 | } |
| 163 | |
| 164 | // 重建ECS API |
| 165 | if (scene.querySystem && scene.eventSystem) { |
| 166 | this._ecsAPI = createECSAPI(scene, scene.querySystem, scene.eventSystem); |
| 167 | } else { |
| 168 | this._ecsAPI = null; |
| 169 | } |
| 170 | |
| 171 | // 触发场景切换回调 |
| 172 | Time.sceneChanged(); |
| 173 | |
| 174 | // 通知调试管理器(通过回调) |
| 175 | if (this._onSceneChangedCallback) { |
| 176 | this._onSceneChangedCallback(); |
| 177 | } |
| 178 | |
| 179 | this._logger.info(`Scene changed to: ${scene.name}`); |
| 180 | |
| 181 | return scene; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * 延迟加载场景(下一帧切换) |
no test coverage detected