private void UpdatePhysicsMaterial(PhysicMaterial oldMaterial, PhysicMaterial newMaterial) { // If the physics material has changed if (newMaterial != oldMaterial) { CSGModel csgModel = (CSGModel)target; // Update the built mesh renderers that use the old material Transform meshGroup = csgModel.GetMeshGroupTransform(); if (meshGroup != null) { MeshCollider[] meshColliders = meshGroup.GetComponentsInChildren <MeshCollider>(); for (int i = 0; i < meshColliders.Length; i++) { if (meshColliders[i].sharedMaterial == oldMaterial) { meshColliders[i].sharedMaterial = newMaterial; } } } // Update the last build's default material because we don't need to build again lastBuildDefaultPhysicsMaterialProperty.objectReferenceValue = newMaterial; } }
private void UpdateVisualMaterial(Material oldMaterial, Material newMaterial) { // If the material has changed and the new material is not null if (newMaterial != oldMaterial && newMaterial != null) { CSGModel csgModel = (CSGModel)target; // Update the built mesh renderers that use the old material Transform meshGroup = csgModel.GetMeshGroupTransform(); if (meshGroup != null) { MeshRenderer[] meshRenderers = meshGroup.GetComponentsInChildren <MeshRenderer>(); for (int i = 0; i < meshRenderers.Length; i++) { if (meshRenderers[i].sharedMaterial == oldMaterial) { meshRenderers[i].sharedMaterial = newMaterial; } } } // Update all the polygons in the source brushes List <Brush> brushes = csgModel.GetBrushes(); for (int i = 0; i < brushes.Count; i++) { if (brushes[i] != null) // Make sure the tracked brush still exists { Polygon[] polygons = brushes[i].GetPolygons(); for (int j = 0; j < polygons.Length; j++) { // If the polygon uses the old material (as an override) if (polygons[j].Material == oldMaterial) { // Set the polygon to null, this removes the material override which gives consistent behaviour polygons[j].Material = null; } } } } } }