untiy3D 通過外部Json檔案來控制場景截圖
阿新 • • 發佈:2018-12-18
{
"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()截圖程式碼來自網路,在此感謝那位兄弟的分享。