Every texture that gets imported by Unity defaults to "Filter Mode: Bilinear" and "Aniso Level: 1". I would like to change these defaults to Trilinear and Aniso 0. So I created the following Editor class:
using UnityEngine;
using UnityEditor;
public class TexturePostprocessor : AssetPostprocessor
{
public void OnPostprocessTexture(Texture2D t)
{
t.anisoLevel = 0;
t.filterMode = FilterMode.Trilinear;
}
}
Which does work. However I would like to change these values for individual textures manually in the editor after importing. When I do that and then save the scene, OnPostprocessTexture is being called again and resets the manually set values, which is undesirable for obvious reasons.
The documentation says "AssetPostprocessor lets you hook into the import pipeline and run scripts prior or after importing assets.". Why does saving the scene cause a reimport? How can I solve this problem?
You've got to change the settings for the importer, not the texture itself. Changing texture settings is for runtime use.
using UnityEngine;
using UnityEditor;
public class TexturePostProcessor : AssetPostprocessor {
void OnPostprocessTexture () {
var importer = assetImporter as TextureImporter;
importer.anisoLevel = 0;
importer.filterMode = FilterMode.Trilinear;
}
}
public class TexturePostProcessor : AssetPostprocessor
{
void OnPreprocessTexture()
{
Object asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Texture2D));
if (asset)
return; //set default values only for new textures;
TextureImporter importer = assetImporter as TextureImporter;
//set your default settings to the importer here
}
}