复制Texture2D以保持原始不变

2 c# unity-game-engine

如何在Unity中正确复制Texture2D,我尝试让它Graphics.CopyTexture(texture, copy);告诉我这两个图像不兼容,我如何解决这个问题并获得图像的完美副本,我查找的所有内容都与相关如何裁剪复制的图像或使用 RenderTextures 时,但我找不到任何有关如何创建大小和像素颜色相同的纹理2D 副本的信息。

小智 6

尝试使用Texture2D类的构造函数以及方法SetPixelsApply

Texture2D copyTexture = new Texture2D(originalTexture.width, originalTexture.height);
copyTexture.SetPixels(originalTexture.GetPixels());
copyTexture.Apply();
Run Code Online (Sandbox Code Playgroud)

您可能会遇到错误:

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.
Run Code Online (Sandbox Code Playgroud)

正如它所说。您可以通过单击项目选项卡中的纹理以打开检查器中的导入设置并选中复选框来解决此问题Read/Write Enabled