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

Gar*_*Lam 1 c# android mesh unity-game-engine fbx

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

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

Pro*_*mer 9

这非常复杂,因为您必须阅读每种格式(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);
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)