Unity EncodeToPNG保存在512x512,无法更改尺寸

时间:2017-05-30 15:42:19

标签: c# unity3d io png

我似乎无法将EncodeToPNG()保存到512 x 512以外的文件尺寸,即使我的纹理是1280 x 1024,我从我的RenderTexture对象的尺寸中拉出来#&# 39; TEX&#39 ;.我错过了什么?谢谢!

// 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);
    }
}

0 个答案:

没有答案