Unity调用iOS原生函数非常简单,只需要两步即可完成:
第一步:在Unity中声明函数并调用:
声明:
#if UNITY_IPHONE
[DllImport("__Internal")]
private static extern void 函数名(参数类型* 参数名);
#endif
eg:
#if UNITY_IPHONE
[DllImport("__Internal")]
private static extern void SavePhoto(char * picPath);
#endif
Unity调用:
IEnumerator unitySaveLocalPic(char * picPath){
SavePhoto(picPath);
}
第二步:在iOS中实现函数:
声明:
extern “C” void 函数名(参数类型* 参数名)
iOS原生实现图片保存到相册eg:
extern "C" void SavePhoto(char *picPath){
NSString *pathStr = [NSString stringWithUTF8String:picPath];
UIImage* image = [UIImage imageWithContentsOfFile:pathStr];
UIImageWriteToSavedPhotosAlbum(image, mySelf , @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
-(void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo
{
NSString* result;
if(error)
{
result = @"图片保存到相册失败!";
}
else
{
result = @"图片保存到相册成功!";
}
NSLog(@"result---->%@", result);
}
注:上面的 extern “C” 声明的C函数内部不能直接使用OC的变量self,所以需要声明一个C语言的中间变量来传值,就如上面我写的mySelf,是接受OC的self值的,需要先声明: static id mySelf,然后在适当位置给mySelf赋值,如在viewDidLoad中或者appDelegate里赋值,mySelf = self。
在发布的unity3D工程里面的程序入口类UnityAppController.mm 里面的createUI方法里面有一句 [UIView setAnimationsEnabled:NO]; 使得导航栏推出效果动画消失了,改为YES或者注释掉就可以。
---------------------
作者:liudongx
来源:优快云
原文:https://blog.youkuaiyun.com/LIUDONGX/article/details/80203023
版权声明:本文为博主原创文章,转载请附上博文链接!