unity3d使用脚本保存屏幕截图

时间:2023-03-08 20:39:41
unity3d使用脚本保存屏幕截图
using UnityEngine;
using System.Collections;
using System.IO; public class FrameAnimation : MonoBehaviour {
public Texture2D image;
public int w;
public int h;
public float nextTime = 0.0f;
public float rate = 0.3f;
int i = ;
// Use this for initialization
void Start () {
w = Screen.width;
h = Screen.height;
image = new Texture2D(w, h);
} // Update is called once per frame
void Update () {
if (Input.GetMouseButton() && Time.time > nextTime)
{
nextTime = Time.time + rate;
i++;
StartCoroutine(SaveImage(i));
}
} IEnumerator SaveImage(int i)
{
yield return new WaitForEndOfFrame();
image.ReadPixels(new Rect(, , w, h), , , true);//read pixels from screen to texture
image.Apply();
byte[] bytes = image.EncodeToPNG();
File.WriteAllBytes(Application.streamingAssetsPath + "/" + i + ".png", bytes);
Debug.Log("write a pic");
yield return null;
}
}