- Home /
Changing Texture import default settings.
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?
Answer by unitylove · Mar 30, 2011 at 02:19 PM
It seems I found a solution to my problem:
using UnityEngine;
using UnityEditor;
public class TexturePostProcessor : AssetPostprocessor
{
void OnPostprocessTexture(Texture2D texture)
{
TextureImporter importer = assetImporter as TextureImporter;
importer.anisoLevel = 0;
importer.filterMode = FilterMode.Trilinear;
Object asset = AssetDatabase.LoadAssetAtPath(importer.assetPath, typeof(Texture2D));
if (asset)
{
EditorUtility.SetDirty(asset);
}
else
{
texture.anisoLevel = 0;
texture.filterMode = FilterMode.Trilinear;
}
}
}
can you explain what you did to solve it ?
or add some comments in your script example ?
Thanks!
It seems that those lines manage the difference between editing the file after import and the first time it's imported
Object asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Texture2D));
if (asset != null){ editing in editor (like rena$$anonymous$$g) }
else { on first import }
Answer by Jessy · Mar 29, 2011 at 03:04 PM
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; }
}
When I use your code, I can no longer change these parameters manually (via Inspector) after importing.
Restart Unity. The code that you had before causes invisible persistent bugginess.
Restarting Unity did NOT solve the issue. Whenever I drag the "Aniso Level" slider (or save the scene), OnPostprocessTexture() will be called, resetting the values.
That was the problem for me. I only have the problem when I use your script, or after I use your script, then edit the script, and save, sometimes.
$$anonymous$$ulti-part comment here, because of the character limit:
I have upgraded to Unity 3.3 and I am experiencing a different behaviour with your script now: When I (re)import a texture or drag the Aniso slider, I get the following error message:
Answer by Andrey-Postelzhuk · Jul 15, 2014 at 03:00 PM
This is solution for me:
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
}
}
Answer by wkwan · May 20, 2015 at 08:05 PM
If you want to set defaults for your textures without writing postprocessor code, I made a simple asset that does just this:
Your answer
Follow this Question
Related Questions
How to add a specific object from one array to another ? 1 Answer
Instantiate prefab in the editor 1 Answer
After AssetPostprocessor meshes seem to be gone. 2 Answers
AssetDataBase.ImportAsset not triggering AssetProcessor.PreProcessModel 0 Answers
Is there a hook for when you update files from the asset server? 0 Answers