Search code examples
c#unity-game-engineunity-editor

How to get the material name that imported from fbx via script in unity?


enter image description here This is my import setting for a FBX file, what I want to get is the material name that imported from 3dsmax, but I can't find a way to achieve it properly. What I have tried is OnPreprocessMaterialDescription() (https://docs.unity3d.com/ScriptReference/AssetPostprocessor.OnPreprocessMaterialDescription.html), from my understanding, the material description include the name info I want, and my code is blow, the OnPreprocessMaterialDescription() seems not working, it print out nothing. Or is there any other way to get the name info i need? please help, thanks in advance!

public class Test : AssetPostprocessor
{
    private void OnPreprocessModel()
    {
        var modelImporter = assetImporter as ModelImporter;
        //set material imported mode to material description
        modelImporter.materialImportMode = ModelImporterMaterialImportMode.ImportViaMaterialDescription;
    }
    public void OnPreprocessMaterialDescription(MaterialDescription description, Material material, AnimationClip[] materialAnimation)
    {
        Debug.Log(description.materialName);
    }

}

Solution

  • I don't know why your code in particular does not work, but if you don't insist on preprocessing, the model is already structured in the Unity-typical way on post-process so you can just access the materials like you would in a scene object:

    void OnPostprocessModel(GameObject model) {
       
     foreach(MeshRenderer mr in model.GetComponentsInChildren<MeshRenderer>()) {
       
      Debug.Log(mr.sharedMaterial);
     }
    }