Search Unity

Can i change the default import settings?

Discussion in 'Editor & General Support' started by baylor, May 27, 2010.

  1. baylor

    baylor

    Joined:
    Oct 29, 2009
    Posts:
    25
    There are two settings (bugs?) on imported files that are making me miserable

    PNGs: isReadable is not set
    3DSMax: scale set to 0.01

    The first makes my textures unreadable to my application and blows things up. The second shrinks all my models to 1/100th of their normal size

    Is there a way to make Unity not do this when adding a new asset?

    It wouldn't be too awful a problem except i have hundreds of both and we're adding a few dozen new ones each week (it's a fashion game, so we have lots of little shoes, textures, etc.). All are (and, for our game, must be) in the same folder. Unity has no sort by date option in the editor so it's not easy to find the new additions. If i manually set these values, it takes half an hour or more and i invariably miss one, catching it only when something blows up

    Option 2: We have an engine script to fix this automatically. Pretty nice but it takes half an hour to run and pegs my CPU so nothing else works while it's going. Everyone on my team has to do this several times a week, so we're losing noticeable amounts of time

    Yes, we could re-work our asset management process to minimize how often this happens, but right now, it's kind of painful

    So, fingers crossed, i'm asking - is there a way to have Unity, by default, turn on isReadable for images or use the proper scale of 1 for models?
     
  2. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    You can write small editor scripts like this to change default behaviour.

    this script forexample disables importing of animations and resets the scale value to 1.0:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System;
    4.  
    5. public class FBXScaleOverride : AssetPostprocessor {
    6.     void OnPreprocessModel() {
    7.         ModelImporter importer = assetImporter as ModelImporter;
    8.         String name = importer.assetPath.ToLower();
    9.         if (name.Substring(name.Length - 4, 4)==".fbx") {
    10.             importer.globalScale = 1.0F;
    11.             importer.generateAnimations = ModelImporterGenerateAnimations.None;
    12.         }
    13.     }
    14. }
     
    Rodolfo-Rubens likes this.
  3. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    It was good... if it worked :(

    Code (csharp):
    1. Assets/SCRIPTS/DefaultMeshImportSettings.cs(2,1): error CS0246: The type or namespace name `UnityEditor' could not be found. Are you missing a using directive or an assembly reference?
     
  4. tomvds

    tomvds

    Joined:
    Oct 10, 2008
    Posts:
    1,028
    Editor scripts must be placed in a directory called 'editor' to compile properly. The directory can be anywhere in your asset folder.
     
  5. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    Ahhh yes sorry, I have put it in SCRIPTS editor :oops: :oops: :oops:
    Thank you man, you saved me around 10 seconds on import on each mesh :D
     
  6. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    I also recently needed that and I have found this:

    http://wiki.unity3d.com/index.php/ModelImporterPresetManager

    This was intended for Unity 3.5, but works even in the latest 5.5 version, although some settings are missing.
    I have updated this script for 5.5, adding the rest of the settings - if someone needed that.
     
  7. Tyrope

    Tyrope

    Joined:
    Nov 14, 2016
    Posts:
    1
    Sorry for reviving this old post with a slightly off-topic request. But can you update this code on the wiki page you linked? It will help (potentially a lot of) future users with this issue.
     
  8. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    973
    This is it.
    I made the updates, but haven't had an opportunity to test it yet.
    Check it in action and let me know. If it's fine I could update it on wiki.
     

    Attached Files:

  9. avi9111

    avi9111

    Joined:
    Feb 24, 2016
    Posts:
    31
    great, these answers are what i need
    I'm impress that you guys got this skill in the year 2010.
    What did I do on these years,
    I wish we could meet at the same period
     
  10. avi9111

    avi9111

    Joined:
    Feb 24, 2016
    Posts:
    31
    I did not fully test it's parmater, but it seems fine.


     
    jmikusch likes this.
  11. Homeak16

    Homeak16

    Joined:
    Feb 27, 2017
    Posts:
    19
    Hi,

    your script is very useful in Editor, but how i can build my project with it?
    Or it's imposible?

    Best regards.
     
  12. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    You put it in a folder named "Editor"

    See here.
     
  13. Homeak16

    Homeak16

    Joined:
    Feb 27, 2017
    Posts:
    19
    Yes, but will it work after in runtime?
    I thought that all what is in the Editor folder is working only in editor.
     
  14. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    It's a script for changing how the editor imports assets. That only happens in the editor. The assets won't need to be imported again when you build the game.
     
  15. Homeak16

    Homeak16

    Joined:
    Feb 27, 2017
    Posts:
    19
    Sorry for long answer.
    I didn't say that i am developing a game.
    It's a system where you can import and edit a 3dModel.
    That's why i'm searching how to import 3d Model in runtime.
     
  16. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Ah. That's not supported by default in Unity. All the scripts in this thread works with Unity's built-in editor only imports.

    If you want to import 3d models at runtime, there's a bunch of third party assets that does that in the Asset store.
     
    Homeak16 likes this.
  17. kideternal

    kideternal

    Joined:
    Nov 20, 2014
    Posts:
    82
    Thanks, Aubergine, for being such an asset to the forums! (Several other posts have helped me in the past.)

    I've been frustrated for years with the long wait on large model imports, with the subsequent ticking of boxes, waiting, switching tabs, applying, waiting some more, etc. I looked into a solution a while back but didn't find anything, until this post. (The missing magic was "AssetPostprocessor".)

    I've thrown together a script for model imports that works for 99% of my needs. Thought I'd share it:
    Code (CSharp):
    1. using UnityEditor;
    2.  
    3. public class ModelImport : AssetPostprocessor {
    4.     void OnPreprocessModel() {
    5.         ModelImporter importer = assetImporter as ModelImporter;
    6.         string name = importer.assetPath.ToLower();
    7.         string extension = name.Substring(name.LastIndexOf(".")).ToLower();
    8.         switch (extension) {
    9.             case ".3ds":
    10.             case ".fbx":
    11.             case ".blend":
    12.                 importer.globalScale = 1.0F;
    13.                 importer.meshCompression = ModelImporterMeshCompression.Medium;
    14.                 importer.optimizeMesh = true;
    15.                 importer.importBlendShapes = false;
    16.                 importer.addCollider = false;
    17.                 importer.weldVertices = true;
    18.                 importer.importVisibility = false;
    19.                 importer.importCameras = false;
    20.                 importer.importLights = false;
    21.  
    22.                 importer.importNormals = ModelImporterNormals.Import;
    23.                 importer.importTangents = ModelImporterTangents.CalculateMikk;
    24.  
    25.                 importer.importAnimation = false;
    26.                 importer.animationType = ModelImporterAnimationType.None;
    27.                 importer.generateAnimations = ModelImporterGenerateAnimations.None;
    28.  
    29.                 importer.importMaterials = true;
    30.                 importer.materialLocation = ModelImporterMaterialLocation.External;
    31.                 importer.materialName = ModelImporterMaterialName.BasedOnMaterialName;
    32.                 importer.materialSearch = ModelImporterMaterialSearch.Everywhere;
    33.                 break;
    34.             default:
    35.                 break;
    36.         }
    37.     }
    38. }
     
    altair2020, Ony, Gundio and 2 others like this.
  18. Gundio

    Gundio

    Joined:
    Jul 17, 2017
    Posts:
    9
    Thanks you!
     
    kideternal likes this.
  19. kideternal

    kideternal

    Joined:
    Nov 20, 2014
    Posts:
    82
    The above code executes for every model imported, which can be a pain if you have a model that needs something different. I experimented with various ways of activating/deactiving the script, but the best workaround was to simply run via menu command. Since Unity persists the settings after import, you only have to do it once (per model, after initial import).

    I also added the ability to crunch textures, which is essentially worth doing on every texture you import. (Crunching takes a fair amount of time whenever you make a change, so I typically just do this when I'm close to releasing a project.)

    So, with the following script in your Editor folder, just right-click the model(s) and/or texture(s) you want to import with defined values, select the shortcut menuitem "Reimport with Defaults", and go grab yourself a tasty beverage:
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public class ReimportWithDefaults : ScriptableObject {
    5.     [MenuItem("Assets/Reimport with Defaults")]
    6.     static void ReimportWithDefaultsMenu() {
    7.         foreach (string guid in Selection.assetGUIDs) {
    8.             string path = AssetDatabase.GUIDToAssetPath(guid);
    9.             AssetImporter assetImporter = AssetImporter.GetAtPath(path);
    10.             Debug.Log(assetImporter.GetType());
    11.             if (assetImporter is ModelImporter) {
    12.                 ModelImporter importer = AssetImporter.GetAtPath(path) as ModelImporter;
    13.                 importer.useFileScale = false;
    14.                 importer.meshCompression = ModelImporterMeshCompression.Medium;
    15.                 importer.isReadable = false;
    16.                 importer.optimizeMesh = true;
    17.                 importer.importBlendShapes = false;
    18.                 importer.addCollider = false;
    19.                 importer.weldVertices = true;
    20.                 importer.importVisibility = false;
    21.                 importer.importCameras = false;
    22.                 importer.importLights = false;
    23.  
    24.                 importer.importNormals = ModelImporterNormals.Import;
    25.                 importer.importTangents = ModelImporterTangents.CalculateMikk;
    26.  
    27.                 importer.importAnimation = false;
    28.                 importer.animationType = ModelImporterAnimationType.None;
    29.                 importer.generateAnimations = ModelImporterGenerateAnimations.None;
    30.  
    31.                 importer.importMaterials = true;
    32.                 importer.materialLocation = ModelImporterMaterialLocation.External;
    33.                 importer.materialName = ModelImporterMaterialName.BasedOnMaterialName;
    34.                 importer.materialSearch = ModelImporterMaterialSearch.Everywhere;
    35.                 importer.SaveAndReimport();
    36.             } else if (assetImporter is TextureImporter) {
    37.                 TextureImporter importer = assetImporter as TextureImporter;
    38.                 importer.crunchedCompression = true;
    39.                 importer.compressionQuality = 75;
    40.                 importer.anisoLevel = 2;
    41.                 importer.SaveAndReimport();
    42.             }
    43.         }
    44.     }
    45. }
    The defaults are values I like in most of my projects. YMMV.
     

    Attached Files:

    Last edited: Mar 17, 2018
    Ony and Gundio like this.
  20. Gundio

    Gundio

    Joined:
    Jul 17, 2017
    Posts:
    9
    Look through code of @kideternal, so i can make my own code to post processing every image i want force Enable Read/Write when import to folder i want. Just share this! :)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class ImagePostProcress : AssetPostprocessor
    7. {
    8.     void OnPostprocessTexture(Texture2D texture)
    9.     {
    10.         TextureImporter importer = assetImporter as TextureImporter;
    11.         string assetPath = importer.assetPath;
    12.  
    13.         if (!IsResourceImage(assetPath))
    14.             return;
    15.  
    16.         importer.isReadable = true;
    17.         Debug.Log("Set Readable for image: " + assetPath);
    18.     }
    19.  
    20.     private bool IsResourceImage(string path)
    21.     {
    22.         if (path.Contains("Resources/Textures/ImagePacks"))
    23.             return true;
    24.  
    25.         return false;
    26.     }
    27. }
     

    Attached Files:

    ikazrima and kideternal like this.
  21. Scott-Steffes

    Scott-Steffes

    Joined:
    Dec 31, 2013
    Posts:
    59
    As of 2018.1 you can now specify default import settings for all types of assets using the Unity preset system:
     
    Captaingerbear, granit and christh like this.
  22. Captaingerbear

    Captaingerbear

    Joined:
    Mar 6, 2013
    Posts:
    57
    Baller. This is exactly what I need, and will save me gads of time mucking around with pixel art import settings.