它非常简单。在您的 C# 代码中,为您的本机代码添加 DllImport,并创建一个简单的函数来接收图像路径和文本消息。
然后在你的 Assets/Plugins/iOS/ 添加一个类,例如:“ShareHandler.mm”
在那个类中,有一个你可以调用的函数,它接受路径和文本。像这样将路径转换为图像:
NSString *imagePath = [NSString stringWithUTF8String:path];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
在此处创建要发布的项目的 NSArray:
NSArray *postItems = @[image, message];
然后添加这个:
UIActivityViewController *activityVc = [[UIActivityViewController alloc]initWithActivityItems:postItems applicationActivities:nil];
if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && [activityVc respondsToSelector:@selector(popoverPresentationController)] ) {
UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityVc];
[popup presentPopoverFromRect:
CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)
inView:[UIApplication sharedApplication].keyWindow.rootViewController.view
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:activityVc animated:YES completion:nil];
}
之后,从类的 extern "C" 部分调用此函数:
extern "C"{
void shareImageWithTextOnIOS(const char * path, const char * message){
ImageShareForiOS *vc = [[ImageShareForiOS alloc] init];
[vc shareMethod:path Message:message];
}
这对我来说很好。希望这会有所帮助。
请阅读 Unity iOS 插件文档以了解如何创建 Unity iOS 插件。它非常有用!
https://docs.unity3d.com/Manual/PluginsForIOS.html