Default materials on meshes

Hello,

I'm really surprised that nobody asked that yet (or at least I didn't find it), but I have a question regarding the default material of a mesh when it's spawned in a scene:

How can I apply a default material to multiple meshes? The only way I know to apply a default material at all is naming it [mesh_name]-defaultmat, which is obviously not usable for materials that can be used for more than one mesh.

Thanks in advance,

Moritz

From your description, I'm pretty sure you're referring to the materials generated on import of an object when material generation is turned on. When an object is "spawned in a scene", it uses the materials assigned to the prefab. You could change the materials after the import, but it's easier in my opinion to simply fix the problem before it starts.

I wanted to assign my own materials to my geo on import for some of my mats so that they use the same mat shared across multiple models. I created an editor script which would assign stored material assets in stead of generated ones, using the names of the materials as a basis.

//Goes in the Assets/Editor folder
class MaterialOverrideProcessor extends AssetPostprocessor {
    var materialsPath : String = "Materials";

    function OnAssignMaterialModel(material : Material,
                                   renderer : Renderer) : Material {
        var materialPath : String = "Assets/" + materialsPath + "/"
                                    + material.name + ".mat";
        return AssetDatabase.LoadAssetAtPath(materialPath, Material);
    }
}

If you wanted to do something special for materials whose names contain default, you could just add some string comparison to look for that:

//Goes in the Assets/Editor folder
class MaterialOverrideProcessor extends AssetPostprocessor {
    var materialsPath : String = "Materials";

    function OnAssignMaterialModel(material : Material,
                                   renderer : Renderer) : Material {
        var materialName : String = material.name;
        if(materialName.Contains("default")) materialName = "default";
        var materialPath : String = "Assets/" + materialsPath + "/"
                                    + materialName + ".mat";
        return AssetDatabase.LoadAssetAtPath(materialPath, Material);
    }
}

If LoadAssetAtPath cannot find the material, it will return null. If OnAssignMaterialModel returns null, then the default material generation will be used. If you wanted to create a new mat or something, you could create that mat and then return the created mat like they do in the OnAssignMaterialModel docs.

If you've already imported your objects and put them into the scene and lost their prefab links, you could always write a scriptableObject script that will recurse through your asset and replace the mats.