How to save a mesh in runtime on android

I’ve been playing around with google’s project tango and want to save the it has mesh generated. I’ve seen a lot of people saving meshes from the editor but I want to be able to save it locally on the android tablet so I could look at it later.

using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
using System.Linq;

public class Exporter : MonoBehaviour {

	private static int StartIndex = 0;
	
	public static void Start()
	{
		StartIndex = 0;
	}
	public static void End()
	{
		StartIndex = 0;
	}
	
	
	public static string MeshToString(MeshFilter mf, Transform t) 
	{	
		Vector3 s = t.localScale;
		Vector3 p = t.localPosition;
		Quaternion r = t.localRotation;
		
		
		int numVertices = 0;
		Mesh m = mf.sharedMesh;
		if (!m) {
			return "####Error####";
		}
		Material[] mats = mf.GetComponent<Renderer> ().sharedMaterials;
		
		StringBuilder sb = new StringBuilder ();
		
		Vector3[] normals = m.normals; 
		for (int i=0; i<normals.Length; i++) // remove this if your exported mesh have faces on wrong side
			normals  _= -normals *;*_

* m.normals = normals;*

* m.triangles = m.triangles.Reverse ().ToArray (); //*

* foreach (Vector3 vv in m.vertices) {*
* Vector3 v = t.TransformPoint (vv);*
* numVertices++;*
* sb.Append (string.Format ("v {0} {1} {2}
", v.x, v.y, -v.z));*

* }*
* sb.Append ("
");*

* foreach (Vector3 nn in m.normals) {*
_ Vector3 v = r * nn;_
* sb.Append (string.Format ("vn {0} {1} {2}
", -v.x, -v.y, v.z));*

* }*
* sb.Append ("
");*

* foreach (Vector3 v in m.uv) {*
* sb.Append (string.Format ("vt {0} {1}
", v.x, v.y));*

* }*
* for (int material=0; material < m.subMeshCount; material ++) {*
* sb.Append ("
");*

* sb.Append ("usemtl “).Append (mats [material].name).Append (”
");*

* sb.Append ("usemap “).Append (mats [material].name).Append (”
");*

* int[] triangles = m.GetTriangles (material);*
* for (int i=0; i<triangles.Length; i+=3) {*
* sb.Append (string.Format ("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}
",*

_ triangles + 1 + StartIndex, triangles [i + 1] + 1 + StartIndex, triangles [i + 2] + 1 + StartIndex));
* }
}*_

* for (int i=0; i<normals.Length; i++) // remove this if your exported mesh have faces on wrong side*
normals = -normals ;
* m.normals = normals;*

* m.triangles = m.triangles.Reverse ().ToArray (); //*

* StartIndex += numVertices;*
* return sb.ToString ();*
* }*

* public void DoExport(bool makeSubmeshes)*
* {*

* string meshName = gameObject.name;*
* string fileName = Application.persistentDataPath+“/”+gameObject.name+“.obj”; // you can also use: “/storage/sdcard1/” +gameObject.name+“.obj”*

* Start();*

* StringBuilder meshString = new StringBuilder();*

* meshString.Append(“#” + meshName + “.obj”*
* + "
#" + System.DateTime.Now.ToLongDateString()*

* + "
#" + System.DateTime.Now.ToLongTimeString()*

* + "
#-------"*

_* + "

");*_

* Transform t = transform;*

* Vector3 originalPosition = t.position;*
* t.position = Vector3.zero;*

* if (!makeSubmeshes)*
* {*
* meshString.Append("g “).Append(t.name).Append(”
");*

* }*
* meshString.Append(processTransform(t, makeSubmeshes));*

* WriteToFile(meshString.ToString(),fileName);*

* t.position = originalPosition;*

* End();*
* Debug.Log("Exported Mesh: " + fileName);*
* }*

* static string processTransform(Transform t, bool makeSubmeshes)*
* {*
* StringBuilder meshString = new StringBuilder();*

* meshString.Append(“#” + t.name*
* + "
#-------"*

* + "
");*

* if (makeSubmeshes)*
* {*
* meshString.Append("g “).Append(t.name).Append(”
");*

* }*

* MeshFilter mf = t.GetComponent();*
* if (mf)*
* {*
* meshString.Append(MeshToString(mf, t));*
* }*

* for(int i = 0; i < t.childCount; i++)*
* {*
* meshString.Append(processTransform(t.GetChild(i), makeSubmeshes));*
* }*

* return meshString.ToString();*
* }*

* static void WriteToFile(string s, string filename)*
* {*
* using (StreamWriter sw = new StreamWriter(filename))*
* {*
* sw.Write(s);*
* }*
* }*
}

This is my mesh exporter for android, it is little modified script from this page:
_*http://wiki.unity3d.com/index.php/ExportOBJ*_

There is much simpler solution for serializing Mesh. Please check out Runtime Serialization for Unity. Its not just another serialization plugin which works only on custom c# objects. But what makes it special is its capablity to serialize Unity Objects like GameObject, MonoBehaviours, Textures, Prefabs etc. As a matter of fact, you can even use it for Scene Serialization. For more info about supported list, please check this 2.