仅适合摄像机是正交模式,Projection=Orthorgraphic
Unity2017.x+及以上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CaptureScreenshotC : MonoBehaviour {
private int count = 0;
void Update () {
if (Input.GetKeyDown(KeyCode.P))
{
OnJTButtonClick();
}
}
public void ScreenShotFile(string fileName) {
UnityEngine.ScreenCapture.CaptureScreenshot(fileName);
}
public void OnJTButtonClick() {
count++;
Debug.Log(Application.persistentDataPath + "/" + count + ".png");
ScreenShotFile(Application.persistentDataPath + "/"+count+".png");
}
}
unity5.6.x及以下
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class CaptureScreenshotC : MonoBehaviour
{
public RawImage CaptureScreenshot;
void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
CaptureScreenshot.texture = CaptureCamera(Camera.main, new Rect(0, 0, 1920 , 1440),true);
}
}
/// <summary>
/// 相机截图
/// </summary>
/// <param name="camera">截屏相机</param>
/// <param name="rect">截屏区域</param>
/// <returns></returns>
public Texture2D CaptureCamera(Camera camera, Rect rect,bool isSaveToFlie)
{
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
camera.targetTexture = rt;
camera.Render();
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
if (isSaveToFlie) {
string path = UnityEngine.Application.persistentDataPath + "/" + "AAA.png";
Debug.Log("保存位置:" + path);
SaveTextureToFile(screenShot, path);
}
camera.targetTexture = null;
RenderTexture.active = null;
Destroy(rt);
return screenShot;
}
private void SaveTextureToFile(Texture2D texture2D,string mFileName)
{
byte[] bytes = texture2D.EncodeToPNG();
//保存
System.IO.File.WriteAllBytes(mFileName, bytes);
}
}
仅适合摄像机是投影模式,Projection=Perspective,但是不包含UI
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class CaptureScreenshotC : MonoBehaviour
{
public RawImage CaptureScreenshot;
public Camera Camera;
void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
//获取正交模式的 摄像机画面
try
{
//CaptureScreenshot.texture = CaptureCamera(Camera, new Rect(0, 0, 1920, 1440), true);
}
catch (System.Exception e)
{
}
//获取投影模式的摄像机画面
CameraCapture(Camera,"AAA.PNG");
}
}
/// <summary>
/// 相机截图
/// </summary>
/// <param name="camera">截屏相机</param>
/// <param name="rect">截屏区域</param>
/// <returns></returns>
public Texture2D CaptureCamera(Camera camera, Rect rect, bool isSaveToFlie)
{
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
camera.targetTexture = rt;
camera.Render();
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
if (isSaveToFlie)
{
string path = UnityEngine.Application.persistentDataPath + "/" + "AAA.png";
Debug.Log("保存位置:" + path);
SaveTextureToFile(screenShot, path);
}
camera.targetTexture = null;
RenderTexture.active = null;
Destroy(rt);
return screenShot;
}
private void SaveTextureToFile(Texture2D texture2D, string mFileName)
{
byte[] bytes = texture2D.EncodeToPNG();
//保存
System.IO.File.WriteAllBytes(mFileName, bytes);
}
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 = Path(filename);
Debug.Log(path);
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;
return UnityEngine.Application.persistentDataPath + "/" + 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
}
}