Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Unity 2017.2 beta is now available for download.
    Dismiss Notice

2017.2.0b11 Model Importer

Discussion in '2017.2 Beta' started by easycodescanner, Sep 14, 2017.

  1. easycodescanner

    easycodescanner

    Joined:
    Jun 17, 2014
    Posts:
    1
    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.
     
  2. bogdancoder

    bogdancoder

    Unity Technologies

    Joined:
    Feb 6, 2017
    Posts:
    3
    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().

    Code (CSharp):
    1. class AutoExtractTextures : AssetPostprocessor
    2. {
    3.     void OnPreprocessModel()
    4.     {
    5.         ModelImporter modelImporter = assetImporter as ModelImporter;
    6.         modelImporter.ExtractTextures("Assets/Textures");
    7.     }
    8. }

    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.
     
  3. KB73

    KB73

    Joined:
    Feb 7, 2013
    Posts:
    109
    Hi, yes we are blocked by this as well, we need materials extracted ( re: legacy version ) ..some ability to do this via script
     
  4. bogdancoder

    bogdancoder

    Unity Technologies

    Joined:
    Feb 6, 2017
    Posts:
    3
    Hi,

    You can extract your materials as well using an AssetPostprocessor, as follows:
    Code (CSharp):
    1. class MaterialExtractorPostprocessor : AssetPostprocessor
    2. {
    3.     public static void OnPostProcessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    4.     {
    5.         var materialsPath = "Assets/Materials";
    6.         Directory.CreateDirectory(materialsPath);
    7.  
    8.         var assetsToReload = new HashSet<string>();
    9.  
    10.         foreach (var assetPath in importedAssets)
    11.         {
    12.             if (assetPath.ToLower().Contains(".fbx"))
    13.             {
    14.                 var importer = AssetImporter.GetAtPath(assetPath) as ModelImporter;
    15.  
    16.                 var materials = AssetDatabase.LoadAllAssetsAtPath(importer.assetPath).Where(x => x.GetType() == typeof(Material));
    17.  
    18.                 foreach (var material in materials)
    19.                 {
    20.                     var newAssetPath = string.Join(Path.DirectorySeparatorChar.ToString(), new[] { materialsPath, material.name }) + ".mat";
    21.  
    22.                     var error = AssetDatabase.ExtractAsset(material, newAssetPath);
    23.                     if (string.IsNullOrEmpty(error))
    24.                     {
    25.                         assetsToReload.Add(importer.assetPath);
    26.                     }
    27.                 }
    28.             }
    29.         }
    30.  
    31.         foreach (var path in assetsToReload)
    32.         {
    33.             AssetDatabase.WriteImportSettingsIfDirty(path);
    34.             AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
    35.         }
    36.     }
    37. }
    Hope this helps!
     
    netural_atti and KB73 like this.