public static bool Export(string folder, string filename, Mesh mesh, ExportMaterial[] textures, bool copyTextures) { exportMesh = mesh; exportTextures = textures; targetFolder = folder; targetName = filename; _copyTextures = copyTextures; if(folder.Contains(" ")) { EditorUtility.DisplayDialog("Filename Error","The filename can't contain spaces","I'm sorry"); return false; } if(filename.Contains(" ")) { EditorUtility.DisplayDialog("Filename Error","The filename can't contain spaces","I'm sorry"); return false; } /*if (!CreateTargetFolder()) { Debug.LogError("There was a problem with the destination folder"); return false; }*/ MeshToFile(targetFolder, targetName); exportMesh = null; exportTextures = null; return true; }
private static string MeshToString(Dictionary <string, ExportMaterial> materialList) { StringBuilder sb = new StringBuilder(); sb.AppendLine("# Unity Asset Buildr OBJ Exporter\n http://buildr.jasperstocker.com"); sb.Append("g ").Append(targetName).Append("\n"); foreach (Vector3 lv in exportMesh.vertices) { Vector3 wv = lv; //This is sort of ugly - inverting x-component since we're in //a different coordinate system than "everyone" is "used to". sb.Append(string.Format("v {0} {1} {2}\n", -wv.x, wv.y, wv.z)); } sb.Append("\n"); foreach (Vector3 lv in exportMesh.normals) { Vector3 wv = lv; sb.Append(string.Format("vn {0} {1} {2}\n", -wv.x, wv.y, wv.z)); } sb.Append("\n"); foreach (Vector3 v in exportMesh.uv) { sb.Append(string.Format("vt {0} {1}\n", v.x, v.y)); } for (int t = 0; t < exportMesh.subMeshCount; t++) { sb.Append("\n"); if (t < exportTextures.Length - 1) { sb.Append("g ").Append(exportTextures[t].name).Append("\n"); sb.Append("usemtl ").Append(exportTextures[t].name).Append("\n"); sb.Append("usemap ").Append(exportTextures[t].name).Append("\n"); } else { sb.Append("g ").Append(exportTextures[exportTextures.Length - 1].name).Append("\n"); sb.Append("usemtl ").Append(exportTextures[exportTextures.Length - 1].name).Append("\n"); sb.Append("usemap ").Append(exportTextures[exportTextures.Length - 1].name).Append("\n"); } //See if this material is already in the materiallist. try { ExportMaterial objMaterial = exportTextures[t]; materialList.Add(objMaterial.name, objMaterial); } catch (ArgumentException) { //Already in the dictionary //Debug.LogError("Already in the dictionary"); } int[] triangles = exportMesh.GetTriangles(t); for (int i = 0; i < triangles.Length; i += 3) { //Because we inverted the x-component, we also needed to alter the triangle winding. sb.Append(string.Format("f {1}/{1}/{1} {0}/{0}/{0} {2}/{2}/{2}\n", triangles[i] + 1 + vertexOffset, triangles[i + 1] + 1 + normalOffset, triangles[i + 2] + 1 + uvOffset)); } } vertexOffset += exportMesh.vertices.Length; normalOffset += exportMesh.normals.Length; uvOffset += exportMesh.uv.Length; return(sb.ToString()); }