Unity与IOS交互之拍照与相册

参考链接 : https://blog.csdn.net/u010133610/article/details/51533434

首先下载 https://github.com/onevcat/XUPorter 它用来自动填写IOS配置

先写ios调用拍照和相册的代码

首先呢是 IOSAlbumCameraController.h 文件

//
//  IOSAlbumCameraController.h
//  Unity-iPhone
//
//  Created by chenlaibo chenlaibo on 2018/10/23.
//

#ifndef IOSAlbumCameraController_h
#define IOSAlbumCameraController_h

#import <QuartzCore/CADisplayLink.h>
//#import <Foundation/Foundation.h>

@interface IOSAlbumCameraController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@end

#endif /* IOSAlbumCameraController_h */

然后是 IOSAlbumCameraController.mm 文件

//
//  IOSAlbumCameraController.m
//  Unity-iPhone
//
//  Created by chenlaibo chenlaibo on 2018/10/23.
//

#import "IOSAlbumCameraController.h"

@implementation IOSAlbumCameraController
//打开相册的
-(void)OpenTarget:(UIImagePickerControllerSourceType)type
{
    UIImagePickerController *picker;
    picker=[[UIImagePickerController alloc]init];
    picker.delegate =self;
    picker.allowsEditing=NO;
    picker.sourceType=type;

//    dispatch_async(dispatch_get_main_queue(), ^{
//        
//    });
    
    [self presentViewController:picker animated:YES completion:nil];
    
}

#pragma mark 代理
//didFinishPickingMediaWithInfo
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    NSLog(@"6666");
    UIImage *img=info[UIImagePickerControllerOriginalImage];
    
    NSString *imgPath=[self GetSavePath:@"Temp.jpg"];
    [self SaveFileToDoc:img path:imgPath];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(NSString*)GetSavePath:(NSString*)filename
{
    NSArray *pathArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *path = [pathArray objectAtIndex:0];
    return [path stringByAppendingPathComponent:filename];
}

-(void)SaveFileToDoc:(UIImage *)imgae path:(NSString*)path
{
    NSData *data;
    if(UIImagePNGRepresentation(imgae)==nil){
        data=UIImageJPEGRepresentation(imgae,1);
    }else{
        data=UIImagePNGRepresentation(imgae);
    }
    [data writeToFile:path atomically:YES];
    UnitySendMessage("IOSBack","getImage" ,[path UTF8String]);
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    UnitySendMessage("IOSBack","getImage" , "");
    [self dismissViewControllerAnimated:YES completion:nil];
}

#if defined (__cplusplus)
extern "C" {
#endif
    void _iosCamera(){
        IOSAlbumCameraController *app=[[IOSAlbumCameraController alloc]init];
        UIViewController *vc = UnityGetGLViewController();
        [vc.view addSubview:app.view];
        [app OpenTarget:UIImagePickerControllerSourceTypeCamera];
    }
    void _iosAlbum(){
        IOSAlbumCameraController *app=[[IOSAlbumCameraController alloc]init];
        UIViewController *vc = UnityGetGLViewController();
        [vc.view addSubview:app.view];
        [app OpenTarget:UIImagePickerControllerSourceTypePhotoLibrary];

    }
#if defined (__cplusplus)
}
#endif

@end

注意 UnitySendMessage 函数

第一个参数是 unity的对象名称

第二个参数是 该对象上挂的脚本的函数名称

 

观察 extern "C" ,这里面有2个函数,unity 就是调用这两个函数运行ios代码

恩,好了现在来写unity调用代码,相信这代码简单易懂

using UnityEngine;
using UnityEngine.UI;
#if UNITY_IPHONE
using System.Runtime.InteropServices;
#endif
public class AlbumCamera : MonoBehaviour {

    public Button albumButton;
    public Button cameraButton;

#if UNITY_IPHONE
	
	[DllImport("__Internal")]
	private static extern void _iosCamera();
	[DllImport("__Internal")]
	private static extern void _iosAlbum();
	
	void Awake()
	{
		cameraButton.onClick.AddListener(_iosCamera);
        albumButton.onClick.AddListener(_iosAlbum);
	}
	
	void getImage(string path)
	{
		Debug.Log(path);
	}
#endif
}

 

 

最后我们来配置一下 XUPorter

文件存放结构

 

编写 my.projmods

{
  "group": "my",
  "libs": [],
  "frameworks": [
    "AssetsLibrary.framework",
    "Photos.framework",
    "SystemConfiguration.framework"
  ],
  "headerpaths": [],
  "files": [],
  "folders": [ "AlbumCamera/XUPorter-master/Mods/iOS/" ],
  "excludes": [ "^.*.meta$", "^.*.mdown$", "^.*.pdf$" ],
  "compiler_flags": [],
  "linker_flags": [],
  "embed_binaries": [],
  "plist": "XML/plist.xml"
}

留意上面的 plist 这里说的是读取 XML目录下的plist.xml

添加一些权限

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CFBundleDevelopmentRegion</key>
	<string>zh_CN</string>
	<key>NSPhotoLibraryUsageDescription</key>
	<string>需要您的同意,才能访问相册</string>
	<key>NSCameraUsageDescription</key>
	<string>需要您的同意,才能访问相机</string>
</dict>
</plist>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

发布运行啊,愣着干嘛

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小鱼游戏开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值