x


How NOT to create redundant materials with new names on import?

Let's say I have an asset A that contains materials X,Y,Z.

I have another asset B that also contains materials X,Y,Z.

I would like to import A and then import B and create only materials X,Y,Z in unity3D. Instead, I am getting materials A-X A-Y A-Z B-X B-Y B-Z

Now imagine I want to import 500 assets that all use X,Y,Z. I'd really rather not have 1500 materials when I only need 3.

Thanks for any tips or pointers!

more ▼

asked Apr 12 '10 at 01:02 PM

pickledzebra gravatar image

pickledzebra
286 31 35 44

(comments are locked)
10|3000 characters needed characters left

2 answers: sort newest

I have a postprocessor that does just that: http://buchhofer.com/?p=228 adjust your paths to suit, and also your default shader type.

//file: MaterialsPostProcessor.cs
//goal: Share a library of materials across different assets 
//based on the name of material supplied in the FBX file
//Dave Buchhofer - 02.10.2010

using UnityEngine;
using UnityEditor;
using System.Collections;

public class MaterialsPostProcessor : AssetPostprocessor
{
    Material OnAssignMaterialModel (Material material, Renderer renderer)
    {
        //The path where you keep your "Standard" library of materials
        string StandardMatPath = "Assets/DMGStandardMaterials/" + material.name + ".mat";

        //the path where you want to keep your "temp" materials, that weren't found above
        //So you have a logical place to look for materials that need editing and tweaking for realtime work.
        string TemporaryMatPath = "Assets/Mesh/Materials/" + material.name + ".mat";

        //Check for it in the standard 
        if (AssetDatabase.LoadAssetAtPath(StandardMatPath, typeof(Material)))
        {
            Debug.Log("FOUND: " + StandardMatPath);
            return (Material)AssetDatabase.LoadAssetAtPath(StandardMatPath, typeof(Material));
        }
        //Else check to see if we've already built one in the temp path
        if (AssetDatabase.LoadAssetAtPath(TemporaryMatPath, typeof(Material)))
        {
            Debug.Log("FOUND temp: " + TemporaryMatPath);
            return (Material)AssetDatabase.LoadAssetAtPath(TemporaryMatPath, typeof(Material));
        }
        //Or create it?
        material.shader = Shader.Find("Diffuse");
        AssetDatabase.CreateAsset(material, TemporaryMatPath);
        Debug.Log("CREATED temp: " + TemporaryMatPath);
        return material;
    }
}
more ▼

answered Apr 12 '10 at 09:45 PM

davebuchhofer gravatar image

davebuchhofer
492 7 11 24

Thanks so much. I'll try these approaches.

Apr 12 '10 at 11:37 PM pickledzebra
(comments are locked)
10|3000 characters needed characters left

You have to create a script for the assetPostprocessor to say that I want unity to assign these materials. Here is an example tool that creates colliders only for the objects containing the word collider in them. The logic is the same, consider using user properties in your objects (Max,Maya) for more detailed control concerning materials.

Have a look at this answer too. here

I am still struggling to create a proper pipeline, but if you are more adept in coding it seems fairly easy to do whatever you want just by naming objects in your 3d app.

more ▼

answered Apr 12 '10 at 03:03 PM

alexnode gravatar image

alexnode
984 27 32 49

Thanks for the pointers. My pipeline is goofy - I have very little if any control over the user properties of the objects I'm importing. I'll give the postprocessor scripting a try.

Apr 12 '10 at 11:40 PM pickledzebra
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x969
x807
x417
x1

asked: Apr 12 '10 at 01:02 PM

Seen: 1884 times

Last Updated: Apr 16 '10 at 05:24 PM