Unity 获取相机画面将其保存成图片
2023-01-03 本文已影响0人
114105lijia
void CameraCapture(Camera m_Camera, string filename)
{
int width = Screen.width;
int height = Screen.height;
RenderTexture rt = new RenderTexture(width, height, 16);
m_Camera.targetTexture = rt;
m_Camera.Render();
RenderTexture.active = rt;
Texture2D t = new Texture2D(width, height);
t.ReadPixels(new Rect(0, 0, width, height), 0, 0);
t.Apply();
string path = CommonTool.Path(filename);
System.IO.File.WriteAllBytes(path, t.EncodeToJPG());
m_Camera.targetTexture = null;
}
public static string Path(string fileName) {
#if UNITY_EDITOR
return Application.dataPath + "/StreamingAssets/" + fileName;
#elif UNITY_IPHONE
return Application.dataPath+"/Ray/" + fileName;
#elif UNITY_ANDROID
return "jar:file://" + Application.dataPath + "!/assets/" + fileName;
#else
return Application.dataPath + "/StreamingAssets/" + fileName;
#endif
}