From cb7e086dcd8c28486c97c0fc3724ae21bd775d0d Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 10 Mar 2025 22:28:54 +0800 Subject: [PATCH 1/2] add EcsWorld.DestroyAndClearAllWorlds --- src/EcsWorld.static.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/EcsWorld.static.cs b/src/EcsWorld.static.cs index d364a2f..02002fe 100644 --- a/src/EcsWorld.static.cs +++ b/src/EcsWorld.static.cs @@ -66,6 +66,21 @@ namespace DCFApixels.DragonECS return ref WorldComponentPool.GetForWorldUnchecked(worldID); } + public static void DestroyAndClearAllWorlds() + { + for (int i = 1; i < _worlds.Length; i++) + { + var world = _worlds[i]; + if (world == null) { continue; } + + if(world.IsDestroyed == false) + { + world.Destroy(); + } + world = null; + } + } + #region WorldComponentPool public ReadOnlySpan GetWorldComponents() { @@ -251,11 +266,14 @@ namespace DCFApixels.DragonECS } #endregion + #region NullWorld private sealed class NullWorld : EcsWorld { internal NullWorld() : base(new EcsWorldConfig(4, 4, 4, 4, 4), null, 0) { } } + #endregion + #region DebuggerProxy protected partial class DebuggerProxy { From 5ba5b7094d266d72dc9342f1573222279c9436c7 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 10 Mar 2025 22:36:12 +0800 Subject: [PATCH 2/2] rename DestroyAndClearAllWorlds to ResetStaticState --- src/EcsWorld.static.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/EcsWorld.static.cs b/src/EcsWorld.static.cs index 02002fe..2798fd8 100644 --- a/src/EcsWorld.static.cs +++ b/src/EcsWorld.static.cs @@ -21,7 +21,7 @@ namespace DCFApixels.DragonECS #endregion private static EcsWorld[] _worlds = Array.Empty(); - private static IdDispenser _worldIdDispenser = new IdDispenser(4, 0, n => Array.Resize(ref _worlds, n)); + private static readonly IdDispenser _worldIdDispenser = new IdDispenser(4, 0, n => Array.Resize(ref _worlds, n)); private static StructList _allWorldComponentPools = new StructList(64); private StructList _worldComponentPools; @@ -66,7 +66,7 @@ namespace DCFApixels.DragonECS return ref WorldComponentPool.GetForWorldUnchecked(worldID); } - public static void DestroyAndClearAllWorlds() + public static void ResetStaticState() { for (int i = 1; i < _worlds.Length; i++) { @@ -79,6 +79,7 @@ namespace DCFApixels.DragonECS } world = null; } + _worldIdDispenser.ReleaseAll(); } #region WorldComponentPool