介绍一个特别使用的插件:NativeGallery
百度云下载地址:链接:https://pan.baidu.com/s/1j7VilQ8dUSNdEwqwrI__OQ
提取码:uefe
github下载地址:https://github.com/yasirkula/UnityNativeGallery
此插件可帮助您将图像和/或视频保存到 Android 上的设备和iOS 上的图库中。
也可以从“图库/照片”中选择图像或视频并获取。
(一)将图像保存在设备上:(第一参数是Texture2D图片信息,第二个参数是目录名称,第三个是图片名称)
NativeGallery.SaveImageToGallery( Texture2D image, string album, string filename, MediaSaveCallback callback = null )
//这里记录一下项目使用案例:(需求服务端发送的是base64接收并下载)
Texture2D pic = new Texture2D(1111, 1111);
byte[] data = System.Convert.FromBase64String(base64STR);
pic.LoadImage(data);
byte[] bytes = pic.EncodeToPNG();
NativeGallery.SaveImageToGallery(pic, "qp", "loadpic.png")
//截屏保存
Texture2D ss = new Texture2D( Screen.width, Screen.height, TextureFormat.RGB24, false );
ss.ReadPixels( new Rect( 0, 0, Screen.width, Screen.height ), 0, 0 );
ss.Apply();
// Save the screenshot to Gallery/Photos
Debug.Log( "Permission result: " + NativeGallery.SaveImageToGallery( ss, "GalleryTest", "My img {0}.png" ) );
(二)获取本地图片
NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
{
Debug.Log("Image path: " + path);
if (path != null)
{
// Create Texture from selected image
Texture2D pic = NativeGallery.LoadImageAtPath(path, 512,false);
if (pic == null)
{
Debug.Log("Couldn't load texture from " + path);
return;
}
Debug.Log("获取图片的Texture2D: " + pic);
byte[] bytesArr = pic.EncodeToPNG();
Debug.Log("获取图片转成png的byte数据: " + bytesArr);
string strbase64 = Convert.ToBase64String(bytesArr);
Debug.Log("获取图片转成base64数据: " + strbase64 );
}
}, "Select a PNG image", "image/png");
以上是本次项目的需求功能实现记录,参照CSDN博主「YOLO_TO_GAME」的原创文章,链接https://blog.csdn.net/weixin_39706943/article/details/81196089