14

I have some AssetBundles that I want to convert to .png image files.

They are Texture2D assets, but the problem is as they are not Read Enable, when I try to convert them to PNG with a

var _bytes = _texture2d.EncodeToPNG();

command, I get the following error message:

Texture 'name of a texture' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.

I really can't access the Texture Import Settings, as they come from asset bundles and everything is made with code.

Somebody has a workaround for this?

Thanks

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
DavidGuaita
  • 501
  • 1
  • 7
  • 18
  • Could you make it readable with [this](http://docs.unity3d.com/ScriptReference/TextureImporter-isReadable.html)? – S.C. Aug 07 '14 at 07:12
  • Thanks Aldour, but I don't think so. Textureimporter works when you import a texture in the editor, but the images have been already converted to Texture2D objects and stored as AssetBundles, so they don't go through the Textureimporter anymore. – DavidGuaita Aug 07 '14 at 07:21

2 Answers2

22

Here's a working solution:

public static void SetTextureImporterFormat( Texture2D texture, bool isReadable)
{
    if ( null == texture ) return;

    string assetPath = AssetDatabase.GetAssetPath( texture );
    var tImporter = AssetImporter.GetAtPath( assetPath ) as TextureImporter;
    if ( tImporter != null )
    {
        tImporter.textureType = TextureImporterType.Advanced;

        tImporter.isReadable = isReadable;

        AssetDatabase.ImportAsset( assetPath );
        AssetDatabase.Refresh();
    }
}
Ilya Suzdalnitski
  • 52,598
  • 51
  • 134
  • 168
7

Select texture in project, open inspector window, set texture type to "Advanced", toggle "Read and write enabled".