{
"SceneName":"PrintScreen",
"Position":{"x":0,"y":10,"z":20},
"Rotation":{"x":0,"y":0,"z":0},
"ScreenWidth":1024,
"ScreenHeight":768,
"ImageName":"Test01.png",
"OutPath":"C://OutPath"
}
上面是Json文件的數據,通過上面的數據,我們來控制截取某個場景的局部圖片,上面的參數分別爲:
SceneName:場景名稱
Position:攝像機位置
Rotation :攝像機朝向
ScreenWidth:圖片的寬
ScreenHight:圖片的高
ImageName:圖片的名稱
OutPath:圖片保存的位置
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
[Serializable]
public class ListRot
{
public float x;
public float y;
public float z;
}
public class JsonManage
{
public string SceneName;
public ListRot Position;
public ListRot Rotation;
public int ScreenWidth;
public int ScreenHeight;
public string ImageName;
public string OutPath;
}
public class PrintScreen : MonoBehaviour
{
private Camera camera;
private Rect rect;
JsonManage jm;
AsyncOperation async;
int num ;
void Start()
{
num = 0;
camera = Camera.main;
//讀取json信息
jm = JsonUtility.FromJson<JsonManage>(File.ReadAllText("C:\\PrintScreen.json"));
//參數賦值
camera.transform.position = new Vector3(jm.Position.x, jm.Position.y, jm.Position.z);
camera.transform.eulerAngles = new Vector3(jm.Rotation.x, jm.Rotation.y, jm.Rotation.z);
rect.width = jm.ScreenWidth;
rect.height = jm.ScreenHeight;
//跳轉場景後保留Camera對象
DontDestroyOnLoad(this);
async = SceneManager.LoadSceneAsync(jm.SceneName);
}
private void Update()
{
//判斷場景跳轉成功,可截圖一次
if (async.isDone && num == 0)
{
num++;
Capture3();
}
}
//private void OnGUI()
//{
// if(GUI.Button(new Rect(10, 10, 100, 50), "截圖"))
// {
// print("開始截圖");
// Capture3();
// }
//}
public Texture2D Capture3()
{
// 創建一個RenderTexture對象
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
// 臨時設置相關相機的targetTexture爲rt, 並手動渲染相關相機
camera.targetTexture = rt;
camera.Render();
//ps: --- 如果這樣加上第二個相機,可以實現只截圖某幾個指定的相機一起看到的圖像。
//ps: camera2.targetTexture = rt;
//ps: camera2.Render();
//ps: -------------------------------------------------------------------
// 激活這個rt, 並從中中讀取像素。
RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
screenShot.ReadPixels(rect, 0, 0);// 注:這個時候,它是從RenderTexture.active中讀取像素
screenShot.Apply();
// 重置相關參數,以使用camera繼續在屏幕上顯示
camera.targetTexture = null;
//ps: camera2.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
GameObject.Destroy(rt);
// 最後將這些紋理數據,成一個png圖片文件
byte[] bytes = screenShot.EncodeToPNG();
//文件夾不存在時自動創建文件夾
if (!Directory.Exists(jm.OutPath))
{
Directory.CreateDirectory(jm.OutPath);
}
//string filename = Application.dataPath + "/Screenshot3.png";
string filename = jm.OutPath + @"/" + jm.ImageName +".png";
System.IO.File.WriteAllBytes(filename, bytes);
//AssetDatabase.Refresh();
Debug.Log(string.Format("截屏了一張照片: {0}", filename));
return screenShot;
}
}
操作方法:
1、寫好Json文件放在C盤下(本案例的Json文件是放在"C:\\PrintScreen.json")
2、創建空場景將此腳本掛在MainCamera上
3、再創建一個需要截圖的場景,場景名需要跟Json文件的第一個參數一致(運行時需保證所有場景均加入了BuildSettings中)
4、運行即可得到一張截圖,保存在指定位置(本案例的截圖保存在C:\OutPath)
此案例可以控制任意場景的截圖,只需要外部在Json文件中修改即可,capture3()截圖代碼來自網絡,在此感謝那位兄弟的分享。