/// <summary>When a Destructible Object is DESTROYED, this script will attempt to find and transfer the appropriate damaged materials over to the new prefab.</summary> public static void TransferMaterials(Destructible oldObj, GameObject newObj) { if (oldObj == null) return; if (!oldObj.IsInitialized) oldObj.Initialize(); MeshRenderer[] oldMeshes = oldObj.GetComponentsInChildren<MeshRenderer>(); MeshRenderer[] newMeshes = newObj.GetComponentsInChildren<MeshRenderer>(); // If either object has no meshes, then there's nothing to transfer, so exit. if (oldMeshes.Length == 0 || newMeshes.Length == 0) return; // Set reflection anchor overrides on new meshes if any exist on the old meshes, but only if it is the same type of material (ie, tags match) for (int i = 0; i < oldMeshes.Length; i++) { TagIt oldMeshTag = oldMeshes[i].GetComponent<TagIt>(); if (oldMeshTag == null) continue; if (oldMeshes[i].probeAnchor != null) { for (int j = 0; j < newMeshes.Length; j++) { TagIt newMeshTag = newMeshes[j].GetComponent<TagIt>(); if (newMeshTag != null && oldMeshTag.tags.Intersect(newMeshTag.tags).Any()) // make sure TagIt tags have at least one match. newMeshes[j].probeAnchor = oldMeshes[i].probeAnchor; } } } // Get new materials for each destroyed mesh for (int i=0; i<newMeshes.Length; i++) newMeshes[i].sharedMaterials = GetNewMaterialsForDestroyedMesh(newMeshes[i], oldObj); }
private void Update() { if (preSoftenDestructible && destructibleObj != null && impactInfo != null) { destructibleObj.ApplyImpactDamage(impactInfo); impactInfo = null; destructibleObj = null; preSoftenDestructible = false; } }
private static Material[] GetNewMaterialsForDestroyedMesh(MeshRenderer destMesh, Destructible destructibleObj) { if (destructibleObj == null) return null; Material[] curMats = destMesh.sharedMaterials; Material[] newMats = new Material[curMats.Length]; for (int i = 0; i < curMats.Length; i++) { Material currentMat = curMats[i]; if (currentMat == null) continue; if (destructibleObj.MaterialReplacements == null) continue; Material replacementMat = destructibleObj.MaterialReplacements[currentMat] as Material; if (replacementMat != null) newMats[i] = replacementMat.GetDestroyedMaterial(); else newMats[i] = currentMat.GetDestroyedMaterial(); } return newMats; }
private static Vector3 GetMeshCenterPoint(Destructible destructibleObj) { Bounds combinedBounds = new Bounds(); MeshFilter[] meshFilters = destructibleObj.GetComponentsInChildren<MeshFilter>(); foreach (MeshFilter meshFilter in meshFilters) combinedBounds.Encapsulate(meshFilter.mesh.bounds); return combinedBounds.center; }