Loading 3d models at runtime in Unity3d
When building a dynamic application, every now and then you might need to load some 3d models at runtime. Either because there's a database with models you want to use or just because you want someone to update the models without rebuilding the project. I actually had this situation a few weeks ago. Surprisingly, this option does not come with Unity out of the box.
This post is an addition to the last series I wrote about dynamic content loading in Unity3D.
IMPORTANT UPDATE - With Unity 3 there were some changes in the API and code described below doesn't work. Here's a new version, that works with Unity 3: obj (v1.2). Now, instead of creating a new object, attach the OBJ script to a game object and add the path to the .obj file in the inspector.
Asset bundles and resource folders
But first thing first. I said there's no ready-to-use way to load 3d models at runtime, but it's not 100% true. In fact, there are two options: asset bundles and resource folders. These methods are discussed in depth here.
They can both be useful in some cases, but they both have one fundamental drawback: in order to create them, someone needs to open the project in Unity, import the 3d models, create the bundles or resource folders and then export the whole thing again. To create an AssetBundle you need to run a script in the editor, while the Resource Folders… honestly, I failed to even make them work!
In brief, it's all too complicated for what I was looking for.
A straightforward solution
What I needed and wanted was easy and simple: to load a 3d object (geometry + some materials and textures) at runtime, using just a URL as parameter. The only solution seemed to write my own importer.
Basically, I had the choice between Collada and Wavefornt OBJ. I would choose OBJ any time of the day because it's a simple, concise plain-text format, while Collada is bloated and is XML-based.
It's not that I hate XML (although I'm not a big fan either) but in order to parse XML you need to include a pretty weighty DLL in your *.unity3d file, around 850Kb, which in this case (and in many others) defeats the purpose. Still, it's good to know that it possible, and there are situations when it's ok to use it. If you want to learn more about Unity3D and XML there's a awesome article on this topic by Paul Tondeur.
It turned out that while it's not rocket science to write an OBJ importer, it's not exactly banal either. I spent a few days coding it so I thought I'll share this with everyone – maybe someone will make good use of it.
Here's a package with the source code (v1.1) (or a version that works in Unity 3 (v 1.2)), along with a simple scene demonstrating how it works. In fact it couldn't be simpler. All you have to do is to create an instance of the OBJ class and start a coroutine to load the contents, like this:
string path = "http://www.everyday3d/unity3d/obj/monkey.obj";
OBJ obj = new OBJ();
StartCoroutine(obj.Load(path));
Supported features
- Vertex, normals and UV data
- Multiple objects per file
- Vertex groups and multiple materials per object
- MTL files, diffuse and specular materials
- Basic textures
And it's all at a cost of ~12Kb extra added to your final file!
Testing and the universality of OBJ format
In the 3D world the OBJ format is nearly universal – I think that every 3D editor in existence is able to export to this format. This also means that Wavefront files come in many different flavors.
I tested the code against models created with Blender and against common sense assumptions on how an OBJ file can be constructed. If it doesn't work with files exported from your editor send me the OBJ file, and if possible I'll update the code. It's released under the MIT license so feel free to use it in your projects, commercial or not.
That's it! I hope you'll find it useful!