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 functions
    private 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/Photos
        NativeGallery.Permission permission = NativeGallery.SaveImageToGallery(ss, "Camera", "Image.png", (success, path) => MyLog("Media save result: " + success + " " + path));

        MyLog("Permission result: " + permission);

        // To avoid memory leaks
        Destroy(ss);
    }

    public void PickImage(int maxSize)
    {
        NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
        {
            MyLog("Image path: " + path);
            if (path != null)
            {
                // Create Texture from selected image
                Texture2D 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 video
                Handheld.PlayFullScreenMovie("file://" + path);
            }
        }, "Select a video");

        MyLog("Permission result: " + permission);
    }

    // Example code doesn't use this function but it is here for reference
    public 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 these
                    switch (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;

    }
}
相关推荐
Yeats_Liao几秒前
Spring 定时任务:@Scheduled 注解四大参数解析
android·java·spring
雾里看山2 小时前
【MySQL】 库的操作
android·数据库·笔记·mysql
陈皮话梅糖@2 小时前
iOS 集成ffmpeg
ios·ffmpeg
幽夜落雨3 小时前
ios老版本应用安装方法
ios
水瓶丫头站住10 小时前
安卓APP如何适配不同的手机分辨率
android·智能手机
xvch11 小时前
Kotlin 2.1.0 入门教程(五)
android·kotlin
胖虎111 小时前
实现 iOS 自定义高斯模糊文字效果的 UILabel(文末有Demo)
ios·高斯模糊文字·模糊文字
墨笺染尘缘14 小时前
Unity——鼠标是否在某个圆形Image范围内
unity·c#·游戏引擎
xvch15 小时前
Kotlin 2.1.0 入门教程(七)
android·kotlin
望风的懒蜗牛15 小时前
编译Android平台使用的FFmpeg库
android