/* * Takes a filename and deserializes the associated mesh * Returns the default cube mesh if there's no mesh saved to disk */ public static Mesh deserializeMesh(string name) { Mesh mesh; string path = Path.Combine(Application.persistentDataPath, name + ".mesh"); if (File.Exists(path)) { FileStream stream = new FileStream(path, FileMode.Open); serializableMesh smesh = (serializableMesh)formatter.Deserialize(stream); stream.Close(); mesh = new Mesh(); mesh.vertices = smesh.vertices.Select(x => serializableVector3ToVector3(x)).ToArray(); mesh.triangles = smesh.triangles; mesh.colors = smesh.colors.Select(x => serializableVector4ToColor(x)).ToArray(); } else { //Load a placeholder cube so that we can at least fail gracefully GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); MeshFilter mf = (MeshFilter)cube.GetComponent("MeshFilter"); mesh = mf.mesh; } return(mesh); }
//Take a mesh and a filename and save it to disk public static void serializeMesh(Mesh mesh, string name) { serializableMesh smesh = new serializableMesh(); smesh.vertices = mesh.vertices.Select(x => vector3ToSerializableVector3(x)).ToArray(); smesh.triangles = mesh.triangles; smesh.colors = mesh.colors.Select(x => vector4ToSerializableVector4(x)).ToArray(); string path = Path.Combine(Application.persistentDataPath, name + ".mesh"); FileStream stream = new FileStream(path, FileMode.Create); formatter.Serialize(stream, smesh); stream.Close(); }