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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Set resizeAlgorithm (Importing Textures)

Discussion in 'Scripting' started by Littlenorwegian, Jan 14, 2020.

  1. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    So, basically, I am trying to set it so my textures imports with bilinear rather than mitchell when it imports my textures.
    upload_2020-1-14_21-41-26.png

    But I am having some trouble with this.

    I looked up code the best I could and so far I have

    Code (CSharp):
    1. TextureImporter importer = assetImporter as TextureImporter;
    2.             importer.GetPlatformTextureSettings("Default").resizeAlgorithm = TextureResizeAlgorithm.Bilinear;
    But at the moment, this isn't working. What am I doing wrong and what is the right way to do it? :)
     
  2. Littlenorwegian

    Littlenorwegian

    Joined:
    Oct 19, 2012
    Posts:
    143
    Hey. Sorry to bump this but it's having quite the profound impact on my game\s visuals and it's very tedious to manually change every imported texture.

    Please help :)
     
  3. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi @Littlenorwegian,

    You can use AssetPostprocessor and then you need feed TextureImporterPlatformSettings to the TextureImporter's SetPlatformTextureSettings:

    This example I wrote changes those bottom region box settings, naturally you could adjust majority if not every parameter in those import settings at the same time (which are also easier to change...). I also set it so that it only applies this custom import if the folder where the texture is contains word "Sprite" (capitalized.)

    Code (CSharp):
    1. public class CustomImport : AssetPostprocessor
    2. {
    3.     // From my notes:
    4.     void OnPreprocessTexture() {
    5.         // Trigger only for asset path that contains word "Sprite"
    6.         if (assetPath.Contains("Sprite")) {
    7.             TextureImporter textureImporter = (TextureImporter) assetImporter;
    8.             Debug.Log("Processing texture " + assetImporter.assetPath);
    9.  
    10.             textureImporter.SetPlatformTextureSettings( new TextureImporterPlatformSettings {
    11.                 maxTextureSize = 32,
    12.                 resizeAlgorithm = TextureResizeAlgorithm.Mitchell,
    13.                 format = TextureImporterFormat.Automatic,
    14.                 textureCompression = TextureImporterCompression.CompressedHQ,
    15.                 crunchedCompression = true,
    16.                 compressionQuality = 75
    17.             });
    18.  
    19.             textureImporter.SetPlatformTextureSettings( new TextureImporterPlatformSettings {
    20.                 name = "Standalone",
    21.                 overridden = true,
    22.                 maxTextureSize = 32,
    23.                 resizeAlgorithm = TextureResizeAlgorithm.Mitchell,
    24.                 format = TextureImporterFormat.RGBA32
    25.             });
    26.         }
    27.     }
    28. }
    Important - put this to an Editor folder (anywhere is fine, just make a folder under your Scripts for example.
    Otherwise the callback OnPreprocessTexture won't fire.

    If you want to change those basic settings like texture type, just do it like this:

    Code (CSharp):
    1. // Sprite settings
    2. textureImporter.textureType = TextureImporterType.Sprite;
    3.  
    4. textureImporter.spriteImportMode = SpriteImportMode.Single;
    5. ...
     
    Ismaeel370 and adunster like this.