Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

First Asset Import

Discussion in 'Scripting' started by beck, May 17, 2012.

  1. beck

    beck

    Joined:
    Nov 23, 2010
    Posts:
    294
    I am moving into the art stage of my development pipeline, and it has become very tedious to go to every model and set it's import scale to 1.0 (the convention which my studio follows). This is becoming very tedious, and I would like to write a custom asset importer which will take assets when they are first imported and set their import settings to a new scale. This is to work around the problem that unity should already have options for default import settings!

    The problem with using the AssetPostProcessor class right now is that it applies to every single asset import, not the first import. This means that any change I make to the import settings with this class will be reflected on every single import. Do you have any solutions which might help me selectively apply the custom import settings?

    Unfortunately, when the asset importer reaches the PreProcess stage, the AssetImporter already exists, so checking for an asset at the assetPath can't yield whether the asset is importing for the first time or not. My other solution would be to maintain a list of assets which I have already PreProcessed, but this would create serious problems if that list were damaged somehow.

    My other thought is to somehow save on each asset whether it was preprocessed yet or not. However, I looked through the variables available on the asset importer and I can't find any that are unused in my game that I could use as a hook. Are there any methods of attaching additional information to an asset importer?

    Thanks for any help you can offer.
     
  2. beck

    beck

    Joined:
    Nov 23, 2010
    Posts:
    294
    Well, I found a solution to the problem that works to my liking. Here is the script if anyone is interested:

    Code (csharp):
    1.  
    2. public class AssetProcessing {
    3.     [MenuItem("Assets/Processing/SetMeshDefaults")]
    4.     public static void SetDefaultMeshSettings () {
    5.         foreach(Object o in Selection.objects){
    6.             if(!AssetDatabase.Contains(o)) continue;
    7.             string path = AssetDatabase.GetAssetPath(o);           
    8.             ModelImporter importer = AssetImporter.GetAtPath(path) as ModelImporter;
    9.             if(importer!=null){
    10.                 importer.globalScale = 1.0f;
    11.                 importer.importMaterials = false;
    12.                 importer.generateAnimations = ModelImporterGenerateAnimations.None;
    13.                 AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
    14.             }
    15.         }
    16.         Selection.objects = new Object[0];
    17.     }
    18. }
    19.  
     
  3. iwHiteRabbiT

    iwHiteRabbiT

    Joined:
    Feb 15, 2011
    Posts:
    47
    Hello

    Same issue here, actually I have resolved it with:
    Code (csharp):
    1.  
    2. public class FBXOverride : AssetPostprocessor
    3. {
    4.     public void OnPreprocessModel()
    5.     {
    6.         ModelImporter importer = assetImporter as ModelImporter;
    7.         String name = importer.assetPath.ToLower();
    8.  
    9.         if (File.Exists(AssetDatabase.GetTextMetaFilePathFromAssetPath(name)))
    10.             return;
    11.  
    12.         if (name.Substring(name.Length - 4, 4)==".fbx")
    13.         {
    14.             importer.globalScale = 1.0F;
    15.             importer.animationType = ModelImporterAnimationType.None;
    16.             importer.importAnimation = false;
    17.             importer.materialName = ModelImporterMaterialName.BasedOnMaterialName;
    18.         }
    19.     }
    20. }
    21.  
     
  4. mikelortega

    mikelortega

    Joined:
    Feb 5, 2014
    Posts:
    43
    Hello @iwHiteRabbiT,
    This solution worked perfectly with Unity 2018 and previous, but not with Unity 2019. It seems that now the .meta files are created before OnPreprocessModel gets called. Do you have any solution to detect the first import with Unity 2019?
     
  5. mikelortega

    mikelortega

    Joined:
    Feb 5, 2014
    Posts:
    43
    Asakawa2, elechen and honyapeke like this.
  6. richard_harrington

    richard_harrington

    Unity Technologies

    Joined:
    Sep 29, 2020
    Posts:
    8
    Note: importSettingsMissing only returns true if the asset has no sibling .meta file, which isn't necessarily the same as "the first time it was imported". For example, if you give somebody a .png and its associated .png.meta, then it importSettingsMissing will return false.
     
    mikelortega likes this.