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!

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;

You can simply cast your Texture to a Texture2D. Like So:

Texture2D tex2D = (Texture2D)PaintedTexture;

I know it’s an old thread, but hear me out! You came here probably because you tried to do the following trick:

//
Texture2D tex2D = (Texture2D)tex; // where tex is of type 'Texture'
//

And the above won’t work. However, the following WILL WORK:

//
Texture2D tex2D = (tex as Texture2D); // where tex is of type 'Texture'
//

Hope this helps!

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();

Unity 2020 crash

Since this took me hours to figure out. TextureFormat might need to change depending on your computer.

If your texture source is PreviewRenderUtility’s EndPreview function, please note the return Texture you get is actually a RenderTexture, so just cast it instead of creating a new one.

private static Texture2D RenderTextureToTexture2D(RenderTexture texture)
{
    var oldRen = RenderTexture.active;
    RenderTexture.active = texture;
    var texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA64, false, false);
    texture2D.ReadPixels(new Rect(0, 0, texture.width, texture.height), 0, 0);
    texture2D.Apply();
    RenderTexture.active = oldRen;
    return texture2D;
}

    private static void CreateImageFiles(Texture2D tex, int index)
    {
        var arrayData = tex.EncodeToJPG();
        if (arrayData .Length < 1)
        {
            return;
        }
        var path = @"EnterPathHere" + index + ".jpg";
        File.WriteAllBytes(path, arrayData );
        AssetDatabase.Refresh();
    }