Skip to content

Commit 2cdba82

Browse files
committed
Fix mesh memory leaks
1 parent 2678526 commit 2cdba82

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

Runtime/Code/Misc/TubeRendererCS.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ void Awake() {
3434
}
3535

3636
_mesh = new Mesh();
37+
_mesh.name = "Tube Mesh";
3738
_meshFilter.mesh = _mesh;
3839
}
3940

@@ -78,7 +79,6 @@ public void SetPositions(Vector3[] positions) {
7879

7980
private void GenerateMesh() {
8081
if (_mesh == null || _positions == null || _positions.Length <= 1) {
81-
_mesh = new Mesh();
8282
return;
8383
}
8484

@@ -117,6 +117,10 @@ private void GenerateMesh() {
117117
_meshFilter.mesh = _mesh;
118118
}
119119

120+
private void OnDestroy() {
121+
Destroy(_mesh);
122+
}
123+
120124
private Vector2[] GenerateUVs() {
121125
var uvs = new Vector2[_positions.Length * _sides];
122126

Runtime/Code/VoxelWorld/VoxelMeshProcessor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,8 @@ public static GameObject ProduceSingleBlock(int blockIndex, VoxelWorld world, fl
15441544
meshRenderer.sharedMaterial = world.voxelBlocks.atlasMaterial;
15451545

15461546
Mesh theMesh = new Mesh();
1547-
1547+
theMesh.name = "SingleBlock";
1548+
15481549
//Center around 0,0,0
15491550
Vector3 origin = new Vector3(-0.5f, -0.5f, -0.5f);
15501551
int flip = 0;

Runtime/Code/VoxelWorld/VoxelWorldChunk.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,13 @@ public void Clear() {
438438
detailGameObjects[i] = null;
439439
}
440440
}
441+
442+
if (detailMeshes != null) {
443+
foreach (var detailMesh in detailMeshes) {
444+
Object.Destroy(detailMesh);
445+
}
446+
}
447+
441448
obj = null;
442449
}
443450
}
@@ -550,8 +557,11 @@ private bool DoVisualUpdate(VoxelWorld world) {
550557
obj.transform.localPosition = Vector3.zero;
551558
obj.hideFlags = HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild;
552559
obj.name = "Chunk";
553-
554-
if (mesh == null) mesh = new Mesh();
560+
561+
if (mesh == null) {
562+
mesh = new Mesh();
563+
mesh.name = "VoxelWorldChunk";
564+
}
555565
// mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; //Big boys
556566

557567
filter = obj.AddComponent<MeshFilter>();
@@ -597,6 +607,7 @@ private bool DoVisualUpdate(VoxelWorld world) {
597607
detailRenderers[i].shadowCastingMode = ShadowCastingMode.Off;
598608

599609
detailMeshes[i] = new Mesh();
610+
detailMeshes[i].name = detailGameObjects[i].name;
600611
// detailMeshes[i].indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; //Big boys
601612

602613
detailFilters[i].mesh = detailMeshes[i];

0 commit comments

Comments
 (0)