using UnityEngine;
using System.Collections;
using System.IO;
public class NewBehaviourScript : MonoBehaviour {
private Texture2D tex;
void Start () {
CaptureScreenshot2(new Rect(Screen.width * 0f, Screen.height * 0f, Screen.width * 1f, Screen.height * 1f));
StartCoroutine(GetImage());
}
// Update is called once per frame
void Update () {
}
IEnumerator GetImage()
{
string dir = Application.persistentDataPath;
string filePath = dir + "hello" + ".png";
string url = "file://" + Application.dataPath + "/StreamingAssets/" + "1.jpg";
WWW www = new WWW(url);
yield return www;
tex = www.texture;
byte[] pngData = tex.EncodeToPNG();
File.WriteAllBytes(filePath, pngData);
}
public IEnumerator CaptureScreenshot()
{
yield return new WaitForEndOfFrame();
Rect rect = new Rect(Screen.width * 0f, Screen.height * 0f, Screen.width * 1f, Screen.height * 1f);
// 先创建一个的空纹理,大小可根据实现需要来设置
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
// 读取屏幕像素信息并存储为纹理数据,
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
// 然后将这些纹理数据,成一个png图片文件
byte[] bytes = screenShot.EncodeToJPG();
string filename = Application.persistentDataPath + "/Screenshot.jpg";
System.IO.File.WriteAllBytes(filename, bytes);
}
}
Unity 截屏,WWW加载图片,保存到本地
最新推荐文章于 2023-12-02 21:27:50 发布