Load .OBJ file and textures into Editor’s viewport via script

Hi All!

I am working on some editor utility and I’ve encountered serious problem. It seems to be easy thing, cause in 3D packages this may be solved quite easily via Python, MaxScript, LScript etc., but i can’t find any solution in unity. This is my problem:

I have full paths to the .OBJ file and to the textures stored inside variables in my script (i am reading those from the text file). For example, in MyGeo variable i have string “C:\TEMP\My_Project\Sphere.obj”, in MyTexture_Diffuse i have string “C:\TEMP\My_Project\Sphere_Diffuse.png”, in MyTexture_Normal i have string “C:\TEMP\My_Project\Sphere_Normal.png”

What i need is to load .OBJ file from MyGeo variable to the scene (and to show it in editor’s viewport if possible), then assign needed shader to this loaded OBJ and to load textures from the variables into texture inputs of the shader.

How all this may be done in unity script? I will be very grateful for any help with this, thanks in advance!

You are going to have to write an importer. You can use the WWW object to read the file, if it is binary you want to use System.BitConverter class. for text, you would want to use parseInt and the other similar functions. (I don’t actually know where the documentation is for it though. I believe it is the same as the actual JavaScript functions)

To piece them all together, you will need to use the Mesh object.

Textures like JPG and PNG can be loaded through the WWW object, you would have to create an importer for other types though. You can use the bytes part of the WWW object if you need it.

I had created a 3ds importer and TGA texture importer at one point, so it can be done. :wink:

You may get lucky and find a previously made one for unity on the Wiki or by Google searches.