Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ELREVENGE · Jul 24, 2015 at 07:15 PM · androidmeshsave

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by 1Piotrek1 · Jul 25, 2015 at 12:55 PM

 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 [i] = -normals [i];
         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}\n", v.x, v.y, -v.z));
         }
         sb.Append ("\n");
         foreach (Vector3 nn in m.normals) {
             Vector3 v = r * nn;
             sb.Append (string.Format ("vn {0} {1} {2}\n", -v.x, -v.y, v.z));
         }
         sb.Append ("\n");
         foreach (Vector3 v in m.uv) {
             sb.Append (string.Format ("vt {0} {1}\n", v.x, v.y));
         }
         for (int material=0; material < m.subMeshCount; material ++) {
             sb.Append ("\n");
             sb.Append ("usemtl ").Append (mats [material].name).Append ("\n");
             sb.Append ("usemap ").Append (mats [material].name).Append ("\n");
             
             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}\n", 
                                         triangles [i] + 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 [i] = -normals [i];
         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"
                           + "\n#" + System.DateTime.Now.ToLongDateString() 
                           + "\n#" + System.DateTime.Now.ToLongTimeString()
                           + "\n#-------" 
                           + "\n\n");
         
         Transform t = transform;
         
         Vector3 originalPosition = t.position;
         t.position = Vector3.zero;
         
         if (!makeSubmeshes)
         {
             meshString.Append("g ").Append(t.name).Append("\n");
         }
         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
                           + "\n#-------" 
                           + "\n");
         
         if (makeSubmeshes)
         {
             meshString.Append("g ").Append(t.name).Append("\n");
         }
         
         MeshFilter mf = t.GetComponent<MeshFilter>();
         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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ELREVENGE · Jul 29, 2015 at 09:05 PM 0
Share

How would I add this in? in the link you sent says to put it onto the editor folder. Is there a specific function that must be called for it to work or something?

avatar image 1Piotrek1 · Jul 30, 2015 at 06:39 AM 0
Share

I changed it to mono behaviour script, so you need to drag and drop it to gameobject that mesh you need to save and send message or invoke DoExport (true) function.

avatar image ELREVENGE · Aug 06, 2015 at 12:40 AM 0
Share

Thank you so much. The script with a few small modifications works really well for what I needed it for. Thanks for responding to me earlier, it really helped me out.

avatar image
0

Answer by Voxel-Busters · Aug 08, 2015 at 12:16 PM

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 link.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Common gamestates between devices/platforms. 1 Answer

Saving game question. 1 Answer

External Saves for android games 1 Answer

Save/ Load in Editor created meshes 1 Answer

Mesh Collider not working so good 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges