2017.2.0b11 Model Importer

Model importer inspector now has a Material item,the setting about “Material Location” seems dont have api to set.
so,an FBX file with embedded texture can not build assetbundle by CMD correctly.it will loss texture.

Hi,

The Material Location option is not exposed in the scripting API by design. That is because the legacy mode of extracting materials will be deprecated in the future and we do not want to break user code.

If you want to automate the extraction of the embedded textures, you can create an AssetPostprocessor and implement OnPreprocessModel. There you can call ModelImporter.ExtractTextures().

class AutoExtractTextures : AssetPostprocessor
{
    void OnPreprocessModel()
    {
        ModelImporter modelImporter = assetImporter as ModelImporter;
        modelImporter.ExtractTextures("Assets/Textures");
    }
}

We are also working on a method that will allow you to search for the extracted materials in the project and use them instead of the imported materials, just like in the legacy mode.

If you are still blocked, please file a bug and we’ll make sure the issue gets tracked and resolved.

Hi, yes we are blocked by this as well, we need materials extracted ( re: legacy version ) …some ability to do this via script

Hi,

You can extract your materials as well using an AssetPostprocessor, as follows:

class MaterialExtractorPostprocessor : AssetPostprocessor
{
    public static void OnPostProcessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    {
        var materialsPath = "Assets/Materials";
        Directory.CreateDirectory(materialsPath);

        var assetsToReload = new HashSet<string>();

        foreach (var assetPath in importedAssets)
        {
            if (assetPath.ToLower().Contains(".fbx"))
            {
                var importer = AssetImporter.GetAtPath(assetPath) as ModelImporter;

                var materials = AssetDatabase.LoadAllAssetsAtPath(importer.assetPath).Where(x => x.GetType() == typeof(Material));

                foreach (var material in materials)
                {
                    var newAssetPath = string.Join(Path.DirectorySeparatorChar.ToString(), new[] { materialsPath, material.name }) + ".mat";

                    var error = AssetDatabase.ExtractAsset(material, newAssetPath);
                    if (string.IsNullOrEmpty(error))
                    {
                        assetsToReload.Add(importer.assetPath);
                    }
                }
            }
        }

        foreach (var path in assetsToReload)
        {
            AssetDatabase.WriteImportSettingsIfDirty(path);
            AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
        }
    }
}

Hope this helps!

2 Likes