fix: 修复项目切换时运行时和系统重复初始化问题

This commit is contained in:
yhh
2025-12-04 10:10:31 +08:00
parent 566e1977fd
commit c744d8d9fc
4 changed files with 29 additions and 0 deletions

View File

@@ -631,6 +631,13 @@ function App() {
await pluginLoader.unloadProjectPlugins(pluginManager);
}
// 清理场景(会清理所有实体和系统)
// Clear scene (clears all entities and systems)
const scene = Core.scene;
if (scene) {
scene.end();
}
// 清理模块系统
const engineService = EngineService.getInstance();
engineService.clearModuleSystems();

View File

@@ -14,6 +14,15 @@ import { EditorEngineSync } from '../services/EditorEngineSync';
let engineInitialized = false;
let engineInitializing = false;
/**
* 重置引擎初始化状态(在项目关闭时调用)
* Reset engine initialization state (called when project is closed)
*/
export function resetEngineState(): void {
engineInitialized = false;
engineInitializing = false;
}
export interface EngineState {
initialized: boolean;
running: boolean;

View File

@@ -28,6 +28,7 @@ import {
type GameRuntimeConfig
} from '@esengine/runtime-core';
import { getMaterialManager } from '@esengine/material-system';
import { resetEngineState } from '../hooks/useEngine';
import { convertFileSrc } from '@tauri-apps/api/core';
import { IdGenerator } from '../utils/idGenerator';
import { TauriAssetReader } from './TauriAssetReader';
@@ -245,7 +246,14 @@ export class EngineService {
ctx.uiInputSystem.unbind?.();
}
// 清理 viewport | Clear viewport
this.unregisterViewport('editor-viewport');
// 重置 useEngine 的模块级状态 | Reset useEngine module-level state
resetEngineState();
this._modulesInitialized = false;
this._initialized = false;
}
/**

View File

@@ -1159,6 +1159,11 @@ export class PluginManager implements IService {
}
}
}
// 重置初始化状态,允许下次重新初始化运行时
// Reset initialized flag to allow re-initialization
this.initialized = false;
logger.debug('Scene systems cleared, runtime can be re-initialized');
}
/**