Unity和ios进行相册的一些交互
2017-03-07 16:50
411 查看
关于unity和相册交互,其实很多人写过,包括大神雨松,但小妹我的这篇绝对也是原创,由于最近这个项目中搞了好久的这东西,所以打算写出来给大家分享一下,希望对大家有所帮助。
说说ios的大概的思路吧,首先在unity打开相册也就是调用_iosOpenPhotoAlbums,然后到ios判断自己的这个应用程序是否有权限打开相册,如果没有权限,就直接调用unity这边的PickImageCallBackJPG_Base64方法,这边可以自行进行处理比如可以提示下你没权限啊去设置一下吧,当然如果你oc出身的话,可以直接在原生处理,比如跳转到设置界面,这样更友好,原谅我有限的oc知识,只能这样实现,如果有权限就要打开相册了,具体的这个方法showPicker,然后再打开相册的方法里面要初始化一些数据,选中某个视频或者图片就走imagePickerController这个方法了,info里面有选中内容,并且可以把一些数据回调给unity,比如地址等等,传回unity的只能是字符串,如果有很多个数据可以自己拼一下,或者是用jason,如果想返回的不是字符串的就要想办法变通一下了,如果不选择任何内容而是点击取消的话会走这个方法imagePickerControllerDidCancel,就关闭相册了。
我这个选中了图片的话会把数据流传到unity那边可以直接进行显示,或者保存到沙盒里面,如果选中的是视频的话,直接把路径传回到unity,可以复制到沙盒,然后进行播放或者是直接按路径把视频上传上去,这个看你具体的需求,得到路径之后,到c#这边应该很好处理了。
抱怨下oc的语法很奇怪,需要有一个.h文件具体内容如下:
@interface IOSAlbumCameraController :
UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@end
然后创建.mm文件,具体内容如下:
#import "IOSAlbumCameraController.h"
@implementation IOSAlbumCameraController
//打开相册
- (void)showPicker:
(UIImagePickerControllerSourceType)type
{
//为什么这么写呢?按照网上其他例子的写法,在打开相册之后,unity的多点触碰会被关掉,如果遇到同样问题的同学可以参考下
UIViewController *vc = UnityGetGLViewController();
IOSAlbumCameraController*picker = [[IOSAlbumCameraController alloc] init];
picker.delegate = self;
picker.sourceType = type;
picker.allowsEditing = NO;
//显示视频不写这句就不显示视频只显示图片看你自己的需求
picker.mediaTypes = [ImageTestController
availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:nil];
}
// 打开相册后选择照片时的响应方法
- (void)imagePickerController:(IOSAlbumCameraController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
UIViewController *vc = UnityGetGLViewController();
[self dismissViewControllerAnimated:YES completion:^{
[vc.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}];
//获取选中类型
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
//如果选择的是视频的话
if([mediaType isEqualToString:@"public.movie"])
{
//应该就算是地址了
NSURL *VIDEOURL = [info objectForKey:UIImagePickerControllerReferenceURL];
NSString *path = [VIDEOURL absoluteString];
//返回unity了
UnitySendMessage( "_ImmortalObjects", "PickImageCallBackMOV_Base64", "100");
}
//如果选择的是图片的话
else if([mediaType isEqualToString:@"public.image"])
{
UIImage *image;
image = [info objectForKey:UIImagePickerControllerOriginalImage];
// 得到了image,然后用你的函数传回u3d
NSData *imgData;
if(image!=nil)
{
imgData= UIImageJPEGRepresentation(image,1);
NSString *_encodeImageStr = [imgData base64Encoding];
UnitySendMessage( "_ImmortalObjects", "PickImageCallBackJPG_Base64", _encodeImageStr.UTF8String);
}
}
// 关闭相册
[picker dismissViewControllerAnimated:YES completion:^{
[vc.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}];
vc.view.multipleTouchEnabled = YES;
}
// 打开相册后点击“取消”的响应
- (void)imagePickerControllerDidCancel:(IOSAlbumCameraController*)picker
{
NSLog(@ " --- imagePickerControllerDidCancel !!");
UIViewController *vc = UnityGetGLViewController();
[self dismissViewControllerAnimated:YES completion:^{
[vc.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}];
vc.view.multipleTouchEnabled = YES;
}
#if defined (__cplusplus)
extern "C" {
#endif
// 打开相册
void _iosOpenPhotoAlbums()
{
//判断权限
ALAuthorizationStatus author =[ALAssetsLibrary authorizationStatus];
if (author == ALAuthorizationStatusRestricted || author==ALAuthorizationStatusDenied)
{
UnitySendMessage( "_ImmortalObjects", "PickImageCallBackJPG_Base64", "没权限");
}
Else if([IOSAlbumCameraController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
IOSAlbumCameraController * app = [[IOSAlbumCameraController alloc] init];
UIViewController *vc = UnityGetGLViewController();
[vc.view addSubview: app.view];
[app showPicker:UIImagePickerControllerSourceTypePhotoLibrary ];
}
}
#if defined (__cplusplus)
}
#endif
在unity中
Void OpenAlum()
{
_iosOpenPhotoAlbums();
}
[DllImport("__Internal")]
private static extern void _iosOpenPhotoAlbums();
void PickImageCallBackJPG_Base64(string base64)
{
if (base64 == "没权限")
{
Debug.log(“没权限”)
}
else
{
//这个t就是选中的图片了
Texture2D t = Base64StringToTexture2D(base64);
byte[] byt = t.EncodeToJPG();
}
}
void PickImageCallBackMOV_Base64(string base64)
{
//base64就是视频的地址
}
说说ios的大概的思路吧,首先在unity打开相册也就是调用_iosOpenPhotoAlbums,然后到ios判断自己的这个应用程序是否有权限打开相册,如果没有权限,就直接调用unity这边的PickImageCallBackJPG_Base64方法,这边可以自行进行处理比如可以提示下你没权限啊去设置一下吧,当然如果你oc出身的话,可以直接在原生处理,比如跳转到设置界面,这样更友好,原谅我有限的oc知识,只能这样实现,如果有权限就要打开相册了,具体的这个方法showPicker,然后再打开相册的方法里面要初始化一些数据,选中某个视频或者图片就走imagePickerController这个方法了,info里面有选中内容,并且可以把一些数据回调给unity,比如地址等等,传回unity的只能是字符串,如果有很多个数据可以自己拼一下,或者是用jason,如果想返回的不是字符串的就要想办法变通一下了,如果不选择任何内容而是点击取消的话会走这个方法imagePickerControllerDidCancel,就关闭相册了。
我这个选中了图片的话会把数据流传到unity那边可以直接进行显示,或者保存到沙盒里面,如果选中的是视频的话,直接把路径传回到unity,可以复制到沙盒,然后进行播放或者是直接按路径把视频上传上去,这个看你具体的需求,得到路径之后,到c#这边应该很好处理了。
抱怨下oc的语法很奇怪,需要有一个.h文件具体内容如下:
@interface IOSAlbumCameraController :
UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@end
然后创建.mm文件,具体内容如下:
#import "IOSAlbumCameraController.h"
@implementation IOSAlbumCameraController
//打开相册
- (void)showPicker:
(UIImagePickerControllerSourceType)type
{
//为什么这么写呢?按照网上其他例子的写法,在打开相册之后,unity的多点触碰会被关掉,如果遇到同样问题的同学可以参考下
UIViewController *vc = UnityGetGLViewController();
IOSAlbumCameraController*picker = [[IOSAlbumCameraController alloc] init];
picker.delegate = self;
picker.sourceType = type;
picker.allowsEditing = NO;
//显示视频不写这句就不显示视频只显示图片看你自己的需求
picker.mediaTypes = [ImageTestController
availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:nil];
}
// 打开相册后选择照片时的响应方法
- (void)imagePickerController:(IOSAlbumCameraController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
UIViewController *vc = UnityGetGLViewController();
[self dismissViewControllerAnimated:YES completion:^{
[vc.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}];
//获取选中类型
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
//如果选择的是视频的话
if([mediaType isEqualToString:@"public.movie"])
{
//应该就算是地址了
NSURL *VIDEOURL = [info objectForKey:UIImagePickerControllerReferenceURL];
NSString *path = [VIDEOURL absoluteString];
//返回unity了
UnitySendMessage( "_ImmortalObjects", "PickImageCallBackMOV_Base64", "100");
}
//如果选择的是图片的话
else if([mediaType isEqualToString:@"public.image"])
{
UIImage *image;
image = [info objectForKey:UIImagePickerControllerOriginalImage];
// 得到了image,然后用你的函数传回u3d
NSData *imgData;
if(image!=nil)
{
imgData= UIImageJPEGRepresentation(image,1);
NSString *_encodeImageStr = [imgData base64Encoding];
UnitySendMessage( "_ImmortalObjects", "PickImageCallBackJPG_Base64", _encodeImageStr.UTF8String);
}
}
// 关闭相册
[picker dismissViewControllerAnimated:YES completion:^{
[vc.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}];
vc.view.multipleTouchEnabled = YES;
}
// 打开相册后点击“取消”的响应
- (void)imagePickerControllerDidCancel:(IOSAlbumCameraController*)picker
{
NSLog(@ " --- imagePickerControllerDidCancel !!");
UIViewController *vc = UnityGetGLViewController();
[self dismissViewControllerAnimated:YES completion:^{
[vc.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}];
vc.view.multipleTouchEnabled = YES;
}
#if defined (__cplusplus)
extern "C" {
#endif
// 打开相册
void _iosOpenPhotoAlbums()
{
//判断权限
ALAuthorizationStatus author =[ALAssetsLibrary authorizationStatus];
if (author == ALAuthorizationStatusRestricted || author==ALAuthorizationStatusDenied)
{
UnitySendMessage( "_ImmortalObjects", "PickImageCallBackJPG_Base64", "没权限");
}
Else if([IOSAlbumCameraController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
IOSAlbumCameraController * app = [[IOSAlbumCameraController alloc] init];
UIViewController *vc = UnityGetGLViewController();
[vc.view addSubview: app.view];
[app showPicker:UIImagePickerControllerSourceTypePhotoLibrary ];
}
}
#if defined (__cplusplus)
}
#endif
在unity中
Void OpenAlum()
{
_iosOpenPhotoAlbums();
}
[DllImport("__Internal")]
private static extern void _iosOpenPhotoAlbums();
void PickImageCallBackJPG_Base64(string base64)
{
if (base64 == "没权限")
{
Debug.log(“没权限”)
}
else
{
//这个t就是选中的图片了
Texture2D t = Base64StringToTexture2D(base64);
byte[] byt = t.EncodeToJPG();
}
}
void PickImageCallBackMOV_Base64(string base64)
{
//base64就是视频的地址
}
相关文章推荐
- Unity与IOS交互,调用IOS系统相机和相册
- unity3d与ios进行交互
- Unity 在iOS中使用ShareSDK进行微博分享
- iOS和JS的一些交互
- IOS与Unity交互
- iOS 人机交互设计(开发)指南及一些综合知识整合
- 如何通过itunes和ios应用底下的沙盒进行共享交互
- iOS 调取本地相册/相机,剪裁图片进行头像上传
- unity用PUN进行信息交互模块
- unity用PUN进行信息交互模块
- iOS --- 使用RestKit与RESTful web服务器进行简单交互
- 对ios创建渐变组件进行点触交互
- Unity iOS保存截图到iOS相册
- IOS 对相册图片进行读取、存储到指定文件夹、从指定文件夹读取出来
- iOS开发中使用相机或系统相册的一些问题总结(UIImagePickerController)
- 在开发过程中,数据的输入和输出全部用string类型进行交互,是否会更好一些?
- ios app 未发布前进行大规模测试的一些思路及做法
- iOS 调用系统相册,调用系统照相机,调用系统相册,并且对照片进行剪裁
- iOS开发工具-如何使用网络封包分析工具Charles,通过配置proxy对http、https、tcp、udp 等协议的请求响应过程交互信息进行分析、判断、解决我们移动开发中的遇到的各种实际问题。
- Unity3D开发之Unity跟IOS交互过程