Create new Texture2D
, use RenderTexture.ReadPixels
to read the pixels from RenderTexture
into the new Texture2D
. Finally, Call Texture2D.Apply();
to apply the changed pixels.
Texture2D toTexture2D(RenderTexture rTex)
{
Texture2D tex = new Texture2D(512, 512, TextureFormat.RGB24, false);
// ReadPixels looks at the active RenderTexture.
RenderTexture.active = rTex;
tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
tex.Apply();
return tex;
}
Usage:
public RenderTexture tex;
Texture2D myTexture = toTexture2D(tex);
You can make it an extension method (restore the previous active RenderTexture to avoid surprises):
public static class ExtensionMethod
{
public static Texture2D toTexture2D(this RenderTexture rTex)
{
Texture2D tex = new Texture2D(rTex.width, rTex.height, TextureFormat.RGB24, false);
var old_rt = RenderTexture.active;
RenderTexture.active = rTex;
tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
tex.Apply();
RenderTexture.active = old_rt;
return tex;
}
}
Usage:
public RenderTexture tex;
Texture2D myTexture = tex.toTexture2D();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…