Unity访问安卓(Android)或苹果(iOS)相册
1.下载Native Gallery for Android & iOS插件
2.在场景中添加截图按钮、选择图片按钮、选择视频按钮等
using OpenCVForUnity.CoreModule;
using OpenCVForUnity.ImgprocModule;
using OpenCVForUnity.UnityUtils;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;public class MyPhoto : MonoBehaviour
{public Renderer renderer;public TMP_Text textPrompt;public void DoCapture(){StartCoroutine(TakeScreenshotAndSave());}// Example code doesn't use this function but it is here for reference. It's recommended to ask for permissions manually using the// RequestPermissionAsync methods prior to calling NativeGallery functionsprivate async void RequestPermissionAsynchronously(NativeGallery.PermissionType permissionType, NativeGallery.MediaType mediaTypes){NativeGallery.Permission permission = await NativeGallery.RequestPermissionAsync(permissionType, mediaTypes);MyLog("Permission result: " + permission);}private IEnumerator TakeScreenshotAndSave(){yield return new WaitForEndOfFrame();Texture2D ss = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);ss.ReadPixels(new UnityEngine.Rect(0, 0, Screen.width, Screen.height), 0, 0);ss.Apply();// Save the screenshot to Gallery/PhotosNativeGallery.Permission permission = NativeGallery.SaveImageToGallery(ss, "Camera", "Image.png", (success, path) => MyLog("Media save result: " + success + " " + path));MyLog("Permission result: " + permission);// To avoid memory leaksDestroy(ss);}public void PickImage(int maxSize){NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>{MyLog("Image path: " + path);if (path != null){// Create Texture from selected imageTexture2D texture = NativeGallery.LoadImageAtPath(path, maxSize);if (texture == null){MyLog("Couldn't load texture from " + path);return;}renderer.material.mainTexture = texture;}});MyLog("Permission result: " + permission);}public void PickVideo(){NativeGallery.Permission permission = NativeGallery.GetVideoFromGallery((path) =>{MyLog("Video path: " + path);if (path != null){// Play the selected videoHandheld.PlayFullScreenMovie("file://" + path);}}, "Select a video");MyLog("Permission result: " + permission);}// Example code doesn't use this function but it is here for referencepublic void PickImageOrVideo(){if (NativeGallery.CanSelectMultipleMediaTypesFromGallery()){NativeGallery.Permission permission = NativeGallery.GetMixedMediaFromGallery((path) =>{MyLog("Media path: " + path);if (path != null){// Determine if user has picked an image, video or neither of theseswitch (NativeGallery.GetMediaTypeOfFile(path)){case NativeGallery.MediaType.Image: MyLog("Picked image"); break;case NativeGallery.MediaType.Video: MyLog("Picked video"); break;default: MyLog("Probably picked something else"); break;}}}, NativeGallery.MediaType.Image | NativeGallery.MediaType.Video, "Select an image or video");MyLog("Permission result: " + permission);}}void MyLog(string str){textPrompt.text += str;}
}