'Converting RenderTexture to Texture2D in Unity 2019

I'm using Intel Real Sense as camera device to capture picture. The capture result is displayed as a RenderTexture. Since I need to sent it via UDP, I need to convert it to byte[], but it only work for Texture2D. Is it possible to convert RenderTexture into Texture2D in unity 2019?

Edit: Right now, I'm using this code to convert RenderTexture to Texture2D:

Texture2D toTexture2D(RenderTexture rTex)
{
    Texture2D tex = new Texture2D(rTex.width, rTex.width, TextureFormat.ARGB32, false);
    RenderTexture.active = rTex;
    tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
    tex.Apply();

    return tex;
}

I got this code from here, which doesn't work anymore for unity 2019 since if I display the texture it only give me white texture.

Edit 2: Here how i called that function:

//sender side
Texture2D WebCam;
public RawImage WebCamSender;
public RenderTexture tex;
Texture2D CurrentTexture;

//receiver side
public RawImage WebCamReceiver;
Texture2D Textur;
IEnumerator InitAndWaitForWebCamTexture()
{

    WebCamSender.texture = tex;
    CurrentTexture = new Texture2D(WebCamSender.texture.width, 
    WebCamSender.texture.height, TextureFormat.RGB24, false, false);
    WebCam = toTexture2D(tex);

    while (WebCamSender.texture.width < 100) //WebCam
    {
        yield return null;
    }

    StartCoroutine(SendUdpPacketVideo());
}

then i'll send it via network like this :

IEnumerator SendUdpPacketVideo()
{
        ...
        CurrentTexture.SetPixels(WebCam.GetPixels());
        byte[] PNGBytes = CurrentTexture.EncodeToPNG();
        ...
}

On receiver side, i'm gonna decode it and display on raw image:

....
Textur.LoadImage(ReceivedVideo);
WebCamReceiver.texture = Textur;
...


Solution 1:[1]

The most optimized way to do this is:

public Texture2D toTexture2D(RenderTexture rTex)
{
    Texture2D dest = new Texture2D(rTex.width, rTex.height, TextureFormat.RGBA32, false);
    dest.Apply(false);
    Graphics.CopyTexture(rTex, dest);
    return dest;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Adam B