Skip to content

A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.

Units: free the old selection ring's mesh with the ring (Fixes #2043) (#2057)

Berie715d1787bd5
diff --git a/changelog.d/2043.md b/changelog.d/2043.md
new file mode 100644
index 000000000..1dff3c237
--- /dev/null
+++ b/changelog.d/2043.md
@@ -0,0 +1,4 @@
+### Fixed
+
+- Selecting a unit no longer keeps one more selection-ring mesh alive for the rest of the session (#2043): replacing
+ or clearing the ring frees its mesh, and the unit layer's teardown still frees the live one.
diff --git a/unity/Assets/Scripts/Map/Units/UnitLayer.cs b/unity/Assets/Scripts/Map/Units/UnitLayer.cs
index dd2609a47..97cc9ebc1 100644
--- a/unity/Assets/Scripts/Map/Units/UnitLayer.cs
+++ b/unity/Assets/Scripts/Map/Units/UnitLayer.cs
@@ -538,7 +538,15 @@ namespace Statecraft.Game.Map.Units
/// <summary>Selects a drawn unit by id (the ring goes round it), or clears the selection (null).</summary>
public void Select(string? id)
{
- if (ring != null) { ring.transform.SetParent(null); if (Application.isPlaying) Destroy(ring); else DestroyImmediate(ring); ring = null; }
+ if (ring != null)
+ {
+ // Each ring has its own mesh (UnitShowcase.Ring), owned by the layer's assets until teardown: free it with
+ // the ring, or every selection keeps one more alive for the session (#2043).
+ var mesh = ring.GetComponent<MeshFilter>().sharedMesh;
+ ring.transform.SetParent(null);
+ if (Application.isPlaying) { Destroy(ring); Destroy(mesh); } else { DestroyImmediate(ring); DestroyImmediate(mesh); }
+ ring = null;
+ }
var hit = placed.FirstOrDefault(p => p.Token.Unit.Id == id);
Selected = id != null && hit.Token != null ? id : null;
if (Selected == null) return;
diff --git a/unity/Assets/Tests/EditMode/UnitsInPlayTests.cs b/unity/Assets/Tests/EditMode/UnitsInPlayTests.cs
index 1ee3dee18..ab4b16e7f 100644
--- a/unity/Assets/Tests/EditMode/UnitsInPlayTests.cs
+++ b/unity/Assets/Tests/EditMode/UnitsInPlayTests.cs
@@ -584,6 +584,28 @@ public class UnitsInPlayTests
finally { if (VisualToggles.Names.Contains(UnitLayer.ToggleName)) VisualToggles.Set(UnitLayer.ToggleName, true); UnityEngine.Object.DestroyImmediate(map.gameObject); }
}
+ /// <summary>#2043: selecting, re-selecting and deselecting a unit many times leaves at most one live selection-ring
+ /// mesh (each replaced ring frees its own), and tearing the layer down frees that one too.</summary>
+ [Test]
+ public void SelectingAgainAndAgainKeepsOneRingMesh()
+ {
+ static int Rings() => Resources.FindObjectsOfTypeAll<Mesh>().Count(m => m.name == "UnitSelectionRing");
+ int before = Rings();
+ var map = BuildMap();
+ try
+ {
+ var layer = UnitLayer.Create(map, Units, C.Hexes, still: true);
+ layer.Show(C, Simulation.NewGame(C, GoldenRun.Seed, Mode.Historical));
+ string id = layer.Tokens[0].Token.Unit.Id;
+ for (int i = 0; i < 10; i++) { layer.Select(id); layer.Select(id); layer.Select(null); }
+ layer.Select(id);
+ Assert.AreEqual(id, layer.Selected);
+ Assert.AreEqual(1, Rings() - before, "one live ring mesh after 21 selections");
+ }
+ finally { UnityEngine.Object.DestroyImmediate(map.gameObject); }
+ Assert.AreEqual(0, Rings() - before, "the layer's teardown frees the live ring's mesh");
+ }
+
/// <summary>The same on the map's real hills: every rifleman of every army anchor of the war stands on its token's draped
/// base at its own point, and the ground under its boots rises at most a tenth of its height over its feet.</summary>
[Test]