Mesh Obj export/import WITH SHADER REFERENCE

Hi everyone,

I’ve been playing around with the ObjExporter to export several meshes to an .obj file at once. In order to make Unity use existing materials when importing a previously exported mesh I simply put a folder called “Materials” with the original materials into the obj’s folder before the import and changing the default import settings.

As I see it the ObjExporter doesn’t properly export the materials (it only writes grey materials to the .mtl file). However, the .mtl file is still useful in the way that it tells unity which mesh uses which material and with the correct import setting it simply uses existing materials with the same name that is used in the .mtl file and referenced in the .obj file by usemtl .

In addition to the material reference I really need a reference to shaders. In my case game objects in the scene may have the same material but I assign different shaders to them during runtime.

Is there anything like “useshader” that I can write to the .obj file so that unity uses the material specified by usemtl with the specified shader (which preferably lies within a folder called “Shaders” next to the “Materials” folder ???

This is really important because the shaders are assigned dynamically to a lot of objects and I just can’t assign them myself after import !!!

Thanks in advance for your help!


Another idea that just came to me is to write the references to a file during the .obj export and write a custom import script that assigns the shaders afer the model was imported according to this file. How would I go about doing this ???

So in the end I wrote the references to a TextAsset. Then, in another project where I import the previously exported mesh, I had to attach a script to the GameObject (that was initialized from prefab, which is turn was created by unity from the .obj and .mtl files), which reads the TextAsset and assigns the shaders accordingly.

Not really a pretty solution :-/ . Does anyone have a better solution in mind?

I believe the best way to do this would be to reference materials instead at runtime as that is primarily where you choose which shader to use (think of your unity project as the master material library) So in other words setup the appropriate library of materials in Unity first, then when you assign the material at runtime as discussed here for example:

It uses the appropriate shader you have setup for it in Unity.

sketchup(with plugin) exports .obj with a .mtl that unity recognizes and assigns correctly as long as there is only a single colour per sketchup group…maybe you can import into sketchup and export to something unity recognizes…

nothings for sure here. goodluck