Unity c#,截图并保存为jpg文件
本文关键字:保存 jpg 文件 Unity | 更新日期: 2023-09-27 18:25:53
我正在尝试拍摄一个屏幕截图并将其保存到jpg格式的文件中。我正以这个例子为例。
http://docs.unity3d.com/ScriptReference/Texture2D.EncodeToPNG.html
这就是我目前所拥有的:
string jpgFile = Application.persistentDataPath + "/scrn-1.jpg";
Texture2D tex = new Texture2D (Screen.width, Screen.height);
tex.ReadPixels (new Rect(0, 0, Screen.width, Screen.height), 0, 0);
tex.Apply ();
var bytes = tex.EncodeToJPG();
Destroy (tex);
System.IO.File.WriteAllBytes(jpgFile, bytes);
我发现在iOS上的Unity中运行这个给了我:
JPEG参数结构不匹配:库认为大小为372,调用方期望360
但是,如果我将转换更改为tex.EncodeToPNG();并将文件名更改为.png,一切正常。我不知道该怎么办。如果有任何帮助,我将不胜感激。谢谢
我找到了这个,它可以让你把它保存为.jpg。我通常只使用捕获屏幕并保存为.png.
给你:
using UnityEngine;
using System.Collections;
public class HiResScreenShots : MonoBehaviour {
public int resWidth = 2550;
public int resHeight = 3300;
private bool takeHiResShot = false;
public static string ScreenShotName(int width, int height) {
return string.Format("{0}/screenshots/screen_{1}x{2}_{3}.png",
Application.dataPath,
width, height,
System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"));
}
public void TakeHiResShot() {
takeHiResShot = true;
}
void LateUpdate() {
takeHiResShot |= Input.GetKeyDown("k");
if (takeHiResShot) {
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
camera.targetTexture = rt;
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
camera.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
camera.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
Destroy(rt);
byte[] bytes = screenShot.EncodeToPNG();
string filename = ScreenShotName(resWidth, resHeight);
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("Took screenshot to: {0}", filename));
takeHiResShot = false;
}
}
}
如果你只想要任何一种图片,我强烈推荐这个:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
void OnMouseDown()
{
Application.CaptureScreenshot("Screenshot.png");
}
}
简单有效。
我今天确实需要这样的东西,但没有保存。Unity没有为我们提供一个默认的方法来立即将图像作为字节返回,这就是我解决问题的方法。
internal class ScreenCapture : MonoBehaviour
{
internal byte[] Image = null;
internal void SaveScreenshot()
{
StartCoroutine(SaveScreenshot_ReadPixelsAsynch());
}
private IEnumerator SaveScreenshot_ReadPixelsAsynch()
{
//Wait for graphics to render
yield return new WaitForEndOfFrame();
//Create a texture to pass to encoding
Texture2D texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
//Put buffer into texture
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
//Split the process up--ReadPixels() and the GetPixels() call inside of the encoder are both pretty heavy
yield return 0;
byte[] bytes = texture.EncodeToPNG();
Image = bytes;
//Tell unity to delete the texture, by default it seems to keep hold of it and memory crashes will occur after too many screenshots.
DestroyObject(texture);
}
}
用法:
internal static ScreenCapture Capture;
if (Capture == null)
{
Capture = Camera.main.gameObject.AddComponent<ScreenCapture>();
}
Capture.SaveScreenshot();
// if you wish to save:
System.IO.File.WriteAllBytes("filename.png", Capture.Image);