ObjImporter.ImportFile C# (CSharp) Method

ImportFile() public method

public ImportFile ( string filePath ) : Mesh
filePath string
return Mesh
    public Mesh ImportFile(string filePath)
    {
        meshStruct newMesh = createMeshStruct(filePath);
        populateMeshStruct(ref newMesh);

        Vector3[] newVerts = new Vector3[newMesh.faceData.Length];
        Vector2[] newUVs = new Vector2[newMesh.faceData.Length];
        Vector3[] newNormals = new Vector3[newMesh.faceData.Length];
        int i = 0;
        /* The following foreach loops through the facedata and assigns the appropriate vertex, uv, or normal
         * for the appropriate Unity mesh array.
         */
        foreach (Vector3 v in newMesh.faceData)
        {
            newVerts[i] = newMesh.vertices[(int)v.x - 1];
            if (v.y >= 1)
                newUVs[i] = newMesh.uv[(int)v.y - 1];

            if (v.z >= 1)
                newNormals[i] = newMesh.normals[(int)v.z - 1];
            i++;
        }

        Mesh mesh = new Mesh();

        mesh.vertices = newVerts;
        mesh.uv = newUVs;
        mesh.normals = newNormals;
        mesh.triangles = newMesh.triangles;

        mesh.RecalculateBounds();
        mesh.Optimize();

        return mesh;
    }

Usage Example

Example #1
0
    void loadmesh()
    {
        ObjImporter import = new ObjImporter();

        leftmesh  = import.ImportFile(Application.streamingAssetsPath + "/upper.obj");
        rightmesh = import.ImportFile(Application.streamingAssetsPath + "/bottom.obj");
    }
All Usage Examples Of ObjImporter::ImportFile