diff --git a/src/CounterDrone.Core/Simulation/SimulationEngine.cs b/src/CounterDrone.Core/Simulation/SimulationEngine.cs index a4c3c4c..e448098 100644 --- a/src/CounterDrone.Core/Simulation/SimulationEngine.cs +++ b/src/CounterDrone.Core/Simulation/SimulationEngine.cs @@ -449,20 +449,30 @@ namespace CounterDrone.Core.Simulation for (int i = 0; i < _drones.Count; i++) { var d = _drones[i]; - _snapshotPool.Add(new EntitySnapshot { EntityId = d.Id, EntityType = Models.EntityType.Drone, PosX = d.PosX, PosY = d.PosY, PosZ = d.PosZ, StateData = "{\"damageStage\":" + (int)_damageModel.GetDamageStage(1 - d.Hp) + ",\"hp\":" + d.Hp.ToString("F2") + "}" }); + int stage = (int)_damageModel.GetDamageStage(1 - d.Hp); + _snapshotPool.Add(new EntitySnapshot { EntityId = d.Id, EntityType = Models.EntityType.Drone, PosX = d.PosX, PosY = d.PosY, PosZ = d.PosZ, StateData = Str($"{{\"damageStage\":{stage},\"hp\":{d.Hp:F2}}}") }); } for (int i = 0; i < _platforms.Count; i++) { var p = _platforms[i]; if (p.PlatformType != Models.PlatformType.AirBased) continue; - _snapshotPool.Add(new EntitySnapshot { EntityId = p.Id, EntityType = Models.EntityType.Platform, PosX = p.PosX, PosY = p.PosY, PosZ = p.PosZ, StateData = "{\"state\":\"" + p.State + "\"}" }); + _snapshotPool.Add(new EntitySnapshot { EntityId = p.Id, EntityType = Models.EntityType.Platform, PosX = p.PosX, PosY = p.PosY, PosZ = p.PosZ, StateData = Str($"{{\"state\":\"{p.State}\"}}") }); } for (int i = 0; i < _clouds.Count; i++) { var c = _clouds[i]; - _snapshotPool.Add(new EntitySnapshot { EntityId = c.Id, EntityType = Models.EntityType.Cloud, PosX = c.Dispersion.Center.X, PosY = c.Dispersion.Center.Y, PosZ = c.Dispersion.Center.Z, StateData = "{\"radius\":" + c.Dispersion.Radius.ToString("F1") + ",\"opacity\":" + c.Dispersion.Particles.Opacity.ToString("F2") + ",\"phase\":" + c.Dispersion.Phase + ",\"elapsed\":" + c.Dispersion.Elapsed.ToString("F1") + "}" }); + _snapshotPool.Add(new EntitySnapshot { EntityId = c.Id, EntityType = Models.EntityType.Cloud, PosX = c.Dispersion.Center.X, PosY = c.Dispersion.Center.Y, PosZ = c.Dispersion.Center.Z, StateData = Str($"{{\"radius\":{c.Dispersion.Radius:F1},\"opacity\":{c.Dispersion.Particles.Opacity:F2},\"phase\":{c.Dispersion.Phase},\"elapsed\":{c.Dispersion.Elapsed:F1}}}") }); } return _snapshotPool; } + + /// GC 友好字符串构造:复用 StringBuilder,避免拼接中间字符串 + private static string Str(FormattableString fs) + { + _sb.Clear(); + _sb.AppendFormat(fs.Format, fs.GetArguments()); + return _sb.ToString(); + } + private static readonly System.Text.StringBuilder _sb = new(256); } } diff --git a/src/Unity/Assets/Plugins/CounterDrone.Core/CounterDrone.Core.dll b/src/Unity/Assets/Plugins/CounterDrone.Core/CounterDrone.Core.dll index f05fc78..b64fa72 100644 Binary files a/src/Unity/Assets/Plugins/CounterDrone.Core/CounterDrone.Core.dll and b/src/Unity/Assets/Plugins/CounterDrone.Core/CounterDrone.Core.dll differ diff --git a/src/Unity/Assets/Plugins/CounterDrone.Core/CounterDrone.Core.pdb b/src/Unity/Assets/Plugins/CounterDrone.Core/CounterDrone.Core.pdb index b9d8a70..a283e53 100644 Binary files a/src/Unity/Assets/Plugins/CounterDrone.Core/CounterDrone.Core.pdb and b/src/Unity/Assets/Plugins/CounterDrone.Core/CounterDrone.Core.pdb differ