1:截屏代码
//保存图片的路径 string androidPath = "/../../../../DCIM/" + albumName + "/" + screenshotFilename; string path = Application.persistentDataPath + androidPath; string pathonly = Path.GetDirectoryName(path); Directory.CreateDirectory(pathonly); //Unity自带的截屏方法CaptureScreenshot Application.CaptureScreenshot(androidPath); // Application.CaptureScreenshot(path); //gText.text = androidPath.ToString(); LoadPath = path;
这部分先定义保存的路径,直接保存到DCIM文件下,然后通过Unity自带的截屏方法进行截屏。
2:显示部分代码
//创建文件读取流 FileStream fileStream = new FileStream("/../../../../"+LoadPath, FileMode.Open, FileAccess.Read); Debug.Log("创建了文件流"); fileStream.Seek(0, SeekOrigin.Begin); //创建文件流的缓冲区 byte[] bytes = new byte[fileStream.Length]; // 读取文件 fileStream.Read(bytes, 0, (int)fileStream.Length); // 释放文件流 fileStream.Close(); fileStream.Dispose(); fileStream = null; // 创建texture int height = 800; int width = 680; Texture2D texture = new Texture2D(width, height); texture.LoadImage(bytes); // 创建image。sprite Sprite sprites = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)); image.sprite = sprites;
这部分代码是通过IO来加载,创建texture并通过Image的sprite来显示。本文比较粗鲁,有时间在细聊,看不懂可以留言,我会第一时间回复。