Copy Texture2D to leave original unchanged

Viewed 6985

How to I properly copy a Texture2D in Unity, I've tried using Graphics.CopyTexture(texture, copy); to have it tell me that the two images are incompatible, how do I work around this and get a perfect copy of the image, Everything I've looked up either relates to how to crop the copied image or when using RenderTextures but I can't find any information about how to create a copy Texture2D that is identical, in size and pixel colors.

1 Answers

Try using the constructor of the Texture2D class, and the methods SetPixels and Apply.

Texture2D copyTexture = new Texture2D(originalTexture.width, originalTexture.height);
copyTexture.SetPixels(originalTexture.GetPixels());
copyTexture.Apply();

You may encounter an error:

UnityException: Texture 'YourTexture' is not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.

And as it says. You solve it by clicking your texture in the project tab to open the Import Settings in the Inspector and check the Read/Write Enabled checkbox.

Related