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!

Yes. You want to write and editor script (they usually import UnityEditor). Then I would:

  • copy obj file from your location to the project folder (if it's not there yet)
  • then AssetDatabase.LoadMainAssetAtPath(path to your mesh) (or something like that)
  • cast result of previous to GameObject
  • Find mesh renderer on your game object and do other stuff you want :)

I hope it gives you basic idea.