Hi,
When I try to import fbx or obj-files in the Beta the Unity Editor does not create the Materials-folder but instead forces the standard materials for the imported materials. Because of performance issues I need to change the material for Hololens Development (to Holotoolkit/FastConfigurable) and can not use standard. In 2017.2.0f3 the Materials-folder gets created.
Hi,
Importing materials as sub-assets is the new default in Unity starting with 2017.3.
You have several options for reproducing the legacy behavior, which involve creating an asset postprocessor:
-
If you already have a library of materials that you want to use, you can use the SearchAndRemapMaterials() method of the ModelImporter. See the example here: Unity - Scripting API: ModelImporter.SearchAndRemapMaterials
-
If your material library is not complete and you’d like to extract new materials from the imported assets, you can write an asset post processor 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);
}
}
}
- If you would like to just preserve the old behavior, no questions asked, you can set the ModelImporter.materialLocation property to External in a similar asset postprocessor. This option has the drawback of making your imports non-deterministic and will be removed in future releases.
Hope this helps!
2 Likes
The third option would be to use the new (well 2017.2) Materials tab and select Use External Materials (Legacy) on the imported file - we hope to have that in the documentation ASAP
2 Likes
One more option is to continue to use the embedded materials and to remap them to the ones in your library using the Search and Remap button on the Materials tab.
2 Likes