【发布时间】:2022-03-27 03:08:46
【问题描述】:
我似乎无法让 EncodeToPNG() 保存到 512 x 512 以外的文件尺寸,即使我的纹理是 1280 x 1024,我是从我的 RenderTexture 对象“tex”的尺寸中提取的。我错过了什么?谢谢!
// Saves texture as PNG file.
using UnityEngine;
using System.Collections;
using System.IO;
public class SaveTexture : MonoBehaviour {
public RenderTexture tex;
int tWidth, tHeight;
int getTextureWidth(int texWidth)
{
return tex.width;
}
int getTextureHeight(int texHeight)
{
return tex.height;
}
public void Start()
{
tWidth = getTextureWidth(tex.width);
tHeight = getTextureHeight(tex.height);
Debug.Log("Texture Width: " + tWidth + ", Texture Height: " + tHeight);
}
Texture2D toTexture2D(RenderTexture rTex)
{
Texture2D tex = new Texture2D(tWidth, tHeight, TextureFormat.ARGB32, false);
RenderTexture.active = rTex;
tex.ReadPixels(new Rect(0, 0, tWidth, tHeight), 0, 0);
tex.Apply();
return tex;
}
// Save Texture as PNG
public void SaveTexturePNG()
{
Texture2D myTexture = tex.toTexture2D();
// Encode texture into PNG
byte[] bytes = myTexture.EncodeToPNG();
Object.Destroy(myTexture);
// For testing purposes, also write to a file in the project folder
File.WriteAllBytes(Application.dataPath + "/../AnimalTexture/AnimalTexture.png", bytes);
}
}
【问题讨论】:
-
我很好奇你是否使用 Unity 5.6,我自己在从渲染纹理中读取像素时遇到了问题(我得到了扭曲的颜色和拉伸的图像),但我很笨拙将数据返回到屏幕,而不是保存它。但是如果 readpixels 是 强制一个 512x512 结果将解释我的问题的 一半(拉伸)。
-
我使用的是 5.6。您在早期版本的 Unity 中是否遇到过同样的问题?
-
有趣。 5.4是我家里的,它工作得很好。我已经为我遇到的问题提交了错误报告,但如果这是相关的......嗯......
-
刚刚进行了屏幕截图检查:我的壁球恰好是(左侧)一半的屏幕拉伸填充,而不是 512x512 区域拉伸填充。不过还是错了。
-
你介意我看看你的方法吗?