In project panel, if you click on any model object, like imported FBX model, Inspector shows Model Import Settings. If you selected Materials tabs and click Import Materials with Location set to “Use Embedded Materials”, there will be a list of Remapped Materials.
I want to get access to this list of remapped materials to set any I want. Traces lead me to SearchAndRemapMaterials(), which is not configurable.
There are several solutions to accomplish this. Here a whole code sample:
using UnityEditor;
using UnityEngine;
using UnityEditor.AssetImporters;
public class GPUInstancing : AssetPostprocessor
{
// Only neccessary for "OnPostprocessMaterial"
void OnPreprocessModel()
{
ModelImporter importer = (ModelImporter)assetImporter;
importer.materialImportMode = ModelImporterMaterialImportMode.ImportStandard;
}
// This will remap the material but not update the renderer it. So you would have to import the object twice for it the actually remap the material.
public void OnPreprocessMaterialDescription(MaterialDescription description, Material material, AnimationClip[] materialAnimation)
{
ModelImporter importer = (ModelImporter)assetImporter;
importer.AddRemap(new AssetImporter.SourceAssetIdentifier(material), (Material)AssetDatabase.LoadAssetAtPath("Assets/ProfilingData/Materials/material.2.mat", typeof(Material)));
}
// For this method to be called "Standard (Legacy) mode" has to be selected (ModelImporterMaterialImportMode.ImportStandard)
public void OnPostprocessMaterial(Material material)
{
ModelImporter importer = (ModelImporter)assetImporter;
importer.AddRemap(new AssetImporter.SourceAssetIdentifier(material), (Material)AssetDatabase.LoadAssetAtPath("Assets/ProfilingData/Materials/material.2.mat", typeof(Material)));
}
// This is probably the best solution though it will go through every renderer (so every submesh in your object)
public Material OnAssignMaterialModel(Material material, Renderer renderer)
{
ModelImporter importer = (ModelImporter)assetImporter;
importer.AddRemap(new AssetImporter.SourceAssetIdentifier(material), (Material)AssetDatabase.LoadAssetAtPath("Assets/ProfilingData/Materials/material.2.mat", typeof(Material)));
// You could use this line to change the material used by the renderer directly. It will have the same result as above, but it will not be listet as remaped material
//return (Material)AssetDatabase.LoadAssetAtPath("Assets/ProfilingData/Materials/material.2.mat", typeof(Material));
return null;
}
}
I guess the one with the “OnPostprocessMaterial” is supposed to be the ultimate solution. Unfortunately it will only be called in legacy mode. So in order to avoid possible issues I would go for the “OnAssignMaterialModel” solution:
using UnityEditor;
using UnityEngine;
public class GPUInstancing : AssetPostprocessor
{
public Material OnAssignMaterialModel(Material material, Renderer renderer)
{
ModelImporter importer = (ModelImporter)assetImporter;
importer.AddRemap(new AssetImporter.SourceAssetIdentifier(material), (Material)AssetDatabase.LoadAssetAtPath("Assets/ProfilingData/Materials/material.2.mat", typeof(Material)));
return null;
}
}