Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        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;
        }