using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Capturer : MonoBehaviour {
public RawImage image;
void Start()
{
image.transform.parent.gameObject.SetActive(false);
}
void OnGUI()
{
if (GUI.Button(new Rect(0, 0, 70, 50), "拍照"))
{
StartCoroutine(Takephoto());
}
}
IEnumerator Takephoto()
{
yield return new WaitForEndOfFrame();
Texture2D photo = new Texture2D(Screen.width, Screen.height, TextureFormat.ARGB32, false);
photo.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
photo.Apply(); //这句必须
image.texture = photo;
image.transform.parent.gameObject.SetActive(true);
byte[] pngs = photo.EncodeToPNG();
//File.WriteAllBytes(Application.dataPath + "/ff.png", pngs);
}
}
unity截屏脚本
最新推荐文章于 2022-10-14 14:43:49 发布