运行 Arch ECS 系统
直接装配
cs
// Direct construction borrows the World/system; their using declarations are the sole final owners.
using var world = World.Create();
using var movement = new NativeMovement(world);
var runtime = new ArchRuntime(world, [movement], new ArchOptions { EnableStatistics = true });
try
{
runtime.Initialize();
var entity = world.Create(new Position(0, 0), new Velocity(10, 5));
runtime.Update(0.5f);
var position = world.Get<Position>(entity);
if (position.X != 5 || position.Y != 2.5f || movement.BeforeCalls != 1 || movement.AfterCalls != 1)
throw new InvalidOperationException("Native phases or entity movement failed.");
var statistics = runtime.GetStatistics()!;
Console.WriteLine(string.Create(CultureInfo.InvariantCulture,
$"ARCH_RUNTIME_PASS entities={statistics.EntityCount} frames={statistics.CompletedFrames} systems={statistics.SystemCount}"));
}
finally
{
runtime.Stop();
}bash
dotnet run --project Samples/ArchRuntimeDemo -c Release看到 ARCH_RUNTIME_PASS 表示原生系统已执行且实体移动结果正确。运行时借用传入的 World 和系统。调用方先停止运行时,再释放系统,最后释放 World。
使用 DI
在组合根调用 AddArch 配置运行时及选项,并登记系统。在宿主循环按实际时间步调用 Update。开启 EnableStatistics 后可读取实例指标;启用前按自己的场景测量花费。