介紹一個特別使用的插件: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