C# Unity EncodeToPNG储存在512x512,can';不能改变尺寸

C# Unity EncodeToPNG储存在512x512,can';不能改变尺寸,c#,unity3d,io,png,C#,Unity3d,Io,Png,我似乎无法让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

我似乎无法让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区域拉伸以填充。不过还是错了。你介意我看看你的方法吗?