bool CloneAndSaveMesh() { bool cloned = _meshFilter.CloneAndSaveMesh(SaveMeshToFbxFolder); SelectMesh(); return(cloned); }
public static bool Freeze(this Transform transform, Vector3 position, Quaternion rotation, Vector3 scale, bool saveToFBX) { MeshFilter filter = transform.GetComponent <MeshFilter>(); Mesh mesh = filter != null ? filter.sharedMesh : null; Collider collider = transform.GetComponent <Collider>(); PropertyInfo colliderCenterProperty = collider.GetType().GetProperty("center"); Vector3 initialColliderCenter = Vector3.zero; Vector3 initialPos = Vector3.zero; if (colliderCenterProperty != null) { initialColliderCenter = (Vector3)colliderCenterProperty.GetValue(collider, null); initialPos = transform.position; } Transform[] children = new Transform[transform.childCount]; // remove children so they don't move along for (int i = 0; i < children.Length; i++) { children[i] = transform.GetChild(i); } for (int i = 0; i < children.Length; i++) { children[i].parent = null; } Vector3[] worldVertices = null; Vector3[] worldNormals = null; if (mesh) { worldVertices = mesh.vertices.Select(v => transform.TransformPoint(v)).ToArray(); worldNormals = mesh.normals.Select(v => transform.TransformDirection(v)).ToArray(); string path = AssetDatabase.GetAssetPath(mesh); if (!path.EndsWith(".asset") && !string.IsNullOrEmpty(path)) { bool cloned = filter.CloneAndSaveMesh(saveToFBX); mesh = filter.sharedMesh; if (!cloned) { return(false); } } } transform.position = position; transform.rotation = rotation; transform.localScale = scale; if (mesh) { Vector3[] vertices = worldVertices.Select(v => transform.InverseTransformPoint(v)).ToArray(); Vector3[] normals = worldNormals.Select(v => transform.InverseTransformDirection(v)).ToArray(); mesh.vertices = vertices; mesh.normals = normals; mesh.RecalculateBounds(); } if (collider) { if (collider is MeshCollider) { MeshCollider m = collider as MeshCollider; m.sharedMesh = mesh; m.convex = !m.convex; m.convex = !m.convex; } if (colliderCenterProperty != null) { colliderCenterProperty.SetValue(collider, initialColliderCenter + (initialPos - transform.position), null); } } // re add children for (int i = 0; i < children.Length; i++) { children[i].parent = transform; } return(true); }