一种sprite变为Textrue2d,存在本地 ,博客已经说过
一种相机截图 需要引入 System.Drawing.dll:
public void CaptureCamera(Camera camera)
{
Rect rect=new Rect(Screen.width * 0f, Screen.height * 0f, Screen.width * 1f, Screen.height * 1f);
// 创建一个RenderTexture对象
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
camera.targetTexture = rt;
camera.Render();
RenderTexture.active = rt;
//Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24, false);
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height);
screenShot.ReadPixels(rect,0, 0);// 注:这个时候,它是从RenderTexture.active中读取像素
screenShot.Compress(true);
screenShot.Apply();
ui.TakePhoto.SetActive(true);
Sprite pic = Sprite.Create(screenShot, new Rect(0, 0, screenShot.width, screenShot.height), new Vector2(0.5f, 0.5f));//后面Vector2就是你Anchors的Pivot的x/y属性值
ui.Photo.GetComponent<UnityEngine.UI.Image>().sprite = pic;
camera.targetTexture = null;
//ps: camera2.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
Destroy(rt);
byte[] bytes = screenShot.EncodeToPNG();
i++;
//filename = Application.persistentDataPath + "/Screenshot" + i.ToString()+".png";
//创建一个新文件夹
path = "/storage/emulated/0/AR课本";
Directory.CreateDirectory(path);
pathpic = "/storage/emulated/0/AR课本/截图";
Directory.CreateDirectory(pathpic);
filenameOld = pathpic + "/IMG_" + i.ToString() + ".png";
File.WriteAllBytes(filenameOld, bytes);
}