Making a Texture2D readable in Unity via code [duplicate]
Asked Answered
A

2

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

Arriaga answered 7/8, 2014 at 6:48 Comment(2)
Could you make it readable with this?Periodical
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.Arriaga
T
7

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

Tubb answered 7/8, 2014 at 7:28 Comment(8)
I'm afraid that's not possible, the textures are not in the project at all, I only have Assetbundles that have been created from Texture2D objects, so I can't make any edition in the Editor. The utility I'm making loads those Assetbundles in order to export again the images, so we can edit them in Photoshop.Arriaga
You still can render unreadable texture to RenderTexture and then read it.Tubb
Good point, game development germ. I will try that workaround, thanks.Arriaga
This is not a valid case for textures made with new Texture2D(...). How can I read such textures, or I should work with Color[] before setting the pixels?Clownery
Textures created in runtime must be readable/writeable.Tubb
I am struggling with this as well. How would you render an unreadable texture to RenderTexture?Witcher
@Arriaga What if the texture is loaded from the server using DownloadHandlerTexture.GetContentAllsopp
I think the question is "Making a Texture2D readable in Unity via code" not "via Unity Inspector"Farny
O
23

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();
    }
}
Otherworld answered 10/3, 2015 at 6:11 Comment(4)
As this solution relies on AssetDatabase (which is in the UnityEditor namespace) this can only be used in the editor. It fails when building, though (The type or namespace name `UnityEditor' could not be found.)Worthen
sorry but this will only work in the EditorIhram
@Worthen this script has to be placed in "Editor" directory.Otherworld
One potential problem, after running this code the gameobject reference to this texture will throw a nullreferenceexception.Debose
T
7

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

Tubb answered 7/8, 2014 at 7:28 Comment(8)
I'm afraid that's not possible, the textures are not in the project at all, I only have Assetbundles that have been created from Texture2D objects, so I can't make any edition in the Editor. The utility I'm making loads those Assetbundles in order to export again the images, so we can edit them in Photoshop.Arriaga
You still can render unreadable texture to RenderTexture and then read it.Tubb
Good point, game development germ. I will try that workaround, thanks.Arriaga
This is not a valid case for textures made with new Texture2D(...). How can I read such textures, or I should work with Color[] before setting the pixels?Clownery
Textures created in runtime must be readable/writeable.Tubb
I am struggling with this as well. How would you render an unreadable texture to RenderTexture?Witcher
@Arriaga What if the texture is loaded from the server using DownloadHandlerTexture.GetContentAllsopp
I think the question is "Making a Texture2D readable in Unity via code" not "via Unity Inspector"Farny

© 2022 - 2024 — McMap. All rights reserved.