当前位置: 首页 > news >正文

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;}
}

http://www.lryc.cn/news/325665.html

相关文章:

  • 用webpack 构建自己的vue-cli
  • ZCC6982最大充电电流 2A、升压型 2 节锂电池充电管理器
  • 【机器学习】无监督学习算法之:K均值聚类
  • 为wordpress特定分类目录下的内容添加自定义字段
  • javaWeb在线考试系统
  • 项目管理商业文件--商业论证与效益管理计划
  • 机器学习揭秘:解锁从理论到实践的每一步!
  • Kotlin协程CoroutineScope命名空间CoroutineName,Kotlin
  • HAL STM32G4 +TIM1 3路PWM互补输出+VOFA波形演示
  • MySQL进阶-----索引的结构与分类
  • FPGA高端项目:解码索尼IMX390 MIPI相机转HDMI输出,提供FPGA开发板+2套工程源码+技术支持
  • 激光显示技术不断进步 国家政策推动行业发展
  • iOS开发优势解析,费用探究以及软件开发详解
  • 02课程发布模块之部署Nginx
  • web学习笔记(四十二)
  • 大模型分布式推理ray
  • Python学习:循环语句
  • 【物联网开源平台】tingsboard二次开发
  • Vue+ELement UI el-table移入或选中某行时改变颜色
  • 【Git】日志功能
  • 【网络爬虫】(1) 网络请求,urllib库介绍
  • yolov9目标检测可视化图形界面GUI源码
  • 美团2024届秋招笔试第二场编程真题
  • Server-u配置FTP 多用户访问多目录图解
  • ARM IHI0069F GIC architecture specification (1)
  • golang+vue微服务电商系统
  • 2024年大模型面试准备(三):聊一聊大模型的幻觉问题
  • 微信小程序实战:无痛集成腾讯地图服务
  • [flask]flask的路由
  • javaWeb项目-快捷酒店信息管理系统功能介绍