2

I am looking for a way to export a mesh into stl/obj/fbx format at runtime and save it on local files of an Android phone.

How do I do this? I am willing to use a plugin(free/paid) if it exist.

Programmer
  • 121,791
  • 22
  • 236
  • 328
Gareth Lam
  • 145
  • 1
  • 4
  • 13

1 Answers1

13

This is really complicated since you have to read the specifications for each each format (stl/obj/fbx) and understand them in order to make one yourself. Luckily, there are many plugins out there already that can be used to export Unity mesh to stl, obj and fbx.

FBX:

UnityFBXExporter is used to export Unity mesh to fbx during run-time.

public GameObject objMeshToExport;

void Start()
{
    string path = Path.Combine(Application.persistentDataPath, "data");
    path = Path.Combine(path, "carmodel"+ ".fbx");

    //Create Directory if it does not exist
    if (!Directory.Exists(Path.GetDirectoryName(path)))
    {
        Directory.CreateDirectory(Path.GetDirectoryName(path));
    }

    FBXExporter.ExportGameObjToFBX(objMeshToExport, path, true, true);
}

OBJ:

For obj, ObjExporter is used.

public GameObject objMeshToExport;

void Start()
{
    string path = Path.Combine(Application.persistentDataPath, "data");
    path = Path.Combine(path, "carmodel" + ".obj");

    //Create Directory if it does not exist
    if (!Directory.Exists(Path.GetDirectoryName(path)))
    {
        Directory.CreateDirectory(Path.GetDirectoryName(path));
    }

    MeshFilter meshFilter = objMeshToExport.GetComponent<MeshFilter>();
    ObjExporter.MeshToFile(meshFilter, path);
}

STL:

You can use the pb_Stl plugin for STL format.

public GameObject objMeshToExport;

void Start()
{
    string path = Path.Combine(Application.persistentDataPath, "data");
    path = Path.Combine(path, "carmodel" + ".stl");

    Mesh mesh = objMeshToExport.GetComponent<MeshFilter>().mesh;

    //Create Directory if it does not exist
    if (!Directory.Exists(Path.GetDirectoryName(path)))
    {
        Directory.CreateDirectory(Path.GetDirectoryName(path));
    }


    pb_Stl.WriteFile(path, mesh, FileType.Ascii);

    //OR
    pb_Stl_Exporter.Export(path, new GameObject[] { objMeshToExport }, FileType.Ascii);
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thanks for your advice. I have never thought of finding plugin in Github...... I will try all these plugins later today. – Gareth Lam Oct 14 '17 at 02:31
  • I would like to develop an Android app(for a school project) that allows users to design their own phone cases and then generate 3D geometry files in STL/OBJ format, which is ready for 3D printing. (Sorry that I didnt make clear in the question.) Thats the main reason I need a plugin for exporting gameobjects/meshes to stl/obj/fbx files at runtime in Android app. I tried both pb_Stl and UnityFBXExporter in void Start() (codes you suggested). But when I turn on my app, and then I open the internal storage. I found that nothing was created. – Gareth Lam Oct 15 '17 at 09:24
  • I would like to know could these export codes being implemented in other functions instead of void Start(). Because what I want to achieve is generating a stl file only when a specific button is clicked instead of the start of the app. – Gareth Lam Oct 15 '17 at 09:28
  • All your comments are unnecessary. The code you see are just example usage and can be used or called anytime if you put them in an non `Start` function. You may need to call `Directory.Exists` and `Directory.CreateDirectory` before saving the model or you run into [this](https://stackoverflow.com/questions/46735735/android-unity-c-sharp-unauthorizedaccessexception-writing-save-game-data/) problem. Also made a typo by putting ".fbx" as extension of all of them. Now, fixed it. See the edit. You can check if the saving worked with `File.Exists` or by reading the file with `File.ReadAllBytes` – Programmer Oct 15 '17 at 12:11