blog games developers documentation portfolio gallery

More in this category:

SimpleObj

Import Wavefront .OBJ models into Unity3D at runtime. SimpleObj provides the tools to let users upload 3D models and use them in your game.

Supports multiple meshes


If you have multiple objects in 1 obj file they are all imported as GameObjects that are children of the first mesh in the .obj file.

Supports submeshes


If a mesh has multiple materials, each material will have its own submesh.

Supports big meshes


Unity does not support meshes that have more than 64K vertices. However, when you import an OBJ file with a mesh that is larger than 64K vertices, it will automatically be split into multiple parts. For each part a child game object is made. The child objects together will render exactly like the original mesh would have.

Supports materials .MTL and textures


If you provide the import function with the contents of the .MTL file and an array of textures, the material are automatically created and applied. The following material attributes are supported.
Kd diffuse color
Ks specular color
d/Tr transparency
Ns specularity
map_Kd diffuse (main)texture file name

Limitations


The OBJ specifications entail more than just vertices, uv coordinates and normals, but this importer is called "SimpleObj" for a reason. It imports vertices, normals, 1 uv map, triangles and materials. (polygons are automatically converted to triangles). This means that bone weights and rig definitions (among whatever else is enclosed in your OBJ file) is not imported.

The demo scene that is included allows you to download any OBJ model and import it. It has sliders to adjust the rotation, scale and position of the mesh and gives a good idea of how to use the importer. You can also run the demo here.

To obtain the SimpleObj package, go to the Unity Asset Store.

To use SimpleObj in your Unity project, you need to have the files ObjImporter.cs, CollectionExtensions.cs, MeshExtensions.cs and StringExtensions.cs somewhere in your project folder.

Some code examples:

// import a OBJ string
myGameObject = ObjImporter.Import(objString);

// import a model + materials string + texture
myGameObject = ObjImporter.Import(objString, mtlString, new Texture2D[] {aTexture});

// If model has Z up and uses inches as unit
myGameObject = ObjImporter.Import(
objString,
Quaternion.Euler(270,0,0), // rotate over X
new Vector3(0.0254f, 0.0254f, 0.0254f), // scale down
Vector3.zero); // no translation













follow us