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 = 0;
    // 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(0) && Time.time > nextTime)
        {
            nextTime = Time.time + rate;
            i++;
            StartCoroutine(SaveImage(i));
        }
    }

    IEnumerator SaveImage(int i)
    {
        yield return new WaitForEndOfFrame();
        image.ReadPixels(new Rect(0, 0, w, h), 0, 0, 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;
    }
}
原文地址:https://www.cnblogs.com/bluebean/p/5746887.html