Unity 保存 Texture

保存 Texture2D 到硬盘
后来想了下,如果只是为了看中间结果,例如噪声图之类的,其实没必要保存,在FrameDebug里直接看就行了

Ctrl + LeftClick
Unity 保存 Texture_第1张图片

public bool SaveRenderTextureToPNG(Texture2D spng, string contents, string pngName)
{
    Debug.Log($"save {contents}");
    Texture2D png = new Texture2D(spng.width, spng.height, TextureFormat.ARGB32, false);
    png.ReadPixels(new Rect(0, 0, spng.width, spng.height), 0, 0);
    byte[] bytes = spng.EncodeToPNG();
    if (!Directory.Exists(contents))
        Directory.CreateDirectory(contents);
    FileStream file = File.Open(contents + "/" + pngName + ".png", FileMode.Create);
    BinaryWriter writer = new BinaryWriter(file);
    writer.Write(bytes);
    file.Close();
    Texture2D.DestroyImmediate(png);
    png = null;
    return true;

}
SaveRenderTextureToPNG(m_DitherMap, "D:\\RT", "111");

你可能感兴趣的:(unity3d)