Unity执行程序内快照
参考文档:http://blog.csdn.net/qq_24807077/article/details/79555557
主要的API:
//将IO数据写入
File.WriteAllBytes(path + "/" + count.ToString() + ".png", bytes);
//数据格式转换为二进制
var bytes = tex.EncodeToPNG();
// 读取屏幕数据
picture.ReadPixels(
new Rect(0,0,Screen.width,Screen.height),0,0
);
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; public class ScreenShot : MonoBehaviour { public float ScreenShotHeight=500; public float ScreenShotWith=500; public string PicSavePath = "MyPhoto"; [SerializeField] private Texture2D picture; private RenderTexture renderBuffer; private string Path = ""; private bool IsShot=false; // Use this for initialization void Start () { picture = new Texture2D(Screen.width,Screen.height,TextureFormat.RGBA32,false,true); Path = Application.dataPath + "/"+ PicSavePath; //Debug.LogError(Path); if (Directory.Exists(Path)) { FileAttributes attr = File.GetAttributes(Path); if (attr == FileAttributes.Directory) Directory.Delete(Path, true); else File.Delete(Path); } if (!Directory.Exists(Path)) Directory.CreateDirectory(Path); } // Update is called once per frame void Update () { if (Input.GetKeyDown(KeyCode.Space)&&!IsShot) ShotScreen(); } public void ShotScreen() { IsShot = true; StartCoroutine(CaptureScreen()); } private static int count = 0; public IEnumerator CaptureScreen() { yield return new WaitForEndOfFrame(); picture.ReadPixels( new Rect(0,0,Screen.width,Screen.height),0,0 ); picture.Apply(); var bytes = picture.EncodeToPNG(); File.WriteAllBytes(Path + "/" + count.ToString() + ".png", bytes); count++; Debug.Log("Fin the work gender texture"); yield return new WaitForSeconds(0.1f); IsShot = false; } }