今天做项目时遇见一个问题,就是texture在内存中,无论怎么新建都指向同一个内存地址,只能新建一个texture来中断它,但是发现无论怎么新建 都无济于事,外网找了一圈有所收获,特意分享出来
public void SaveScreenShot()
{
Texture2D tex = new Texture2D(1920,1080,TextureFormat.RGBA32,false);
Texture2D texture2D = TextureToTexture2D(transform.Find("paintCanvasImg").GetComponent<RawImage>().texture);
tex.SetPixels(texture2D.GetPixels());
tex.Apply();
UIController.instance.autograph.texture = tex;
}
private Texture2D TextureToTexture2D(Texture texture)
{
Texture2D texture2D = new Texture2D(texture.width, texture.height, TextureFormat.RGBA32, false);
RenderTexture currentRT = RenderTexture.active;
RenderTexture renderTexture = RenderTexture.GetTemporary(texture.width, texture.height, 32);
Graphics.Blit(texture, renderTexture);
RenderTexture.active = renderTexture;
texture2D.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
texture2D.Apply();
RenderTexture.active = currentRT;
RenderTexture.ReleaseTemporary(renderTexture);
return texture2D;
}