user3915050
user3915050

Reputation:

Copy Texture2D to leave original unchanged

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.

Upvotes: 2

Views: 14763

Answers (2)

Jung
Jung

Reputation: 1

Try this:

texture = (Texture2D)SpecMat.GetTexture(name);
                    Texture2D  textureClone = new Texture2D(texture.width, texture.height, texture.format, false);
                    textureClone.LoadRawTextureData(texture.GetRawTextureData());
                    textureClone.Apply();

Upvotes: 0

Marco Elizondo
Marco Elizondo

Reputation: 562

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.

Upvotes: 6

Related Questions