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 
//#import 

@interface IOSAlbumCameraController : UIViewController
@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 *)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

文件存放结构

Unity与IOS交互之拍照与相册_第1张图片

 

编写 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

添加一些权限





	CFBundleDevelopmentRegion
	zh_CN
	NSPhotoLibraryUsageDescription
	需要您的同意,才能访问相册
	NSCameraUsageDescription
	需要您的同意,才能访问相机

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

发布运行啊,愣着干嘛

你可能感兴趣的:(unity3d)