- Home /
Import Material from filepath?
I'm using the ObjImporter from the wiki to import custom .obj files at runtime, to allow user-created assets. I then create a new GameObject, add a MeshFilter, assign the MeshFilter's mesh to the mesh returned from ObjImporter, and finally add a MeshRenderer.
However, the newly-created MeshRenderer (obviously) has no Material set. One can assume that a .mat file should have been generated alongside the .obj file, so the logical solution would be for the user to specify a string filepath from which to load the .mat, the same way I load the .obj file. However, I can't seem to find a way in which to do so.
Does Unity have a built-in way to import materials at runtime, similar to Resources.Load? If not, do I need to write a custom .mat importer, or is there one available already?
Edit: I'm using Unity Free and have no financial means of upgrading in the foreseeable future, so if it requires Pro I have no way of using it.
Answer by Frank Yeh · Nov 26, 2012 at 09:11 AM
Hi, Did you try to put the material to a dummy Cube object and save it to a prefab, put the prefab in the Resources folder then load it during runtime, finally get the material from that object you just loaded?
Perhaps I should have explained my situation a bit better. This is for mod support; i.e. the ability for users to import custom models and have them work ingame.
While I'm sure I could probably import the .mat by hand by clicking and dragging from within the Unity window, I'd prefer to do it programmatically, as users who don't have access to the Unity editor wouldn't be able to import the material in such a way, they could only pass a reference to a filepath.