复制纹理 2D 以保持原始不变



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

尝试使用 Texture2D 类的构造函数,以及方法 SetPixel 和 App。

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复选框来解决它。

试试这个:

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

最新更新