From 8312f37e7eaa38e5dd2e020a4df4639709be537c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A1lint=20Hodossy?= Date: Tue, 6 Dec 2022 16:11:03 +0000 Subject: [PATCH] Reuse existing mesh assets in MjMeshFilter --- unity/Runtime/Components/Shapes/MjMeshFilter.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/unity/Runtime/Components/Shapes/MjMeshFilter.cs b/unity/Runtime/Components/Shapes/MjMeshFilter.cs index c91f7a87..83f4963d 100644 --- a/unity/Runtime/Components/Shapes/MjMeshFilter.cs +++ b/unity/Runtime/Components/Shapes/MjMeshFilter.cs @@ -48,6 +48,12 @@ public class MjMeshFilter : MonoBehaviour { throw new ArgumentException("Unsupported geom shape detected"); } + if(_geom.ShapeType == MjShapeComponent.ShapeTypes.Mesh) { + MjMeshShape meshShape = _geom.Shape as MjMeshShape; + _meshFilter.sharedMesh = meshShape.Mesh; + return; + } + DisposeCurrentMesh(); var mesh = new Mesh(); @@ -73,7 +79,7 @@ public class MjMeshFilter : MonoBehaviour { // Dynamically created meshes with no references are only disposed automatically on scene changes. // This prevents resource leaks in case the host environment doesn't reload scenes. private void DisposeCurrentMesh() { - if (_meshFilter.sharedMesh != null) { + if (_meshFilter.sharedMesh != null && _geom.ShapeType != MjShapeComponent.ShapeTypes.Mesh) { #if UNITY_EDITOR DestroyImmediate(_meshFilter.sharedMesh); #else