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;

    }
}
相关推荐
芋只因9 分钟前
MySQL 分库分表与 MyCat 的使用
android
Ehtan_Zheng20 分钟前
Jetpack Compose 与 RecyclerView 混合布局的性能债
android
pop_xiaoli1 小时前
【iOS】SDWebImage源码
macos·ios·objective-c·cocoa
Kapaseker1 小时前
MVVM 旧城改造,边界划分各有招
android·kotlin
郝学胜-神的一滴1 小时前
中级OpenGL教程 004:为几何体注入法线灵魂
c++·unity·游戏引擎·godot·图形渲染·opengl·unreal
我滴老baby1 小时前
多智能体协作系统设计当AI学会团队合作效率翻十倍
android·开发语言·人工智能
StockTV2 小时前
新加坡股票API 实时行情、K 线及指数数据
android·java·spring boot·后端·区块链
草莓熊Lotso2 小时前
LangChain从入门到精通:环境搭建→核心能力→LCEL链式编程全实战
android·java·linux·服务器·langchain
私人珍藏库15 小时前
【Android】聆听岛[特殊字符]聚合全网音乐[特殊字符]免费听歌下载神器[特殊字符] 聚合音乐平台|无损母带下载|歌词封面同步|免费无广告听歌工具
android·人工智能·工具·软件·多功能
YF021116 小时前
Android触摸机制与自定义 View 实战
android·app