Material import and TextureImporter / AssetPostprocessor

Hello guys,

I have a question on how to alter the asset pipeline, and what the best path might be. Here’s the situation:

I have bunch of FBX files, let’s say 4. The 4 share the same texture, were exported separately. When I import them into Unity, it’ll create 4 materials, with 4 .fbm files with 4 copies of the image. I’d like them all to share the same material, so that only 1 material and 1 texture copy gets imported instead of N.

At first I thought that the way to hook this in would be via TextureImporter, hashing the material against a list of known ones, and assign a pre-existing one instead of creating a new one if there’s a match. I can’t see a way to access the Texture2D in TextureImporter, however, and this seems to be closer to that provided by AssetPostprocessor.OnAssignMaterialModel.

Would hooking into AssetPostprocessor.OnAssignMaterialModel and hashing the material then be the right workflow? Any better suggestions?

Thanks in advance,

It would seem another - maybe hackier - alternative is to add an user property indicating the texture, and read it on OnPostprocessGameObjectWithUserProperties. It seems exceedingly brittle and error-prone, however.

Only a single texture copy gets imported unless you drop them into 4 own folders so the texture is added 4 times, because they are considered different, otherwise you would place them into different places

Sort of having a conversation with myself here, but I’d rather not start a separate thread for a related topic.

When AssetPostprocessor.OnAssignMaterialModel gets called, one has the opportunity to return a different material to be assigned, for instance creating it in another path. There doesn’t seem to be something similar for OnPostprocessTexture, where one can only alter the texture itself. I considered moving them OnPostprocessAllAssets, but this would break the materials that reference them.

is there a better way?

Hello Dreamora,

I figured that’s what was expected, but the FBX importer is not behaving like that at all. I have the importer material settings “Per texture” and all files in the same folder, and it’s generating multiple materials and texture copies. I’ll attach some screenshots.

181695--6453--$materials_750.png

As you can see in the previous screenshot, it’s generating a bunch of materials for “08 - default”, each for a transicionXX object. This screenshot shows how it’s also creating a separate .fbm directory and importing a copy of the texture on each.

181696--6454--$textures_112.png

Magnus Blikstad mentioned that Unity doesn’t recognize the textures when exported as embedded from 3ds, so they should be exported separately. Doing so and storing them in a Textures folder causes Unity to recognize them, import them as described in the documentation, and avoids the material proliferation.