Search Unity

Can not set texture filter mode

Discussion in 'Scripting' started by Graham-B, Feb 10, 2016.

  1. Graham-B

    Graham-B

    Joined:
    Feb 27, 2013
    Posts:
    331
    I am simply trying to set a texture's filter mode via editor script. Any idea why this isn't working?

    Any advice is much appreciated!

    Code (CSharp):
    1.  Texture2D test = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/test.png", typeof(Texture2D));
    2. test.filterMode = FilterMode.Point;
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Don't forget to save the asset and refresh the inspector, might be changing and you just don't see it.
     
  3. Graham-B

    Graham-B

    Joined:
    Feb 27, 2013
    Posts:
    331
    Thank you hpjohn. I have saved using AssetDatabase.SaveAssets();
    Still does not work, I can't for the life of me figure this one out.

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public class TextureProperties
    5. {
    6.     // Add a menu item named "Do Something" to MyMenu in the menu bar.
    7.     [MenuItem("MyMenu/Do Something")]
    8.     static void DoSomething()
    9.     {
    10.         Texture2D test = (Texture2D)AssetDatabase.LoadAssetAtPath("Assets/test.png", typeof(Texture2D));
    11.         test.filterMode = FilterMode.Point;
    12.         AssetDatabase.SaveAssets();
    13.     }
    14. }
    15.  
     
    Last edited: Feb 11, 2016
  4. Graham-B

    Graham-B

    Joined:
    Feb 27, 2013
    Posts:
    331
    I decided to go with re-importing the texture with new settings, this worked for what I needed.

    Code (CSharp):
    1. TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath("Assets/test.png");
    2. importer.filterMode = FilterMode.Point;
    3. importer.textureFormat = TextureImporterFormat.AutomaticTruecolor;
     
  5. Dunkelheit

    Dunkelheit

    Joined:
    Sep 3, 2013
    Posts:
    81
    Use:

    Code (csharp):
    1.  
    2. importer.textureCompression = TextureImporterCompression.Uncompressed;
    3.  
    instead of this old method because it is deprecated. Uncompressed mode is the same as old TrueColor.