- Home /
How to get the pixels of a texture which is in memory
I make some texture into AssetBundle, and loaded it in memory. I want to patch all texture in assetBundle into one atlas. But can't get the pixels of each texture.
code like this:
assetBundleTexture.GetPixels32();
got error:
Texture 001 is not readable, the texture memory can not be accessed from scripts.
You can make the texture readable in the Texture Import Settings.
whether the Unity locked the memory block where the assetBundle used or not. and what should i do.
Thanks in advance!
Answer by erick_weil · Feb 15, 2014 at 02:12 AM
you need to set the texture as readable before GetPixels or SetPixels; see the unity script reference ( i use one simple google search ) https://docs.unity3d.com/Documentation/ScriptReference/TextureImporter-isReadable.html
sorry, i forgot mension. I had already set the source Texture file type to advance and set read/write enable on before. But it's still has this error.
try to copy the texture. like this : var newtexture : Texture2D = assetBundleTexture; newtexture.GetPixels32();
Thanks erick_weil. Finally, i achieve it via file operation. Here is another answer:
http://answers.unity3d.com/questions/639234/instantiating-texture2d-object-get-non-readable-er.html
it in the hope of Unity Team.
Answer by Pieter_Unit040 · Sep 26, 2016 at 04:44 PM
There is another way to bypass the read/write restriction;
basically its the operation you perform when rescaling a texture (but in this case you don't actually rescale - see http://gamedev.stackexchange.com/questions/92285/unity3d-resize-texture-without-corruption)
Perhaps the following code can be optimized, but it works:
Color32[] pixelBlock = null;
try
{
pixelBlock = img.GetPixels32();
}
catch (UnityException _e)
{
img.filterMode = FilterMode.Point;
RenderTexture rt = RenderTexture.GetTemporary(img.width, img.height);
rt.filterMode = FilterMode.Point;
RenderTexture.active = rt;
Graphics.Blit(img, rt);
Texture2D img2 = new Texture2D(img.width, img.height);
img2.ReadPixels(new Rect(0, 0, img.width, img.height), 0, 0);
img2.Apply();
RenderTexture.active = null;
img = img2;
pixelBlock = img.GetPixels32();
}
Very nice, I needed to do this with a mod for a Unity game and this helped being able to write the texture to disk. It makes a fully readable texture even, so it can also export to png, jpg and so on.
Answer by Nick_lzn · Feb 26, 2014 at 01:42 AM
Thanks very much erick_weil. Finally, i achieve it via file operation. Here is another answer:
http://answers.unity3d.com/questions/639234/instantiating-texture2d-object-get-non-readable-er.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613133543im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to reduce memory usage? 1 Answer
Texture3D stays black no matter what? 1 Answer
What is the best way to create a simple colored plane object in NGUI? 1 Answer
Fatal Error - Could not allocate memory 2 Answers
Hexagonal Grids 2 Answers