Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2017.2.0b11 Model Importer

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

  1. axun1992

    axun1992

    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:
    29
    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:
    234
    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:
    29
    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!
     
    roomle_atti and KB73 like this.