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. Dismiss Notice

Resolved Change object import settings via code

Discussion in 'Asset Importing & Exporting' started by TeamDefiant, Jun 21, 2021.

  1. TeamDefiant

    TeamDefiant

    Joined:
    Mar 29, 2017
    Posts:
    50
    Is there a way to view/edit an objects import settings via code?
    upload_2021-6-21_14-7-17.png
    Specifically, these settings?

    I'd like to create a validation tool which scans all models in the project and corrects these settings. (I.E. disables Camera and Light importing.) Scanning through the models is the easy part, extracting their import settings is not so easy. :)

    (I know I can create a tool to remove these once the model has been imported, but I'd rather not import them to start with as the next time the object is updated outside of unity, the cameras/lights etc will be back and will need to be removed again. Fixing it properly once is more efficient, especially if your projects contains hundreds of models.)
     
  2. TeamDefiant

    TeamDefiant

    Joined:
    Mar 29, 2017
    Posts:
    50
    Solved it. (This is of course an Editor Script, so don't forget that!)

    Code (CSharp):
    1. string[] assetPaths = AssetDatabase.GetAllAssetPaths();
    2. foreach (string assetPath in assetPaths)
    3. {
    4.     System.Type assetType = AssetDatabase.GetMainAssetTypeAtPath(assetPath);
    5.  
    6.     if (assetType != null)
    7.     {
    8.         if (!assetPath.Contains(".prefab") && assetType.ToString().Contains("GameObject")) // Filter out prefabs and leave only models
    9.         {
    10.             ModelImporter importer = ModelImporter.GetAtPath(assetPath) as ModelImporter;
    11.  
    12.             if( importer.importLights ||
    13.                 importer.importCameras )
    14.             {
    15.                 // Do something
    16.             }
    17.         }
    18.     }
    19. }
    20.  
    This scans the project for all 3D models, then checks if they have the import lights or cameras flag. You can then do whatever you want in the //Do Something block.
    You can use this code to see what else is inside the ModelImporter class to twerk it you your own needs. this does what I need it to.