- Home /
convert Texture to Texture2D
Hello guys, I want to save a custom painted texture of car in device storage (android/IOS) but my painted texture is of Texture type, now I need to convert it to Texture2D to have a byte format to save it into device. Searched many demos and examples but none works. Thanks!
Answer by Vincent1236 · Feb 19, 2018 at 02:01 PM
After some digging I found another link which explains your situation, see if this helps you. Copying it here for future sake (Credit To:BBIT-SOLUTIONS):
Texture mainTexture = renderer.material.mainTexture;
Texture2D texture2D = new Texture2D(mainTexture.width, mainTexture.height, TextureFormat.RGBA32, false);
RenderTexture currentRT = RenderTexture.active;
RenderTexture renderTexture = new RenderTexture(mainTexture.width, mainTexture.height, 32);
Graphics.Blit(mainTexture, renderTexture);
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture2D.Apply();
Color[] pixels = texture2D.GetPixels();
RenderTexture.active = currentRT;
Hmmm. I'm totally unaware of Graphics.Blit()
. I should try it! Thank you again!
Answer by Vince_TenebrisLab · Feb 19, 2018 at 01:18 PM
You can simply cast your Texture to a Texture2D. Like So:
Texture2D tex2D = (Texture2D)PaintedTexture;
Gives an error "InvalidCastException: Cannot cast from source type to destination type." Even PaintedTexture as Texture2D
doesn't work.
You can definitely convert a Texture to Texture2D as shown above. Are you sure PaintedTexture is of type Texture? Some code from you might help
Actually, no. You can't always cast a Texture to a Texture2D. You can only do that if the texture is actually a Texture2D. Texture is the base class for all texture types. Currently there are only two texture types: Texture2D and RenderTexture. If the texture you want to cast to Texture2d is a RenderTexture it will fail.
Texture2D has a representation on the GPU and might also have one on the CPU side. RenderTextures are pure GPU textures. If you want to "convert" a RenderTexture to a Texture2D you actually have to copy the content like Vincent1236 showed in his answer.
In 2018.3.11, it does work on the Editor but however it gives the following error. InvalidCastException: Specified cast is not valid.
Answer by Max-Bot · Jan 07, 2019 at 04:15 PM
Unity 2018+
public static class TextureExtentions
{
public static Texture2D ToTexture2D(this Texture texture)
{
return Texture2D.CreateExternalTexture(
texture.width,
texture.height,
TextureFormat.RGB24,
false, false,
texture.GetNativeTexturePtr());
}
}
Usage:
Texture2D texture2D = your_texture.ToTexture2D();
Worth noting - Unity will die if you use this code on anything runtime generated. Only use this if you imported the asset into Unity Editor.
Your answer
Follow this Question
Related Questions
Create .png file from script, then import as asset 1 Answer
Texture to Texture2D 2 Answers
SetPixels32 called with invalid number if pixels in the array 0 Answers
Convert Texture2D to array of uint and back 2 Answers
Assigning script-generated textures 0 Answers