Search Unity

Showcase Simple guide to change Default Sprite Import Settings (FilterMode, Compression, PPU) via script

Discussion in '2D' started by Brian-Santos, Mar 25, 2021.

  1. Brian-Santos

    Brian-Santos

    Joined:
    Apr 10, 2014
    Posts:
    6
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. //Throw this inside YourGame/Assets/Editor/Importer.cs
    5. public class Importer: AssetPostprocessor {
    6.  
    7.     const int PostProcessOrder = 0;
    8.  
    9.     public override int GetPostprocessOrder () {
    10.         return PostProcessOrder;
    11.     }
    12.  
    13.     void OnPreprocessTexture () {
    14.         var textureImporter = assetImporter as TextureImporter;
    15.  
    16.         textureImporter.filterMode = FilterMode.Point;
    17.         //textureImporter.filterMode = FilterMode.Bilinear;
    18.         //textureImporter.filterMode = FilterMode.Trilinear;
    19.  
    20.         textureImporter.textureCompression = TextureImporterCompression.Uncompressed;
    21.         //textureImporter.textureCompression = TextureImporterCompression.Compressed;
    22.         //textureImporter.textureCompression = TextureImporterCompression.CompressedLQ;
    23.         //textureImporter.textureCompression = TextureImporterCompression.CompressedHQ;
    24.    
    25.         //textureImporter.spritePixelsPerUnit = 16;
    26.         textureImporter.spritePixelsPerUnit = 32;
    27.         //textureImporter.spritePixelsPerUnit = 64;
    28.     }
    29. }
    Note: this can also be done via presets if you somehow know how to do it.

    This code will make every sprite image you import default to Point Filter, No Compression and 32 pixels per unit. Original code author is anonymous, and I added Uncompressed and PixelsPerUnity settings to it to make it more flexible.

    I know I didn't invent the wheel or anything since I'm no professional, but finding this code took me way longer than it should, and since I'm happy that I did, I decided to also share. I found it extremely annoying to have to dig through dozens of old threads with outdated methods to change default importer settings, so once I found a working method, I added a few more settings to it to tweak it for pixel art and I am sharing it here for ease of access and spreading the knowledge.

    Usage:
    - Create an Editor folder inside your unity project
    - Create an Importer.cs class inside the folder and paste the code above
    - After saving, all imported sprites should follow your settings.

    Tested in Unity 2021.1.0f1. Should work most older versions too.
     
    Last edited: Mar 25, 2021
  2. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    theres already a feature that does this - Import Preset, you can create multiple presets and select which one is active
     
  3. Brian-Santos

    Brian-Santos

    Joined:
    Apr 10, 2014
    Posts:
    6
    I didn't find any tutorials on that that I liked/understood yet, but that's a good point. I'll add it as a note about another way to do it on the main post.
    Finding that code was easier than finding a decent preset tutorial and they look confusing.
     
  4. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    its very simple you just press this button and its done

    Untitled.png
     
  5. Brian-Santos

    Brian-Santos

    Joined:
    Apr 10, 2014
    Posts:
    6
    Oh, that sounds simple enough. I feel like I've been outsmarted if it just works magically like that, thanks.
     
  6. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    its easy to overlook these small buttons