Editor class "Texture Importer" + Apply import settings question

I created an editor script that helps me change the settings on textures automatically so that I don't have to. It works for the most part except for changing the texture importer isReadable = true. If the texture isn't readable, a dialog box pops open saying "Unapplied import settings" and then asks me to Apply or Revert. Even when I press "Apply", I double-check the texture and isReadable is still set to false. Is there any code that will allow me to "Apply" the import settings?

Here's a snippet of my code (I'm using C#):

string path = AssetDatabase.GetAssetPath(texture);
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

if (textureImporter.isReadable == false)
{
   textureImporter.isReadable = true;
   AssetDatabase.ImportAsset(path);
}

I've also been trying to get this working, and have found a few things. This is the bare minimum code you need to get it working, and also it has to be in this order - if you change the filtering/anis/clamp modes, and -then- change the importer settings, it loses the previous changes. This is as of Unity 3.1, tested and confirmed on a Win7 PC.

        TextureImporter tImporter = AssetImporter.GetAtPath( path ) as TextureImporter;
        if( tImporter != null ) {
            tImporter.mipmapEnabled = ...;
            tImporter.isReadable = ...;
            tImporter.maxTextureSize = ...;
            AssetDatabase.ImportAsset( path, ImportAssetOptions.ForceUpdate );                
        }

        UnityEngine.Object t = AssetDatabase.LoadAssetAtPath( path, typeof( T ) );
        if( t != null ) {
            ( (Texture2D)t ).filterMode = ...;
            ( (Texture2D)t ).wrapMode = ...;
        }

I hope that helps :)

You should specify ImportAssetOptions.ForceUpdate when calling ImportAsset, in your case:

AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);

You'll have more luck when trying a AssetPreProcessor instead. (In fact, I didn't even know you could do what your example shows. Learn something new every day :) )

Here's a link to the help which includes an example usage:

http://unity3d.com/support/documentation/ScriptReference/AssetPostprocessor.OnPostprocessGameObjectWithUserProperties.html

I have the same issue by using the texture import settings from Unity Community wiki. If I click apply on the popup, then the current active selection texture object won’t get update. Though I didn’t find a perfect solution, but if you click revert on the popup, everything works fine.

I am reproducing the same issue when using TextureImporter.SetPlatformTextureSettings().

My editor script works fine (and updates the platform override settings) only if the texture in question is NOT selected.

I notice that the texture settings script from the wiki sets Selection.objects to an empty Object array before it operates on textures. I tried using the same approach in my script, but it throws an InvalidCastException.

It might be worth mentioning that (for some silly reason) I’m using javascript…

Old topic, but I found the best way to deal with this Inspector bug (still running in 3.5…).

Just add this to the end of your processing routine :

Selection.objects = new Object[0];

this will force the inspector to unselect anything, and avoid the “Apply” override mentionned here.