首页 > 解决方案 > 复制 Texture2D 以保持原始不变

问题描述

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

标签: c#unity3d

解决方案


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

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

您可能会遇到错误:

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.

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


推荐阅读