unity与iOS之间的简单交互
来源:互联网 发布:ar技术应用于淘宝 编辑:程序博客网 时间:2024/06/06 07:20
unity与iOS的交互要比Android之间的简单,
unity会编译成xcode的项目以供我们进行二次的开发。
目前我所知道的交互有,unity调用iOS的方法,iOS传递字符串给unity两种
1
随意创建xcode的项目,
创建一个NSObject类文件,在.h文件
#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface AlertView : NSObject#if defined(__cplusplus)extern"C"{#endif extern void UnitySendMessage(const char *,const char *, const char *);#if defined(__cplusplus)}#endifvoid _showTestView(char *msg);void _showCompressView (void);@end
以上的_showTestView(含参数),showCompressView(无参数)是unity调用的方法
在.m文件中,要先实例
static AlertView* alert=nil;+ (AlertView *) instance{ if (alert==nil) { alert=[[AlertView alloc]init]; } return alert;}
然后通过alert调用具体的方法
#if defined(__cplusplus)extern"C"{#endif void _showTestView(char *msg){ [AlertView instance]; NSString * stringMsg = [NSString stringWithUTF8String:msg]; [alert showTestView:stringMsg]; } void _showCompressView (void){ [AlertView instance]; [alert showCompress]; } #if defined(__cplusplus)}#endif
在具体实现_showTestView(含参数),showCompressView(无参数)的方法
- (void)showCompress{ UIWindow *window = [UIApplication sharedApplication].keyWindow; UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, window.frame.size.width - 150, 50)]; lab.text = @"实现showCompress方法"; lab.textAlignment = 1; lab.center = CGPointMake(window.frame.size.width / 2 , window.frame.size.height - 40); [lab setFont:[UIFont systemFontOfSize:20]]; lab.textColor = [UIColor whiteColor]; lab.numberOfLines = 0; lab.backgroundColor = [UIColor lightGrayColor]; lab.alpha = 0.7; lab.layer.masksToBounds = YES; lab.layer.cornerRadius = 10; [window addSubview:lab];}
- (void)showTestView:(NSString *)msg{ UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil]; [alert show]; //这是将iOS的字符串传递到unity中的方法 /* 参数一 unity中的代码所挂载的物体 参数二 unity中代码里面的方法 参数三 所需传递的字符串 */ UnitySendMessage([@"Main Camera" UTF8String],[@"iOS" UTF8String],[@"hello world" UTF8String]); }
这样,在iOS中需要做的工作就完成了
将这个类文件复制到unity的文件夹中,一般路径是Plugins/iOS文件夹中
然后在该文件夹中创建C#代码,
using UnityEngine; using System.Collections; using System.Runtime.InteropServices; public static class IOS { [DllImport ("__Internal")] private static extern void _showTestView(string msg); [DllImport ("__Internal")] private static extern void _showCompressView();public static void showTestView (string msg){_showTestView(msg);}public static void showCompressView (){_showCompressView();}}
创建一个C#脚本,挂载到物体上,例如Main Camera相机上,
这样当我们在脚本中调用IOS.showTestView方法,和IOS.showCompressView方法时,就会自动调用到NSObject类中写好的方法,
同时,在unitySendMessage方法会返回一个“hello world”字符串到Main Camera物体上的iOS方法,所以我们要在脚本中创建一个方法
void iOS (string msg){Debug.Log (msg);}
然后打包成xcode项目,就可以看到能够正常运行了,具体代码不写了,这个不难理解。
0 0
- unity与iOS之间的简单交互
- unity与Android之间的简单交互
- unity与ios交互
- iOS与Unity交互
- Unity与ios交互
- Unity与iOS交互
- Unity 与 Web 的简单交互
- RING0 与 RING3之间的简单交互
- IOS UIWebView与Javascript之间的交互
- IOS UIWebView与Javascript之间的交互
- IOS UIWebView与Javascript之间的交互
- iOS UIWeb与JS之间的交互
- iOS端与网页之间的交互
- 关于Unity与Xcode之间的交互问题
- Unity与Android之间的交互之AndroidManifest
- 记录一些关于android与unity之间交互的文章
- Unity 与IOS基本交互
- html网页与iOS的简单交互
- java excle导出
- 《好书推荐》提高Unity中C#代码质量的xxx条准则
- RecyclerView的使用
- 搭建x86汇编语言学习环境
- TortoiseGit 配置密钥的图文教程
- unity与iOS之间的简单交互
- 网管的自我修养-服务器相关
- CentOS7安装Nagios并配置出图详解
- ZCMU-1626-计算面积
- js模块化编码
- vim 设置tab宽度
- 文件缓冲区测试实例及总结
- db2解锁及数据导入
- 元数据的使用