mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
fix(ecs): 修复模块重复初始化和系统更新问题
- 添加 _isInitialized 标志防止 ArchEcsModule 重复初始化 - 在 Initialize 方法中检查是否已初始化避免重复执行 - 设置初始化标志在所有系统初始化完成后 - 修改 DestroyAsync 方法确保仅在已初始化时执行销毁 - 重置 _isInitialized 标志为 false 在销毁时 - 修正 ArchSystemAdapter 注释描述 OnUpdate 为虚方法而非抽象方法 - 简化 EcsAdvancedTests 中的断言语法移除不必要的大括号
This commit is contained in:
parent
43dc5a4d11
commit
155ca3024b
@ -126,7 +126,7 @@ public class EcsAdvancedTests
|
|||||||
|
|
||||||
_world = _container!.Get<World>();
|
_world = _container!.Get<World>();
|
||||||
|
|
||||||
Assert.DoesNotThrow(() => { _ecsModule.Update(1.0f); });
|
Assert.DoesNotThrow(() => _ecsModule.Update(1.0f));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
|||||||
@ -11,6 +11,7 @@ public sealed class ArchEcsModule : IServiceModule
|
|||||||
{
|
{
|
||||||
private readonly List<ArchSystemAdapter<float>> _systems = [];
|
private readonly List<ArchSystemAdapter<float>> _systems = [];
|
||||||
private IIocContainer? _container;
|
private IIocContainer? _container;
|
||||||
|
private bool _isInitialized;
|
||||||
private World? _world;
|
private World? _world;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -58,6 +59,12 @@ public sealed class ArchEcsModule : IServiceModule
|
|||||||
{
|
{
|
||||||
if (!IsEnabled || _world == null || _container == null) return;
|
if (!IsEnabled || _world == null || _container == null) return;
|
||||||
|
|
||||||
|
// 防止重复初始化
|
||||||
|
if (_isInitialized)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// 从容器获取所有适配器
|
// 从容器获取所有适配器
|
||||||
var adapters = _container.GetAll<ArchSystemAdapter<float>>();
|
var adapters = _container.GetAll<ArchSystemAdapter<float>>();
|
||||||
if (adapters.Count > 0)
|
if (adapters.Count > 0)
|
||||||
@ -70,6 +77,8 @@ public sealed class ArchEcsModule : IServiceModule
|
|||||||
system.Initialize();
|
system.Initialize();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_isInitialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -77,7 +86,7 @@ public sealed class ArchEcsModule : IServiceModule
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public async ValueTask DestroyAsync()
|
public async ValueTask DestroyAsync()
|
||||||
{
|
{
|
||||||
if (!IsEnabled) return;
|
if (!IsEnabled || !_isInitialized) return;
|
||||||
|
|
||||||
// 销毁所有系统
|
// 销毁所有系统
|
||||||
foreach (var system in _systems)
|
foreach (var system in _systems)
|
||||||
@ -94,6 +103,8 @@ public sealed class ArchEcsModule : IServiceModule
|
|||||||
_world = null;
|
_world = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_isInitialized = false;
|
||||||
|
|
||||||
await ValueTask.CompletedTask;
|
await ValueTask.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -39,7 +39,7 @@ public abstract class ArchSystemAdapter<T> : AbstractSystem, ArchSys.ISystem<T>
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 显式实现 Arch.System.ISystem<T> 的主更新方法
|
/// 显式实现 Arch.System.ISystem<T> 的主更新方法
|
||||||
/// 调用受保护的抽象方法 OnUpdate 以强制子类实现核心更新逻辑
|
/// 调用受保护的虚方法 OnUpdate 以强制子类实现核心更新逻辑
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="t">系统数据参数(通常是 deltaTime)</param>
|
/// <param name="t">系统数据参数(通常是 deltaTime)</param>
|
||||||
public void Update(in T t)
|
public void Update(in T t)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user