From 50e5284694bf8b047c395c13e684f9a97212f169 Mon Sep 17 00:00:00 2001 From: NorthropBtwo Date: Fri, 15 Feb 2019 19:24:27 +0100 Subject: [PATCH 1/2] added MeshCollider to RoadMeshCreator.cs --- .../Examples/Scripts/RoadMeshCreator.cs | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/PathCreator/Examples/Scripts/RoadMeshCreator.cs b/PathCreator/Examples/Scripts/RoadMeshCreator.cs index e733759..5e94bc7 100644 --- a/PathCreator/Examples/Scripts/RoadMeshCreator.cs +++ b/PathCreator/Examples/Scripts/RoadMeshCreator.cs @@ -11,6 +11,7 @@ public class RoadMeshCreator : PathSceneTool [Range(0, .5f)] public float thickness = .15f; public bool flattenSurface; + public bool meshCollider; [Header("Material settings")] public Material roadMaterial; @@ -20,16 +21,19 @@ public class RoadMeshCreator : PathSceneTool MeshFilter meshFilter; MeshRenderer meshRenderer; + string meshHolderName = "Mesh Holder"; + protected override void PathUpdated() { if (pathCreator != null) { AssignMeshComponents(); AssignMaterials(); - meshFilter.mesh = CreateRoadMesh();; + meshFilter.mesh = CreateRoadMesh(); + UpdateMeshCollider(meshCollider); } } - + Mesh CreateRoadMesh() { @@ -132,7 +136,6 @@ Mesh CreateRoadMesh() void AssignMeshComponents() { // Find/creator mesh holder object in children - string meshHolderName = "Mesh Holder"; Transform meshHolder = transform.Find(meshHolderName); if (meshHolder == null) { meshHolder = new GameObject(meshHolderName).transform; @@ -166,5 +169,36 @@ void AssignMaterials() } } + //update meshcolider if enabled + void UpdateMeshCollider(bool meshColliderEnable) + { + GameObject meshHolder = transform.Find(meshHolderName).gameObject; + if (meshHolder != null) + { + MeshCollider meshCol = meshHolder.GetComponent(); + if (meshCol != null) + { + bool convex = meshCol.convex; + bool isTrigger = meshCol.isTrigger; + MeshColliderCookingOptions cookingOptions = meshCol.cookingOptions; + PhysicMaterial material = meshCol.material; + DestroyImmediate(meshCol); + if (meshColliderEnable) + { + meshCol = meshHolder.AddComponent(); + meshCol.convex = convex; + meshCol.isTrigger = isTrigger; + meshCol.cookingOptions = cookingOptions; + meshCol.material = material; + } + } + else if (meshColliderEnable) + { + meshHolder.AddComponent(); + } + } + } + + } } \ No newline at end of file From cb2013d1c1142670928152aea909299f91f4dc0a Mon Sep 17 00:00:00 2001 From: NorthropBtwo Date: Sat, 16 Feb 2019 21:13:17 +0100 Subject: [PATCH 2/2] Update RoadMeshCreator.cs according to feedback see feedback from https://github.com/SebLague/Path-Creator/pull/19 --- .../Examples/Scripts/RoadMeshCreator.cs | 23 ++++++++----------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/PathCreator/Examples/Scripts/RoadMeshCreator.cs b/PathCreator/Examples/Scripts/RoadMeshCreator.cs index 5e94bc7..6717c6b 100644 --- a/PathCreator/Examples/Scripts/RoadMeshCreator.cs +++ b/PathCreator/Examples/Scripts/RoadMeshCreator.cs @@ -21,7 +21,7 @@ public class RoadMeshCreator : PathSceneTool MeshFilter meshFilter; MeshRenderer meshRenderer; - string meshHolderName = "Mesh Holder"; + Transform meshHolder; protected override void PathUpdated() { @@ -136,7 +136,8 @@ Mesh CreateRoadMesh() void AssignMeshComponents() { // Find/creator mesh holder object in children - Transform meshHolder = transform.Find(meshHolderName); + string meshHolderName = "Mesh Holder"; + meshHolder = transform.Find(meshHolderName); if (meshHolder == null) { meshHolder = new GameObject(meshHolderName).transform; meshHolder.transform.parent = transform; @@ -172,29 +173,23 @@ void AssignMaterials() //update meshcolider if enabled void UpdateMeshCollider(bool meshColliderEnable) { - GameObject meshHolder = transform.Find(meshHolderName).gameObject; if (meshHolder != null) { MeshCollider meshCol = meshHolder.GetComponent(); if (meshCol != null) { - bool convex = meshCol.convex; - bool isTrigger = meshCol.isTrigger; - MeshColliderCookingOptions cookingOptions = meshCol.cookingOptions; - PhysicMaterial material = meshCol.material; - DestroyImmediate(meshCol); if (meshColliderEnable) { - meshCol = meshHolder.AddComponent(); - meshCol.convex = convex; - meshCol.isTrigger = isTrigger; - meshCol.cookingOptions = cookingOptions; - meshCol.material = material; + meshCol.sharedMesh = meshHolder.GetComponent().sharedMesh; } + else + { + DestroyImmediate(meshCol); + } } else if (meshColliderEnable) { - meshHolder.AddComponent(); + meshHolder.gameObject.AddComponent(); } } }