From 155ca3024b5ff0d59e9b4af69aa520807a152a25 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Mon, 2 Mar 2026 21:13:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(ecs):=20=E4=BF=AE=E5=A4=8D=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E9=87=8D=E5=A4=8D=E5=88=9D=E5=A7=8B=E5=8C=96=E5=92=8C?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E6=9B=B4=E6=96=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 _isInitialized 标志防止 ArchEcsModule 重复初始化 - 在 Initialize 方法中检查是否已初始化避免重复执行 - 设置初始化标志在所有系统初始化完成后 - 修改 DestroyAsync 方法确保仅在已初始化时执行销毁 - 重置 _isInitialized 标志为 false 在销毁时 - 修正 ArchSystemAdapter 注释描述 OnUpdate 为虚方法而非抽象方法 - 简化 EcsAdvancedTests 中的断言语法移除不必要的大括号 --- GFramework.Core.Tests/ecs/EcsAdvancedTests.cs | 2 +- GFramework.Core/ecs/ArchEcsModule.cs | 13 ++++++++++++- GFramework.Core/ecs/ArchSystemAdapter.cs | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/GFramework.Core.Tests/ecs/EcsAdvancedTests.cs b/GFramework.Core.Tests/ecs/EcsAdvancedTests.cs index 298e0e6..3831138 100644 --- a/GFramework.Core.Tests/ecs/EcsAdvancedTests.cs +++ b/GFramework.Core.Tests/ecs/EcsAdvancedTests.cs @@ -126,7 +126,7 @@ public class EcsAdvancedTests _world = _container!.Get(); - Assert.DoesNotThrow(() => { _ecsModule.Update(1.0f); }); + Assert.DoesNotThrow(() => _ecsModule.Update(1.0f)); } [Test] diff --git a/GFramework.Core/ecs/ArchEcsModule.cs b/GFramework.Core/ecs/ArchEcsModule.cs index 549b234..df25f37 100644 --- a/GFramework.Core/ecs/ArchEcsModule.cs +++ b/GFramework.Core/ecs/ArchEcsModule.cs @@ -11,6 +11,7 @@ public sealed class ArchEcsModule : IServiceModule { private readonly List> _systems = []; private IIocContainer? _container; + private bool _isInitialized; private World? _world; /// @@ -58,6 +59,12 @@ public sealed class ArchEcsModule : IServiceModule { if (!IsEnabled || _world == null || _container == null) return; + // 防止重复初始化 + if (_isInitialized) + { + return; + } + // 从容器获取所有适配器 var adapters = _container.GetAll>(); if (adapters.Count > 0) @@ -70,6 +77,8 @@ public sealed class ArchEcsModule : IServiceModule system.Initialize(); } } + + _isInitialized = true; } /// @@ -77,7 +86,7 @@ public sealed class ArchEcsModule : IServiceModule /// public async ValueTask DestroyAsync() { - if (!IsEnabled) return; + if (!IsEnabled || !_isInitialized) return; // 销毁所有系统 foreach (var system in _systems) @@ -94,6 +103,8 @@ public sealed class ArchEcsModule : IServiceModule _world = null; } + _isInitialized = false; + await ValueTask.CompletedTask; } diff --git a/GFramework.Core/ecs/ArchSystemAdapter.cs b/GFramework.Core/ecs/ArchSystemAdapter.cs index 47b9346..8d567fd 100644 --- a/GFramework.Core/ecs/ArchSystemAdapter.cs +++ b/GFramework.Core/ecs/ArchSystemAdapter.cs @@ -39,7 +39,7 @@ public abstract class ArchSystemAdapter : AbstractSystem, ArchSys.ISystem /// /// 显式实现 Arch.System.ISystem<T> 的主更新方法 - /// 调用受保护的抽象方法 OnUpdate 以强制子类实现核心更新逻辑 + /// 调用受保护的虚方法 OnUpdate 以强制子类实现核心更新逻辑 /// /// 系统数据参数(通常是 deltaTime) public void Update(in T t)