在运行时将网格转换为stl / obj / fbx

时间:2017-10-13 15:37:08

标签: c# android unity3d mesh fbx

我正在寻找一种在运行时将网格导出为stl / obj / fbx格式的方法,并将其保存在Android手机的本地文件中。

我该怎么做?如果存在,我愿意使用插件(免费/付费)。

1 个答案:

答案 0 :(得分:7)

这非常复杂,因为您必须阅读每种格式(stl / obj / fbx)的规范并理解它们才能自己制作。幸运的是,已经有许多插件可用于将Unity网格导出到stl,obj和fbx。

<强> FBX

UnityFBXExporter用于在运行时将Unity网格导出到fbx。

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

对于obj,使用ObjExporter

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

您可以将pb_Stl插件用于STL格式。

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);
}